Skip to content

Commit

Permalink
Add float dataType to transformer
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Lundbøl <[email protected]>
  • Loading branch information
Michael Lundbøl committed May 8, 2018
1 parent eec6a54 commit 19799af
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 @@ -127,6 +127,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 19799af

Please sign in to comment.