1818
1919namespace CloudCreativity \JsonApi \Validator \Resource ;
2020
21+ use CloudCreativity \JsonApi \Contracts \Validator \KeyedValidatorInterface ;
2122use CloudCreativity \JsonApi \Contracts \Validator \ValidatorInterface ;
2223use CloudCreativity \JsonApi \Error \ErrorObject ;
2324use CloudCreativity \JsonApi \Object \Resource \ResourceObject ;
@@ -100,39 +101,49 @@ abstract public function getTypeValidator();
100101 abstract public function getIdValidator ();
101102
102103 /**
103- * Whether attributes must always be present in the resource object.
104+ * Get the attributes validator or null if the resource object must not have attributes .
104105 *
105- * @return bool
106+ * @return ValidatorInterface
106107 */
107- abstract public function isExpectingAttributes ();
108+ abstract public function getAttributesValidator ();
108109
109110 /**
110- * Get the attributes validator or null if the resource object must not have attributes .
111+ * Get the relationships validator or null if the resource object must not have relationships .
111112 *
112113 * @return ValidatorInterface
113114 */
114- abstract public function getAttributesValidator ();
115+ abstract public function getRelationshipsValidator ();
115116
116117 /**
117- * Whether relationships must always be present in the resource object.
118- *
119118 * @return bool
120119 */
121- abstract public function isExpectingRelationships ();
120+ public function hasIdValidator ()
121+ {
122+ return $ this ->getIdValidator () instanceof ValidatorInterface;
123+ }
122124
123125 /**
124- * Get the relationships validator or null if the resource object must not have relationships .
126+ * Whether the resource object expects to have attributes .
125127 *
126- * @return ValidatorInterface
128+ * @return bool
127129 */
128- abstract public function getRelationshipsValidator ();
130+ public function isExpectingAttributes ()
131+ {
132+ $ attr = $ this ->getAttributesValidator ();
133+
134+ return ($ attr instanceof KeyedValidatorInterface && $ attr ->hasRequiredKeys ());
135+ }
129136
130137 /**
138+ * Whether the resource object expects to have relationships.
139+ *
131140 * @return bool
132141 */
133- public function hasIdValidator ()
142+ public function isExpectingRelationships ()
134143 {
135- return $ this ->getIdValidator () instanceof ValidatorInterface;
144+ $ rel = $ this ->getRelationshipsValidator ();
145+
146+ return ($ rel instanceof KeyedValidatorInterface && $ rel ->hasRequiredKeys ());
136147 }
137148
138149 /**
@@ -213,12 +224,14 @@ protected function validateId(StandardObject $object)
213224 */
214225 protected function validateAttributes (StandardObject $ object )
215226 {
227+ $ expectation = $ this ->isExpectingAttributes ();
228+
216229 // valid if the object does not have attributes, and attributes are not expected.
217- if (!$ object ->has (ResourceObject::ATTRIBUTES ) && !$ this -> isExpectingAttributes () ) {
230+ if (!$ object ->has (ResourceObject::ATTRIBUTES ) && !$ expectation ) {
218231 return $ this ;
219232 }
220233
221- if (!$ object ->has (ResourceObject::ATTRIBUTES ) && $ this -> isExpectingAttributes () ) {
234+ if (!$ object ->has (ResourceObject::ATTRIBUTES ) && $ expectation ) {
222235 $ this ->error (static ::ERROR_MISSING_ATTRIBUTES );
223236 return $ this ;
224237 }
@@ -243,12 +256,14 @@ protected function validateAttributes(StandardObject $object)
243256 */
244257 protected function validateRelationships (StandardObject $ object )
245258 {
259+ $ expectation = $ this ->isExpectingRelationships ();
260+
246261 // valid if no relationships and not expecting relationships
247- if (!$ object ->has (ResourceObject::RELATIONSHIPS ) && !$ this -> isExpectingRelationships () ) {
262+ if (!$ object ->has (ResourceObject::RELATIONSHIPS ) && !$ expectation ) {
248263 return $ this ;
249264 }
250265
251- if (!$ object ->has (ResourceObject::RELATIONSHIPS ) && $ this -> isExpectingRelationships () ) {
266+ if (!$ object ->has (ResourceObject::RELATIONSHIPS ) && $ expectation ) {
252267 $ this ->error (static ::ERROR_MISSING_RELATIONSHIPS );
253268 return $ this ;
254269 }
0 commit comments