From 19799afe763b5e0082a5c88e8cf3c24e9c8d3f32 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20Lundb=C3=B8l?= Date: Tue, 8 May 2018 17:35:23 +0200 Subject: [PATCH] Add float dataType to transformer MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Michael Lundbøl --- src/Transformers/ApiTransformer.php | 2 ++ tests/ApiTransformerTest.php | 13 +++++++++---- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/src/Transformers/ApiTransformer.php b/src/Transformers/ApiTransformer.php index faf3c39..38947eb 100644 --- a/src/Transformers/ApiTransformer.php +++ b/src/Transformers/ApiTransformer.php @@ -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; } diff --git a/tests/ApiTransformerTest.php b/tests/ApiTransformerTest.php index 8840c10..7e17b89 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);