/**
  * @param $value
  * @param Property $property
  * @throws PropertyValidationException
  */
 public function validate(Property $property, $value)
 {
     $messages = new MessageCollection();
     foreach ($this as $requirement) {
         /** @var Requirement $requirement */
         try {
             $requirement->validate($property, $value);
         } catch (RequirementValidationException $e) {
             $messages->add($e->getRequirement()->getErrorMessage($e));
         }
     }
     if (count($messages) > 0) {
         throw PropertyValidationException::make($property, $messages);
     }
 }
Ejemplo n.º 2
0
 /**
  * @param string $path
  * @throws PropertyValidationException
  * @throws ResourceException
  * @throws \CatLab\Requirements\Exceptions\ResourceValidationException
  */
 public function validate(string $path)
 {
     $messages = new MessageCollection();
     $field = $this->getField();
     if (!$field instanceof RelationshipField) {
         throw new ResourceException("RelationshipValue found without a RelationshipField.");
     }
     if ($field->canCreateNewChildren()) {
         /*
          * Now check the children
          */
         foreach ($this->getChildrenToProcess() as $child) {
             /** @var RESTResource $child */
             if ($child) {
                 try {
                     $child->validate($this->appendToPath($path, $field));
                 } catch (ResourceValidationException $e) {
                     $messages->merge($e->getMessages());
                 }
             } else {
                 $this->getField()->validate(null, $this->appendToPath($path, $field));
             }
         }
     } elseif ($field->canLinkExistingEntities()) {
         /*
          *  We only need identifiers...
          */
         $identifiers = $field->getChildResource()->getFields()->getIdentifiers();
         foreach ($this->getChildrenToProcess() as $child) {
             if ($child) {
                 /** @var RESTResource $child */
                 foreach ($identifiers as $identifier) {
                     /** @var IdentifierField $identifier */
                     $prop = $child->getProperties()->getProperty($identifier);
                     if (!$prop || $prop->getValue() === null) {
                         $identifier->setPath($this->appendToPath($path, $field));
                         $propertyException = RequirementValidationException::make($identifier, new Exists(), null);
                         $messages = new MessageCollection();
                         $messages->add($propertyException->getRequirement()->getErrorMessage($propertyException));
                         throw PropertyValidationException::make($identifier, $messages);
                     }
                 }
             } else {
                 try {
                     $this->getField()->validate(null, $this->appendToPath($path, $field));
                 } catch (ResourceValidationException $e) {
                     $messages->merge($e->getMessages());
                 }
             }
         }
     }
     if (count($messages) > 0) {
         throw PropertyValidationException::make($field, $messages);
     }
 }