Skip to content

Commit 9f585e5

Browse files
[Feature] Auto-detect date attributes for Eloquent hydration
Merge pull request #52 from Neuromobile/default-dates-attribute-hydrator Use dates attribute from Eloquent model for hydrator by default
2 parents ee05e63 + b5a01d9 commit 9f585e5

File tree

1 file changed

+12
-5
lines changed

1 file changed

+12
-5
lines changed

src/Hydrator/EloquentHydrator.php

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ class EloquentHydrator extends AbstractHydrator implements HydratesRelatedInterf
8585
*
8686
* @var string[]
8787
*/
88-
protected $dates = [];
88+
protected $dates = NULL;
8989

9090
/**
9191
* Resource relationship keys that should be automatically hydrated.
@@ -163,7 +163,7 @@ protected function hydrateAttributes(StandardObjectInterface $attributes, $recor
163163
}
164164

165165
if ($attributes->has($resourceKey)) {
166-
$data[$modelKey] = $this->deserializeAttribute($attributes->get($resourceKey), $resourceKey);
166+
$data[$modelKey] = $this->deserializeAttribute($record, $attributes->get($resourceKey), $resourceKey);
167167
}
168168
}
169169

@@ -255,15 +255,16 @@ protected function keyForAttribute($resourceKey, Model $model)
255255
/**
256256
* Deserialize a value obtained from the resource's attributes.
257257
*
258+
* @param $record
258259
* @param $value
259260
* the value that the client provided.
260261
* @param $resourceKey
261262
* the attribute key for the value
262263
* @return Carbon|null
263264
*/
264-
protected function deserializeAttribute($value, $resourceKey)
265+
protected function deserializeAttribute($record, $value, $resourceKey)
265266
{
266-
if ($this->isDateAttribute($resourceKey)) {
267+
if ($this->isDateAttribute($record, $resourceKey)) {
267268
return $this->deserializeDate($value);
268269
}
269270

@@ -282,11 +283,17 @@ protected function deserializeDate($value)
282283
/**
283284
* Is this resource key a date attribute?
284285
*
286+
* @param $record
285287
* @param $resourceKey
286288
* @return bool
287289
*/
288-
protected function isDateAttribute($resourceKey)
290+
protected function isDateAttribute($record, $resourceKey)
289291
{
292+
if(is_null($this->dates))
293+
{
294+
return in_array(Str::snake($resourceKey), $record->getDates(), true);
295+
}
296+
290297
return in_array($resourceKey, $this->dates, true);
291298
}
292299

0 commit comments

Comments
 (0)