Skip to content

Commit

Permalink
Fixing bugs caught by unit tests to transformer and base model.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeremy Sik committed Aug 6, 2015
1 parent ffe2453 commit d103891
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 7 deletions.
12 changes: 7 additions & 5 deletions api/app/Http/Transformers/EloquentModelTransformer.php
Expand Up @@ -42,6 +42,13 @@ public function transform($object)
throw new \InvalidArgumentException('must be array or '.Arrayable::class.' instead got '.gettype($object));
}

if (($object instanceof BaseModel)) {
$castTypes = $object['casts'];
foreach ($array as $key => $value) {
$array[$key] = $this->castAttribute($castTypes, $key, $value);
}
}

foreach ($array as $key => $value) {

// Handle snakecase conversion in sub arrays
Expand All @@ -58,11 +65,6 @@ public function transform($object)
}

if (($object instanceof BaseModel)) {
$castTypes = $object['casts'];
foreach ($array as $key => $value) {
$array[$key] = $this->castAttribute($castTypes, $key, $value);
}

$this->addSelfKey($object, $array);
}

Expand Down
4 changes: 4 additions & 0 deletions api/app/Models/BaseModel.php
Expand Up @@ -71,6 +71,10 @@ protected function castAttribute($key, $value)
// Run the parent cast rules in the parent method
$value = parent::castAttribute($key, $value);

if(is_null($value)) {
return $value;
}

switch ($this->getCastType($key)) {
case 'date':
return Carbon::createFromFormat('Y-m-d', $value);
Expand Down
4 changes: 2 additions & 2 deletions api/app/Models/TestEntity.php
Expand Up @@ -39,8 +39,8 @@ class TestEntity extends BaseModel
protected $casts = [
'decimal' => 'float',
'date' => 'date',
'created_at' => 'dateTime',
'updated_at' => 'dateTime',
'created_at' => 'datetime',
'updated_at' => 'datetime',
];

protected $validationRules = [
Expand Down

0 comments on commit d103891

Please sign in to comment.