66
77use JsonSchema \ConstraintError ;
88use JsonSchema \Constraints \ConstraintInterface ;
9- use JsonSchema \Constraints \Factory ;
109use JsonSchema \Entity \ErrorBagProxy ;
1110use JsonSchema \Entity \JsonPointer ;
1211
1312class DependenciesConstraint implements ConstraintInterface
1413{
1514 use ErrorBagProxy;
1615
16+ /** @var Factory */
17+ private $ factory ;
18+
1719 public function __construct (?Factory $ factory = null )
1820 {
19- $ this ->initialiseErrorBag ($ factory ?: new Factory ());
21+ $ this ->factory = $ factory ?: new Factory ();
22+ $ this ->initialiseErrorBag ($ this ->factory );
2023 }
2124
2225 public function check (&$ value , $ schema = null , ?JsonPointer $ path = null , $ i = null ): void
@@ -40,9 +43,20 @@ public function check(&$value, $schema = null, ?JsonPointer $path = null, $i = n
4043 $ this ->addError (ConstraintError::FALSE (), $ path , ['dependant ' => $ dependant ]);
4144 continue ;
4245 }
43- foreach ($ dependencies as $ dependency ) {
44- if (property_exists ($ value , $ dependant ) && !property_exists ($ value , $ dependency )) {
45- $ this ->addError (ConstraintError::DEPENDENCIES (), $ path , ['dependant ' => $ dependant , 'dependency ' => $ dependency ]);
46+
47+ if (is_array ($ dependencies )) {
48+ foreach ($ dependencies as $ dependency ) {
49+ if (property_exists ($ value , $ dependant ) && !property_exists ($ value , $ dependency )) {
50+ $ this ->addError (ConstraintError::DEPENDENCIES (), $ path , ['dependant ' => $ dependant , 'dependency ' => $ dependency ]);
51+ }
52+ }
53+ }
54+
55+ if (is_object ($ dependencies )) {
56+ $ schemaConstraint = $ this ->factory ->createInstanceFor ('schema ' );
57+ $ schemaConstraint ->check ($ value , $ dependencies , $ path , $ i );
58+ if (!$ schemaConstraint ->isValid ()) {
59+ $ this ->addErrors ($ schemaConstraint ->getErrors ());
4660 }
4761 }
4862 }
0 commit comments