10
10
use PHPModelGenerator \Model \Property \PropertyType ;
11
11
use PHPModelGenerator \Model \SchemaDefinition \JsonSchema ;
12
12
use PHPModelGenerator \Model \Validator \PropertyValidator ;
13
- use PHPModelGenerator \PropertyProcessor \ PropertyProcessorInterface ;
13
+ use PHPModelGenerator \Utils \ RenderHelper ;
14
14
use PHPModelGenerator \Utils \TypeConverter ;
15
15
16
16
/**
17
17
* Class ConstProcessor
18
18
*
19
19
* @package PHPModelGenerator\PropertyProcessor\Property
20
20
*/
21
- class ConstProcessor implements PropertyProcessorInterface
21
+ class ConstProcessor extends AbstractPropertyProcessor
22
22
{
23
23
/**
24
24
* @inheritdoc
@@ -34,13 +34,26 @@ public function process(string $propertyName, JsonSchema $propertySchema): Prope
34
34
$ json ['description ' ] ?? '' ,
35
35
);
36
36
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 ;
45
58
}
46
59
}
0 commit comments