From 6b71b84024466e6d39dc183a349e10632016334a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pedro=20Luis=20L=C3=B3pez=20S=C3=A1nchez?= Date: Tue, 28 Feb 2017 12:09:39 +0100 Subject: [PATCH 1/2] Get default attributes for schemas and hydrators from model filleable attribute --- src/Hydrator/EloquentHydrator.php | 28 +++++++++++++++++++++++++++- src/Schema/EloquentSchema.php | 19 ++++++++++++++++++- 2 files changed, 45 insertions(+), 2 deletions(-) diff --git a/src/Hydrator/EloquentHydrator.php b/src/Hydrator/EloquentHydrator.php index 82ae3ab9..bbb468b7 100644 --- a/src/Hydrator/EloquentHydrator.php +++ b/src/Hydrator/EloquentHydrator.php @@ -115,6 +115,32 @@ public function __construct(HttpServiceInterface $service) { $this->service = $service; } + + /** + * @param StandardObjectInterface $attributes + * @param $record + * @return void + */ + protected function defaultHydrateAttributes(StandardObjectInterface $attributes, $record) + { + $hydratorAttributes = $this->attributes; + if(empty($hydratorAttributes)) + { + $hydratorAttributes = []; + foreach($record->getFillable() as $attribute) + { + if(strpos($attribute, '_') !== false) + { + $hydratorAttributes[str_replace('_', '-', $attribute)] = $attribute; + } + else + { + $hydratorAttributes[] = $attribute; + } + } + } + return $hydratorAttributes; + } /** * @inheritDoc @@ -127,7 +153,7 @@ protected function hydrateAttributes(StandardObjectInterface $attributes, $recor $data = []; - foreach ($this->attributes as $resourceKey => $modelKey) { + foreach ($this->defaultHydrateAttributes($attributes, $record) as $resourceKey => $modelKey) { if (is_numeric($resourceKey)) { $resourceKey = $modelKey; $modelKey = $this->keyForAttribute($modelKey, $record); diff --git a/src/Schema/EloquentSchema.php b/src/Schema/EloquentSchema.php index 01dbf6fb..563fdbae 100644 --- a/src/Schema/EloquentSchema.php +++ b/src/Schema/EloquentSchema.php @@ -154,6 +154,23 @@ protected function getDefaultAttributes(Model $model) return $defaults; } + /** + * Get attributes for the provided model using fillable attribute. + * + * @param Model $model + * @return array + */ + protected function getDefaultModelAttributes(Model $model) + { + $schemaAttributes = $this->attributes; + if(empty($schemaAttributes)) + { + $schemaAttributes = $model->getFillable(); + } + return $schemaAttributes; + } + + /** * Get attributes for the provided model. * @@ -164,7 +181,7 @@ protected function getModelAttributes(Model $model) { $attributes = []; - foreach ($this->attributes as $modelKey => $attributeKey) { + foreach ($this->getDefaultModelAttributes($model) as $modelKey => $attributeKey) { if (is_numeric($modelKey)) { $modelKey = $attributeKey; $attributeKey = $this->keyForAttribute($attributeKey); From 056958265de212cdb4a121d8196bf610750982df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pedro=20Luis=20L=C3=B3pez=20S=C3=A1nchez?= Date: Wed, 1 Mar 2017 11:39:44 +0100 Subject: [PATCH 2/2] Change function names and improvement code --- src/Hydrator/EloquentHydrator.php | 24 ++++++++---------------- src/Schema/EloquentSchema.php | 17 ++++++++++------- 2 files changed, 18 insertions(+), 23 deletions(-) diff --git a/src/Hydrator/EloquentHydrator.php b/src/Hydrator/EloquentHydrator.php index bbb468b7..b0c487cf 100644 --- a/src/Hydrator/EloquentHydrator.php +++ b/src/Hydrator/EloquentHydrator.php @@ -70,7 +70,7 @@ class EloquentHydrator extends AbstractHydrator implements HydratesRelatedInterf * * @var array */ - protected $attributes = []; + protected $attributes = NULL; /** * The resource attributes that are dates. @@ -117,29 +117,21 @@ public function __construct(HttpServiceInterface $service) } /** - * @param StandardObjectInterface $attributes * @param $record * @return void */ - protected function defaultHydrateAttributes(StandardObjectInterface $attributes, $record) + protected function attributeKeys($record) { - $hydratorAttributes = $this->attributes; - if(empty($hydratorAttributes)) + if(is_null($this->attributes)) { - $hydratorAttributes = []; + $fillableAttributes = []; foreach($record->getFillable() as $attribute) { - if(strpos($attribute, '_') !== false) - { - $hydratorAttributes[str_replace('_', '-', $attribute)] = $attribute; - } - else - { - $hydratorAttributes[] = $attribute; - } + $fillableAttributes[Str::dasherize($attribute)] = $attribute; } + return $fillableAttributes; } - return $hydratorAttributes; + return $this->attributes; } /** @@ -153,7 +145,7 @@ protected function hydrateAttributes(StandardObjectInterface $attributes, $recor $data = []; - foreach ($this->defaultHydrateAttributes($attributes, $record) as $resourceKey => $modelKey) { + foreach ($this->attributeKeys($record) as $resourceKey => $modelKey) { if (is_numeric($resourceKey)) { $resourceKey = $modelKey; $modelKey = $this->keyForAttribute($modelKey, $record); diff --git a/src/Schema/EloquentSchema.php b/src/Schema/EloquentSchema.php index 563fdbae..7a5788ae 100644 --- a/src/Schema/EloquentSchema.php +++ b/src/Schema/EloquentSchema.php @@ -82,7 +82,7 @@ abstract class EloquentSchema extends AbstractSchema * * @var array */ - protected $attributes = []; + protected $attributes = NULL; /** * Whether resource member names are hyphenated @@ -160,14 +160,17 @@ protected function getDefaultAttributes(Model $model) * @param Model $model * @return array */ - protected function getDefaultModelAttributes(Model $model) + protected function attributeKeys(Model $model) { - $schemaAttributes = $this->attributes; - if(empty($schemaAttributes)) + if(is_null($this->attributes)) { - $schemaAttributes = $model->getFillable(); + if(! empty($model->getVisible())) + { + return $model->getVisible(); + } + return array_diff($model->getFillable(), $model->getHidden()); } - return $schemaAttributes; + return $this->attributes; } @@ -181,7 +184,7 @@ protected function getModelAttributes(Model $model) { $attributes = []; - foreach ($this->getDefaultModelAttributes($model) as $modelKey => $attributeKey) { + foreach ($this->attributeKeys($model) as $modelKey => $attributeKey) { if (is_numeric($modelKey)) { $modelKey = $attributeKey; $attributeKey = $this->keyForAttribute($attributeKey);