From 25d62be5a34ea44ec1b915dfa5adbe986ebdc6cb Mon Sep 17 00:00:00 2001 From: Illia Somov Date: Mon, 3 Jun 2019 12:13:46 +0300 Subject: [PATCH] allow only strings in form data; throw exception if integer is invalid; allow transformer instances --- src/Exception/UnexpectedValueException.php | 7 ++++ src/FieldTransformer/CallableTransformer.php | 2 +- src/FieldTransformer/CheckboxField.php | 4 +- src/FieldTransformer/IntegerField.php | 9 +++-- src/FieldTransformer/StringField.php | 3 +- src/FieldTransformerInterface.php | 7 +++- src/FormDataTransformer.php | 4 ++ src/FormDataTransformerInterface.php | 3 ++ tests/FieldTransformer/IntegerFieldTest.php | 8 +++- tests/FormDataTransformerTest.php | 39 +++++++++++++++++++- 10 files changed, 72 insertions(+), 14 deletions(-) create mode 100644 src/Exception/UnexpectedValueException.php diff --git a/src/Exception/UnexpectedValueException.php b/src/Exception/UnexpectedValueException.php new file mode 100644 index 0000000..f8220ee --- /dev/null +++ b/src/Exception/UnexpectedValueException.php @@ -0,0 +1,7 @@ +defaultValue = $defaultValue; } - public function transformField($formFieldValue) + public function transformField(string $formFieldValue) { return ($this->callback)($formFieldValue); } diff --git a/src/FieldTransformer/CheckboxField.php b/src/FieldTransformer/CheckboxField.php index 1434d9d..16d087e 100644 --- a/src/FieldTransformer/CheckboxField.php +++ b/src/FieldTransformer/CheckboxField.php @@ -6,10 +6,8 @@ final class CheckboxField implements FieldTransformerInterface { - public function transformField($formFieldValue) + public function transformField(string $formFieldValue) { - \assert(is_string($formFieldValue)); - return $formFieldValue !== ''; } diff --git a/src/FieldTransformer/IntegerField.php b/src/FieldTransformer/IntegerField.php index 4833a56..8146040 100644 --- a/src/FieldTransformer/IntegerField.php +++ b/src/FieldTransformer/IntegerField.php @@ -2,18 +2,21 @@ namespace Someniatko\FormTransformer\FieldTransformer; +use Someniatko\FormTransformer\Exception\UnexpectedValueException; use Someniatko\FormTransformer\FieldTransformerInterface; final class IntegerField implements FieldTransformerInterface { - public function transformField($formFieldValue) + public function transformField(string $formFieldValue) { - \assert(is_string($formFieldValue)); if ($formFieldValue === '') { return null; } - \assert(is_numeric($formFieldValue)); + if (!is_numeric($formFieldValue)) { + throw new UnexpectedValueException; + } + return (int)$formFieldValue; } diff --git a/src/FieldTransformer/StringField.php b/src/FieldTransformer/StringField.php index 5630cf4..5cb6d38 100644 --- a/src/FieldTransformer/StringField.php +++ b/src/FieldTransformer/StringField.php @@ -6,9 +6,8 @@ final class StringField implements FieldTransformerInterface { - public function transformField($formFieldValue) + public function transformField(string $formFieldValue) { - \assert(is_string($formFieldValue)); return $formFieldValue; } diff --git a/src/FieldTransformerInterface.php b/src/FieldTransformerInterface.php index fe0f10f..605089e 100644 --- a/src/FieldTransformerInterface.php +++ b/src/FieldTransformerInterface.php @@ -2,14 +2,17 @@ namespace Someniatko\FormTransformer; +use Someniatko\FormTransformer\Exception\UnexpectedValueException; + interface FieldTransformerInterface { /** * Transforms form field value into normalized one - * @param mixed $formFieldValue + * @param string $formFieldValue * @return mixed + * @throws UnexpectedValueException */ - public function transformField($formFieldValue); + public function transformField(string $formFieldValue); /** * Returns default field normalized value if form data does not contains the field diff --git a/src/FormDataTransformer.php b/src/FormDataTransformer.php index c76ebe3..16b5e52 100644 --- a/src/FormDataTransformer.php +++ b/src/FormDataTransformer.php @@ -46,6 +46,10 @@ private function createTransformer($configNode) :FieldTransformerInterface { \assert(!is_array($configNode)); + if ($configNode instanceof FieldTransformerInterface) { + return $configNode; + } + return is_callable($configNode) ? new CallableTransformer($configNode) : new $configNode; diff --git a/src/FormDataTransformerInterface.php b/src/FormDataTransformerInterface.php index 78d7d24..61629e8 100644 --- a/src/FormDataTransformerInterface.php +++ b/src/FormDataTransformerInterface.php @@ -2,6 +2,8 @@ namespace Someniatko\FormTransformer; +use Someniatko\FormTransformer\Exception\UnexpectedValueException; + interface FormDataTransformerInterface { /** @@ -14,6 +16,7 @@ interface FormDataTransformerInterface * field name => FieldTransformer class * Fields not specified in the config will be left as is. * @return array Normalized data + * @throws UnexpectedValueException */ public function transform(array $formData, array $config) :array; } diff --git a/tests/FieldTransformer/IntegerFieldTest.php b/tests/FieldTransformer/IntegerFieldTest.php index 1d14576..c187d7f 100644 --- a/tests/FieldTransformer/IntegerFieldTest.php +++ b/tests/FieldTransformer/IntegerFieldTest.php @@ -5,7 +5,7 @@ use PHPUnit\Framework\TestCase; use Someniatko\FormTransformer\FieldTransformer\IntegerField; -final class IntegerFieldTest extends TestCase +final class IntegerFieldTest extends TestCase { /** * @dataProvider fieldDataProvider @@ -26,4 +26,10 @@ public function testGetDefaultValue() :void { $this->assertNull((new IntegerField)->getDefaultValue()); } + + public function testNonNumericValueResultsInException(): void + { + $this->expectException(\UnexpectedValueException::class); + (new IntegerField)->transformField('abcd'); + } } diff --git a/tests/FormDataTransformerTest.php b/tests/FormDataTransformerTest.php index f9f0133..49e490b 100644 --- a/tests/FormDataTransformerTest.php +++ b/tests/FormDataTransformerTest.php @@ -8,9 +8,9 @@ class BorkTransformer implements FieldTransformerInterface { - public function transformField($formFieldValue) + public function transformField(string $formFieldValue) { - return $formFieldValue === null ? null : $formFieldValue.'-bork'; + return $formFieldValue.'-bork'; } public function getDefaultValue() @@ -19,6 +19,26 @@ public function getDefaultValue() } } +final class TransformerWithConstructor implements FieldTransformerInterface +{ + private $addition; + + public function __construct(string $addition) + { + $this->addition = $addition; + } + + public function transformField(string $formFieldValue) + { + return $formFieldValue.$this->addition; + } + + public function getDefaultValue() + { + return null; + } +} + final class FormDataTransformerTest extends TestCase { public function testTransformWithoutConfig() :void @@ -216,4 +236,19 @@ public function testTransformCallableInsideEach() :void ], ], (new FormDataTransformer)->transform($formData, $config)); } + + public function testTransformWithTransformerInstance(): void + { + $formData = [ + 'wolf' => 'boo' + ]; + + $config = [ + 'wolf' => new TransformerWithConstructor('-bar') + ]; + + $this->assertEquals([ + 'wolf' => 'boo-bar', + ], (new FormDataTransformer)->transform($formData, $config)); + } }