From 6ae5a27f9f99525f053e7b0382dfbd3349f1d0ea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pedro=20Luis=20L=C3=B3pez=20S=C3=A1nchez?= Date: Wed, 1 Mar 2017 13:48:46 +0100 Subject: [PATCH 1/2] Use dates attribute from Eloquent model for hydrator by default --- src/Hydrator/EloquentHydrator.php | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/src/Hydrator/EloquentHydrator.php b/src/Hydrator/EloquentHydrator.php index 82ae3ab9..8cf434ea 100644 --- a/src/Hydrator/EloquentHydrator.php +++ b/src/Hydrator/EloquentHydrator.php @@ -77,7 +77,7 @@ class EloquentHydrator extends AbstractHydrator implements HydratesRelatedInterf * * @var string[] */ - protected $dates = []; + protected $dates = NULL; /** * Resource relationship keys that should be automatically hydrated. @@ -134,7 +134,7 @@ protected function hydrateAttributes(StandardObjectInterface $attributes, $recor } if ($attributes->has($resourceKey)) { - $data[$modelKey] = $this->deserializeAttribute($attributes->get($resourceKey), $resourceKey); + $data[$modelKey] = $this->deserializeAttribute($record, $attributes->get($resourceKey), $resourceKey); } } @@ -226,15 +226,16 @@ protected function keyForAttribute($resourceKey, Model $model) /** * Deserialize a value obtained from the resource's attributes. * + * @param $record * @param $value * the value that the client provided. * @param $resourceKey * the attribute key for the value * @return Carbon|null */ - protected function deserializeAttribute($value, $resourceKey) + protected function deserializeAttribute($record, $value, $resourceKey) { - if ($this->isDateAttribute($resourceKey)) { + if ($this->isDateAttribute($record, $resourceKey)) { return $this->deserializeDate($value); } @@ -253,12 +254,20 @@ protected function deserializeDate($value) /** * Is this resource key a date attribute? * + * @param $record * @param $resourceKey * @return bool */ - protected function isDateAttribute($resourceKey) + protected function isDateAttribute($record, $resourceKey) { - return in_array($resourceKey, $this->dates, true); + if(is_null($this->dates)) + { + return in_array(Str::snake($resourceKey), $record->getDates(), true); + } + else + { + return in_array($resourceKey, $this->dates, true); + } } /** From b5a01d903083e23c198dcc6b6fb46259470b5e04 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pedro=20Luis=20L=C3=B3pez=20S=C3=A1nchez?= Date: Wed, 1 Mar 2017 14:44:15 +0100 Subject: [PATCH 2/2] Improve code about use dates attribute from model --- src/Hydrator/EloquentHydrator.php | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/Hydrator/EloquentHydrator.php b/src/Hydrator/EloquentHydrator.php index 8cf434ea..e74f433e 100644 --- a/src/Hydrator/EloquentHydrator.php +++ b/src/Hydrator/EloquentHydrator.php @@ -264,10 +264,8 @@ protected function isDateAttribute($record, $resourceKey) { return in_array(Str::snake($resourceKey), $record->getDates(), true); } - else - { - return in_array($resourceKey, $this->dates, true); - } + + return in_array($resourceKey, $this->dates, true); } /**