/** * @param RelationshipInterface $relationship * @param string|null $key * @return bool */ private function validateEmpty(RelationshipInterface $relationship, $key = null) { if ($relationship->isHasOne()) { $empty = !$relationship->hasIdentifier(); } else { $empty = $relationship->getIdentifiers()->isEmpty(); } if ($empty && !$this->isEmptyAllowed()) { $this->addError($this->errorFactory->relationshipEmptyNotAllowed($key)); return false; } return true; }
/** * @param $key * @param RelationshipsInterface $relationships * @param ResourceInterface $resource * @param object|null $record * @return bool */ protected function validateRelationship($key, RelationshipsInterface $relationships, ResourceInterface $resource, $record = null) { if (!is_object($relationships->get($key))) { $this->addError($this->errorFactory->memberObjectExpected($key, P::relationship($key))); return false; } $validator = $this->get($key); $relationship = $relationships->getRelationship($key); if (!$validator->isValid($relationship, $record, $key, $resource)) { $this->addErrors($validator->getErrors()); return false; } return true; }
/** * @param ResourceInterface $resource * @param object|null $record * @return bool */ protected function validateRelationships(ResourceInterface $resource, $record = null) { $raw = $resource->get(ResourceInterface::RELATIONSHIPS); /** Relationships member must be an object. */ if ($resource->has(ResourceInterface::RELATIONSHIPS) && !is_object($raw)) { $this->addError($this->errorFactory->memberObjectExpected(ResourceInterface::RELATIONSHIPS, P::relationships())); return false; } /** Ok if no relationships validator or one that returns true for `isValid()` */ if (!$this->relationships || $this->relationships->isValid($resource, $record)) { return true; } /** Ensure there is at least one error message. */ if (0 < count($this->relationships->getErrors())) { $this->addErrors($this->relationships->getErrors()); } else { $this->addError($this->errorFactory->resourceInvalidRelationships()); } return false; }