/** * @inheritdoc */ public function resourceInvalidAttributesMessages(MessageBag $messageBag, $attributePrefix = null, $statusCode = self::STATUS_INVALID_ATTRIBUTES) { $prototype = $this->repository->error(self::RESOURCE_INVALID_ATTRIBUTES_MESSAGES); $prototype = Error::cast($prototype)->setStatus($statusCode); $prefix = $attributePrefix ? P::attribute($attributePrefix) : P::attributes(); $errors = new ErrorBag($messageBag, $prototype, $prefix); return $errors->toArray(); }
/** * @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; }
/** * @inheritdoc */ public function relationshipNotAcceptable(ResourceIdentifierInterface $identifier, $relationshipKey = null, $error = null) { $base = $this->repository->errorWithPointer(self::RELATIONSHIP_NOT_ACCEPTABLE, $relationshipKey ? P::relationship($relationshipKey) : P::data(), ['type' => $identifier->getType(), 'id' => $identifier->getId()]); $errors = new MutableErrorCollection(); /** @var MutableErrorInterface $err */ foreach (MutableErrorCollection::cast($error ?: $base) as $err) { $add = clone $base; $errors->add($add->merge($err)); } return $errors; }
/** * @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; }
/** * @param ResourceIdentifierInterface $identifier * @param string|null $key * @return bool */ protected function validateIdentifier(ResourceIdentifierInterface $identifier, $key = null) { $valid = true; /** Must have a type */ if (!$identifier->hasType()) { $this->addError($this->errorFactory->memberRequired(ResourceIdentifierInterface::TYPE, $key ? P::relationshipData($key) : P::data())); $valid = false; } elseif (!$this->isSupportedType($identifier->getType())) { $this->addError($this->errorFactory->relationshipUnsupportedType($this->expectedTypes, $identifier->getType(), $key)); $valid = false; } /** Must have an id */ if (!$identifier->hasId()) { $this->addError($this->errorFactory->memberRequired(ResourceIdentifierInterface::ID, $key ? P::relationshipId($key) : P::data())); $valid = false; } return $valid; }