1010use PHPModelGenerator \Model \Property \PropertyType ;
1111use PHPModelGenerator \Model \SchemaDefinition \JsonSchema ;
1212use PHPModelGenerator \Model \Validator \PropertyValidator ;
13- use PHPModelGenerator \PropertyProcessor \ PropertyProcessorInterface ;
13+ use PHPModelGenerator \Utils \ RenderHelper ;
1414use PHPModelGenerator \Utils \TypeConverter ;
1515
1616/**
1717 * Class ConstProcessor
1818 *
1919 * @package PHPModelGenerator\PropertyProcessor\Property
2020 */
21- class ConstProcessor implements PropertyProcessorInterface
21+ class ConstProcessor extends AbstractPropertyProcessor
2222{
2323 /**
2424 * @inheritdoc
@@ -34,13 +34,26 @@ public function process(string $propertyName, JsonSchema $propertySchema): Prope
3434 $ json ['description ' ] ?? '' ,
3535 );
3636
37- return $ property
38- ->setRequired (true )
39- ->addValidator (new PropertyValidator (
40- $ property ,
41- '$value !== ' . var_export ($ json ['const ' ], true ),
42- InvalidConstException::class,
43- [$ json ['const ' ]],
44- ));
37+ $ property ->setRequired ($ this ->propertyMetaDataCollection ->isAttributeRequired ($ propertyName ));
38+
39+ $ check = match (true ) {
40+ $ property ->isRequired ()
41+ => '$value !== ' . var_export ($ json ['const ' ], true ),
42+ $ this ->isImplicitNullAllowed ($ property )
43+ => '!in_array($value, ' . RenderHelper::varExportArray ([$ json ['const ' ], null ]) . ', true) ' ,
44+ default
45+ => "array_key_exists(' {$ property ->getName ()}', \$modelData) && \$value !== " . var_export ($ json ['const ' ], true ),
46+ };
47+
48+ $ property ->addValidator (new PropertyValidator (
49+ $ property ,
50+ $ check ,
51+ InvalidConstException::class,
52+ [$ json ['const ' ]],
53+ ));
54+
55+ $ this ->generateValidators ($ property , $ propertySchema );
56+
57+ return $ property ;
4558 }
4659}
0 commit comments