diff --git a/src/Transformers/ApiTransformer.php b/src/Transformers/ApiTransformer.php index 0288ff1..ec438f4 100644 --- a/src/Transformers/ApiTransformer.php +++ b/src/Transformers/ApiTransformer.php @@ -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; } diff --git a/tests/ApiTransformerTest.php b/tests/ApiTransformerTest.php index 0eeb86b..3ac91b4 100644 --- a/tests/ApiTransformerTest.php +++ b/tests/ApiTransformerTest.php @@ -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(); @@ -34,7 +35,8 @@ public function test_input_transforming() 'companyName' => 'Wayne Industries', 'hasAccess' => 0, 'someAdditionalField' => 'someAdditionalValue', - 'cats' => ['foo' => 'bar'] + 'cats' => ['foo' => 'bar'], + 'price' => '1000' ]; $expectedInput = [ @@ -42,7 +44,8 @@ public function test_input_transforming() 'name' => 'Wayne Industries', 'has_access' => 0, 'someAdditionalField' => 'someAdditionalValue', - 'categories' => ['foo' => 'bar'] + 'categories' => ['foo' => 'bar'], + 'price' => (float) 1000, ]; $transformedInput = $this->transformer->transformInput($input); @@ -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);