From 96099fa9e24491b827db965c1b31b4e45b87eda7 Mon Sep 17 00:00:00 2001 From: Mdewet Date: Thu, 6 Dec 2018 09:48:03 +0100 Subject: [PATCH] Add JSON Guesser and Transformer --- features/bootstrap/FeatureContext.php | 2 +- src/Populator/Guesser/JsonGuesser.php | 38 ++++++++++++++++++++++++++ src/Resources/config/services.xml | 9 +++++++ src/Transformer/JsonTransformer.php | 39 +++++++++++++++++++++++++++ tests/app/Entity/Beer.php | 2 +- 5 files changed, 88 insertions(+), 2 deletions(-) create mode 100755 src/Populator/Guesser/JsonGuesser.php create mode 100644 src/Transformer/JsonTransformer.php diff --git a/features/bootstrap/FeatureContext.php b/features/bootstrap/FeatureContext.php index b513772..4225ff5 100755 --- a/features/bootstrap/FeatureContext.php +++ b/features/bootstrap/FeatureContext.php @@ -100,7 +100,7 @@ public function iRunBehat($argumentsString = '') $argumentsString, '--lang=en', '--no-colors', - '--format-settings=\'{"timer": false}\'' + '--format-settings=\'{"timer": false}\'', ], __DIR__.'/../../tests/app'); $this->process->run(); } diff --git a/src/Populator/Guesser/JsonGuesser.php b/src/Populator/Guesser/JsonGuesser.php new file mode 100755 index 0000000..3df241a --- /dev/null +++ b/src/Populator/Guesser/JsonGuesser.php @@ -0,0 +1,38 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +declare(strict_types=1); + +namespace ApiExtension\Populator\Guesser; + +use Doctrine\DBAL\Types\Type; + +/** + * @author Mathieu Dewet + */ +class JsonGuesser extends AbstractGuesser implements GuesserAwareInterface +{ + use GuesserAwareTrait; + + public function supports(array $mapping): bool + { + $class_name = Type::class; + $constant = "$class_name::JSON"; + $constant_value = \defined($constant) ? $constant : null; + + return \in_array($mapping['type'], (null !== $constant_value) ? [Type::JSON, Type::JSON_ARRAY] : [Type::JSON_ARRAY], true); + } + + public function getValue(array $mapping): array + { + return $this->guesser->getValue(['type' => Type::TARRAY]); + } +} diff --git a/src/Resources/config/services.xml b/src/Resources/config/services.xml index 882f38b..6799c77 100644 --- a/src/Resources/config/services.xml +++ b/src/Resources/config/services.xml @@ -114,6 +114,11 @@ + + + + + @@ -211,6 +216,10 @@ + + + + diff --git a/src/Transformer/JsonTransformer.php b/src/Transformer/JsonTransformer.php new file mode 100644 index 0000000..e6d9a90 --- /dev/null +++ b/src/Transformer/JsonTransformer.php @@ -0,0 +1,39 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +declare(strict_types=1); + +namespace ApiExtension\Transformer; + +use Doctrine\DBAL\Types\Type; + +/** + * @author Mathieu Dewet + */ +final class JsonTransformer implements TransformerInterface +{ + public function supports(array $mapping, $value): bool + { + $typeClass = Type::class; + + return \in_array($mapping['type'], \defined("$typeClass::JSON") ? [Type::JSON, Type::JSON_ARRAY] : [Type::JSON_ARRAY], true) && \is_string($value); + } + + public function toObject(array $mapping, $value): array + { + return \json_decode($value, true); + } + + public function toScalar(array $mapping, $value): array + { + return $this->toObject($mapping, $value); + } +} diff --git a/tests/app/Entity/Beer.php b/tests/app/Entity/Beer.php index 711cf6a..79f7f40 100644 --- a/tests/app/Entity/Beer.php +++ b/tests/app/Entity/Beer.php @@ -80,7 +80,7 @@ class Beer private $volume; /** - * @var boolean + * @var bool * @ORM\Column(type="boolean", name="is_active") * @Groups({"beer_read", "beer_write"}) */