From 6f77ce9a06db8ad5307f975e6d729446f804e0ac Mon Sep 17 00:00:00 2001 From: Joel Wurtz Date: Wed, 17 Oct 2018 11:57:37 +0200 Subject: [PATCH] Add tests and fix some cs --- Generator/Normalizer/NormalizerGenerator.php | 4 +- Guesser/Guess/MultipleType.php | 18 ++-- Tests/fixtures/one-of/.jane | 8 ++ Tests/fixtures/one-of/expected/Model/Foo.php | 39 +++++++ .../expected/Normalizer/FooNormalizer.php | 100 ++++++++++++++++++ .../expected/Normalizer/NormalizerFactory.php | 24 +++++ Tests/fixtures/one-of/schema.json | 53 ++++++++++ 7 files changed, 235 insertions(+), 11 deletions(-) create mode 100644 Tests/fixtures/one-of/.jane create mode 100644 Tests/fixtures/one-of/expected/Model/Foo.php create mode 100644 Tests/fixtures/one-of/expected/Normalizer/FooNormalizer.php create mode 100644 Tests/fixtures/one-of/expected/Normalizer/NormalizerFactory.php create mode 100644 Tests/fixtures/one-of/schema.json diff --git a/Generator/Normalizer/NormalizerGenerator.php b/Generator/Normalizer/NormalizerGenerator.php index c2203fc..57d2b55 100644 --- a/Generator/Normalizer/NormalizerGenerator.php +++ b/Generator/Normalizer/NormalizerGenerator.php @@ -66,8 +66,8 @@ protected function createSupportsNormalizationMethod($modelFqdn) /** * Create the normalization method. * - * @param string $modelFqdn - * @param Context $context + * @param string $modelFqdn + * @param Context $context * @param ClassGuess $classGuess * * @return Stmt\ClassMethod diff --git a/Guesser/Guess/MultipleType.php b/Guesser/Guess/MultipleType.php index 48ee554..1458a14 100644 --- a/Guesser/Guess/MultipleType.php +++ b/Guesser/Guess/MultipleType.php @@ -51,16 +51,18 @@ public function getTypes() /** * We have to place mixed normalization path at the last. + * * @return Type[] */ protected function getTypesSorted() { $types = $this->getTypes(); - usort($types, function($first, $second) { - /** @var Type $first */ - /** @var Type $second */ + usort($types, function ($first, $second) { + /* @var Type $first */ + /* @var Type $second */ return $first->name == 'mixed' ? 1 : 0; }); + return $types; } @@ -120,10 +122,9 @@ public function createDenormalizationStatement(Context $context, Expr $input): a $condition = $type->createConditionStatement($input); $statement = array_merge($typeStatements, [new Expr\Assign($output, $typeOutput)]); - if($ifStmt === null) { + if ($ifStmt === null) { $ifStmt = new Stmt\If_($condition, ['stmts' => $statement]); - } - else { + } else { $ifStmt->elseifs[] = new Stmt\ElseIf_($condition, $statement); } } @@ -150,10 +151,9 @@ public function createNormalizationStatement(Context $context, Expr $input): arr $condition = $type->createNormalizationConditionStatement($input); $statement = array_merge($typeStatements, [new Expr\Assign($output, $typeOutput)]); - if($ifStmt === null) { + if ($ifStmt === null) { $ifStmt = new Stmt\If_($condition, ['stmts' => $statement]); - } - else { + } else { $ifStmt->elseifs[] = new Stmt\ElseIf_($condition, $statement); } } diff --git a/Tests/fixtures/one-of/.jane b/Tests/fixtures/one-of/.jane new file mode 100644 index 0000000..5714e73 --- /dev/null +++ b/Tests/fixtures/one-of/.jane @@ -0,0 +1,8 @@ + __DIR__ . '/schema.json', + 'root-class' => 'Test', + 'namespace' => 'Jane\JsonSchema\Tests\Expected', + 'directory' => __DIR__ . '/generated', +]; diff --git a/Tests/fixtures/one-of/expected/Model/Foo.php b/Tests/fixtures/one-of/expected/Model/Foo.php new file mode 100644 index 0000000..88486f3 --- /dev/null +++ b/Tests/fixtures/one-of/expected/Model/Foo.php @@ -0,0 +1,39 @@ +foo; + } + + /** + * @param string|object|null[] $foo + * + * @return self + */ + public function setFoo($foo): self + { + $this->foo = $foo; + + return $this; + } +} diff --git a/Tests/fixtures/one-of/expected/Normalizer/FooNormalizer.php b/Tests/fixtures/one-of/expected/Normalizer/FooNormalizer.php new file mode 100644 index 0000000..da27a39 --- /dev/null +++ b/Tests/fixtures/one-of/expected/Normalizer/FooNormalizer.php @@ -0,0 +1,100 @@ +{'$ref'})) { + return new Reference($data->{'$ref'}, $context['document-origin']); + } + $object = new \Jane\JsonSchema\Tests\Expected\Model\Foo(); + if (property_exists($data, 'foo')) { + $value = $data->{'foo'}; + if (is_string($data->{'foo'})) { + $value = $data->{'foo'}; + } elseif (isset($data->{'foo'})) { + $values = new \ArrayObject([], \ArrayObject::ARRAY_AS_PROPS); + foreach ($data->{'foo'} as $key => $value_1) { + if (preg_match('/^[a-zA-Z0-9._-]+$/', $key) && isset($value_1)) { + $value_2 = $value_1; + if (is_object($value_1)) { + $value_2 = $value_1; + } elseif (is_null($value_1)) { + $value_2 = $value_1; + } + $values[$key] = $value_2; + continue; + } + } + $value = $values; + } + $object->setFoo($value); + } + + return $object; + } + + public function normalize($object, $format = null, array $context = []) + { + $data = new \stdClass(); + if (null !== $object->getFoo()) { + $value = $object->getFoo(); + if (is_string($object->getFoo())) { + $value = $object->getFoo(); + } elseif (!is_null($object->getFoo())) { + $values = new \stdClass(); + foreach ($object->getFoo() as $key => $value_1) { + if (preg_match('/^[a-zA-Z0-9._-]+$/', $key) && !is_null($value_1)) { + $value_2 = $value_1; + if (is_object($value_1)) { + $value_2 = $value_1; + } elseif (is_null($value_1)) { + $value_2 = $value_1; + } + $values->{$key} = $value_2; + continue; + } + } + $value = $values; + } + $data->{'foo'} = $value; + } + + return $data; + } +} diff --git a/Tests/fixtures/one-of/expected/Normalizer/NormalizerFactory.php b/Tests/fixtures/one-of/expected/Normalizer/NormalizerFactory.php new file mode 100644 index 0000000..76ed41c --- /dev/null +++ b/Tests/fixtures/one-of/expected/Normalizer/NormalizerFactory.php @@ -0,0 +1,24 @@ +