/** * Get the instance as an array. * * @return array */ public function toArray() { $model = []; foreach ($this->model->attributesToArray() as $attribute => $value) { $model[$attribute] = $this->{$attribute}; if (is_object($model[$attribute]) && is_callable([$model[$attribute], 'toArray'])) { $model[$attribute] = $model[$attribute]->toArray(); } } foreach ($this->model->getRelations() as $relationName => $relation) { try { $pivotRelationName = $relation->relationName; } catch (Exception $e) { // Fall though... } if (isset($pivotRelationName)) { $relationName = $pivotRelationName; } if (is_object($relation) && is_callable([$relation, 'toArray'])) { $model[$relationName] = $relation->toArray(); } else { $model[$relationName] = $relation; } } if (!empty($this->additionalAttributes)) { foreach ($this->additionalAttributes as $attribute) { $model[$attribute] = $this->{$attribute}; if (is_object($model[$attribute]) && is_callable([$model[$attribute], 'toArray'])) { $model[$attribute] = $model[$attribute]->toArray(); } } } return $model; }
/** * Convert a value to XML. * * @param Model $value * @param SimpleXMLElement $element * @return SimpleXMLElement * @throws CantConvertValueException */ public function convert($value, SimpleXMLElement $element) : SimpleXMLElement { if (!$value instanceof Model) { throw new CantConvertValueException("Value is not a model."); } return $this->prepareElement(collect($value->attributesToArray()), $element); }
/** * Bind Model to the form * * @param Model|Array $model * @return $this */ public function bind($model = []) { /** * Cast eloquent model to array */ $data = $model instanceof Model ? $model->attributesToArray() : $model; $this->bind = $this->collection->make($data); return $this; }
/** * Decorates a single Eloquent Model * * @param Model $item * * @return array */ public function decorate(Model $item) { $returnObject = []; foreach ($item->attributesToArray() as $attribute => $value) { if (isset($this->decorateTable[$attribute])) { $returnObject[$this->decorateTable[$attribute]] = $value; } } return $returnObject; }
/** * @param Model $value * * @return array */ protected function serializeEloquentModel(Model $value) { $stdClass = (object) $value->attributesToArray(); $data = $this->serializeData($stdClass); $data[self::CLASS_IDENTIFIER_KEY] = get_class($value); $methods = RelationshipPropertyExtractor::getRelationshipAsPropertyName($value, get_class($value), new ReflectionClass($value), $this); if (!empty($methods)) { $data = array_merge($data, $methods); } return $data; }
/** * Convert the model's attributes to an array. * * @inheritdoc */ public function attributesToArray() { // unset date values that are 0, so they don't cause problems with serializeDate() foreach ($this->getDates() as $key) { if (!isset($this->attributes[$key])) { continue; } // 0 should be considered 'unset' aswell if (0 === $this->attributes[$key]) { $this->attributes[$key] = null; } } $attributes = parent::attributesToArray(); // make sure that our date values are strings foreach ($this->getDates() as $key) { if ($attributes[$key] instanceof \DateTime) { $attributes[$key] = $attributes[$key]->format($this->getDateFormat()); } } return $attributes; }
/** * Convert the model's attributes to an array. * * @return array */ public function attributesToArray() { $attributes = parent::attributesToArray(); // Because the original Eloquent never returns objects, we convert // MongoDB related objects to a string representation. This kind // of mimics the SQL behaviour so that dates are formatted // nicely when your models are converted to JSON. foreach ($attributes as $key => &$value) { if ($value instanceof MongoId) { $value = (string) $value; } } // Convert dot-notation dates. foreach ($this->getDates() as $key) { if (str_contains($key, '.') and array_has($attributes, $key)) { array_set($attributes, $key, (string) $this->asDateTime(array_get($attributes, $key))); } } return $attributes; }
/** * Lets the model consume the old one * @param Model $model The old model * @return $this */ public function consumeModel(Model $model) { $this->consumedModel = $model; $this->consumedModelProps = array_merge($model->attributesToArray(), $model->relationsToArray()); return $this; }
/** * Convert the model's attributes to an array. * * @inheritdoc */ public function attributesToArray() { // unset date values that are 0, so they don't cause problems with serializeDate() foreach ($this->getDates() as $key) { if (!isset($this->attributes[$key])) { continue; } // 0 should be considered 'unset' aswell if (0 === $this->attributes[$key]) { $this->attributes[$key] = null; } } return parent::attributesToArray(); }
/** * @param Driver $serializer * @param Model $model * * @return array */ protected static function getModelData(Driver $serializer, Model $model) { $stdClass = (object) $model->attributesToArray(); $data = $serializer->serialize($stdClass); $data[Serializer::CLASS_IDENTIFIER_KEY] = get_class($model); return $data; }
/** * Convert the model's attributes to an array. * * @return array */ public function attributesToArray() { $attributes = parent::attributesToArray(); // Convert dot-notation dates. foreach ($this->getDates() as $key) { if (str_contains($key, '.') and array_has($attributes, $key)) { array_set($attributes, $key, (string) $this->asDateTime(array_get($attributes, $key))); } } return $attributes; }
/** * {@inheritdoc} */ public function attributesToArray() { $attributes = parent::attributesToArray(); foreach ($attributes as $key => $value) { if (in_array($key, $this->encrypted) && !empty($this->attributes[$key])) { $attributes[$key] = Crypt::decrypt($value); } } return $attributes; }
/** * Return the attributes for a particular model as an array of input values * * @param Model $model The model for which we would like to return the input values * @param string $prefix The prefix that needs to be added to the input * @return array */ public function getInputForModel($model, $prefix = '') { return $this->getPrefixedInput($model->attributesToArray(), $prefix); }
/** * @return array */ public function attributesToArray() { $attributes = parent::attributesToArray(); foreach ($this->jsonFields as $field) { if (array_key_exists($field, $attributes)) { $attributes[$field] = json_decode($attributes[$field], true); } } foreach ($this->booleanFields as $field) { if (array_key_exists($field, $attributes)) { $attributes[$field] = boolval($attributes[$field]); } } return $attributes; }
/** * Convert the model's attributes to an array. * * @return array */ public function attributesToArray() { $attributes = parent::attributesToArray(); $attributes = $this->convertAttributes($attributes); return $attributes; }