Skip to content

Commit

Permalink
Merge pull request #13 from Napp/add_float_to_transformer
Browse files Browse the repository at this point in the history
Add float dataType to transformer
  • Loading branch information
Mads Møller authored May 8, 2018
2 parents 18c35ce + 19799af commit eab647b
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
2 changes: 2 additions & 0 deletions src/Transformers/ApiTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,8 @@ protected function convertValueType(string $key, $value)
return (array) $value;
case 'json':
return json_decode($value);
case 'float':
return (float) $value;
default:
return $value;
}
Expand Down
13 changes: 9 additions & 4 deletions tests/ApiTransformerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public function setUp()
'name' => ['newName' => 'companyName', 'dataType' => 'string'],
'has_access' => ['newName' => 'hasAccess', 'dataType' => 'bool'],
'categories' => ['newName' => 'cats', 'dataType' => 'array'],
'price' => ['newName' => 'price', 'dataType' => 'float']
];

$this->transformer = new ApiTransformer();
Expand All @@ -34,15 +35,17 @@ public function test_input_transforming()
'companyName' => 'Wayne Industries',
'hasAccess' => 0,
'someAdditionalField' => 'someAdditionalValue',
'cats' => ['foo' => 'bar']
'cats' => ['foo' => 'bar'],
'price' => '1000'
];

$expectedInput = [
'id' => 1,
'name' => 'Wayne Industries',
'has_access' => 0,
'someAdditionalField' => 'someAdditionalValue',
'categories' => ['foo' => 'bar']
'categories' => ['foo' => 'bar'],
'price' => (float) 1000,
];
$transformedInput = $this->transformer->transformInput($input);

Expand Down Expand Up @@ -79,14 +82,16 @@ public function test_output_transforming()
'id' => 1,
'name' => 'Wayne Industries',
'has_access' => 0,
'some_additional_field' => 'some_additional_value'
'some_additional_field' => 'some_additional_value',
'price' => '1000'
];

$expectedOutput = [
'id' => 1,
'companyName' => 'Wayne Industries',
'hasAccess' => false,
'some_additional_field' => 'some_additional_value'
'some_additional_field' => 'some_additional_value',
'price' => (float) 1000
];

$transformedOutput = $this->transformer->transformOutput($output);
Expand Down

0 comments on commit eab647b

Please sign in to comment.