Skip to content

Use dates attribute from Eloquent model for hydrator by default #52

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions src/Hydrator/EloquentHydrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -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);
}

Expand All @@ -253,11 +254,17 @@ 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)
{
if(is_null($this->dates))
{
return in_array(Str::snake($resourceKey), $record->getDates(), true);
}

return in_array($resourceKey, $this->dates, true);
}

Expand Down