protected $_cast = false;
protected $_translatable = false;
+ /**
+ * @var CubistMagicAbstractModel
+ */
+ protected $_modelInstance;
+
protected $_databaseAttributes = [];
/**
return new $class($attributes);
}
+ /**
+ * @return CubistMagicAbstractModel
+ */
+ public function getModelInstance(): CubistMagicAbstractModel
+ {
+ return $this->_modelInstance;
+ }
+
+ /**
+ * @param CubistMagicAbstractModel $modelInstance
+ * @return Field
+ */
+ public function setModelInstance(CubistMagicAbstractModel $modelInstance): Field
+ {
+ $this->_modelInstance = $modelInstance;
+ return $this;
+ }
+
+
public function getDatabaseType()
{
if ($this->_translatable) {
public function getDefaultAttributes()
{
- return array_merge(parent::getDefaultAttributes(), ['mime_types' => $this->_mimeTypes, 'multiple' => $this->_multiple]);
+ return array_merge(parent::getDefaultAttributes(), ['mime_types' => $this->_mimeTypes, 'multiple' => $this->_multiple, 'upload' => true, 'disk' => 'uploads']);
}
protected function _postSetAttributes()
return parent::_postSetAttributes();
}
+ public function setMutator($value)
+ {
+ $model = $this->getModelInstance();
+
+ $function = $this->_multiple ? 'uploadMultipleToDisk' : 'uploadToDisk';
+ $path = $model->getAttribute('name') . '/' . $model->getPrimaryKey();
+
+ $model->$function($value, $this->getAttribute('name'), $this->getAttribute('disk'), $path);
+ }
+
}
class Images extends Files
{
protected $_mimeTypes = ['image/png', 'image/jpeg', 'image/svg+xml', 'image/webp', 'image/gif'];
+
+ protected function _postSetAttributes()
+ {
+ if ($this->_multiple) {
+ $this->setAttribute('type', 'upload_multiple');
+ } else {
+ $this->setAttribute('type', 'image');
+ }
+
+ return parent::_postSetAttributes();
+ }
}
{
/** @var Field $field */
$field = Field::getInstance($attributes);
+ $field->setModelInstance($this);
if (is_callable([$field, 'getRelationship']) && null !== $field->getRelationship()) {
$this->_addRelationship($field);
}
public function __call($method, $parameters)
{
+
+ // Set mutators
+ if (preg_match('/^set([a-zA-Z0-9]+)Attribute$/', $method, $matches)) {
+ $attr = Str::snake($matches[1]);
+
+ if (isset($this->_fields[$attr])) {
+ $callback = [$this->_fields[$attr], 'setMutator'];
+ if (is_callable($callback)) {
+ return call_user_func_array($callback, $parameters);
+ }
+ }
+ }
+
+ // magic call of relationships
foreach ($this->_relationships as $relationship) {
/** @var $relationship Field */
if ($method == $relationship->getAttribute('entity')) {