Last active
June 24, 2023 20:51
-
-
Save adrolter/71c74201be41e0e619d09086cb555b62 to your computer and use it in GitHub Desktop.
jane-php/json-schema Symfony 6.3 compat
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff -ruN '--exclude=PATCHES.txt' json-schema/Generator/NormalizerGenerator.php json-schema.symfony-63-getSupportedTypes/Generator/NormalizerGenerator.php | |
--- json-schema/Generator/NormalizerGenerator.php 2023-06-24 16:46:45.876081361 -0400 | |
+++ json-schema.symfony-63-getSupportedTypes/Generator/NormalizerGenerator.php 2023-06-24 16:46:39.636091630 -0400 | |
@@ -9,6 +9,9 @@ | |
use Jane\Component\JsonSchema\Registry\Schema; | |
use PhpParser\Node\Expr; | |
use PhpParser\Node\Name; | |
+use PhpParser\Node\NullableType; | |
+use PhpParser\Node\Param; | |
+use PhpParser\Node\Scalar; | |
use PhpParser\Node\Stmt; | |
use PhpParser\Parser; | |
@@ -94,6 +97,7 @@ | |
$methods[] = $this->createSupportsNormalizationMethod($modelFqdn); | |
$methods[] = $this->createDenormalizeMethod($modelFqdn, $context, $class); | |
$methods[] = $this->createNormalizeMethod($modelFqdn, $context, $class, $this->skipNullValues, $this->skipRequiedFields); | |
+ $methods[] = $this->createGetSupportedTypesMethod($modelFqdn, $this->useCacheableSupportsMethod); | |
if ($this->useCacheableSupportsMethod) { | |
$methods[] = $this->createHasCacheableSupportsMethod(); | |
@@ -194,4 +198,28 @@ | |
return array_merge($useStmts, [$normalizerClass]); | |
} | |
+ | |
+ /** | |
+ * Create method to return the supported type. | |
+ * | |
+ * @param string $modelFqdn Fully Qualified name of the model class denormalized | |
+ * | |
+ * @return Stmt\ClassMethod | |
+ */ | |
+ protected function createGetSupportedTypesMethod(string $modelFqdn, $useCacheableSupportsMethod = false) | |
+ { | |
+ return new Stmt\ClassMethod('getSupportedTypes', [ | |
+ 'type' => Stmt\Class_::MODIFIER_PUBLIC, | |
+ 'returnType' => 'array', | |
+ 'params' => [ | |
+ new Param(new Expr\Variable('format'), new Expr\ConstFetch(new Name('null')), new NullableType('string')), | |
+ ], | |
+ 'stmts' => [new Stmt\Return_(new Expr\Array_([ | |
+ new Expr\ArrayItem( | |
+ new Expr\ConstFetch(new Name($useCacheableSupportsMethod ? 'true' : 'false')), | |
+ new Scalar\String_($modelFqdn), | |
+ ), | |
+ ]))], | |
+ ]); | |
+ } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment