Ejemplo n.º 1
0
 /**
  * @param Property $property
  * @param $value
  * @return mixed
  * @throws RequirementValidationException
  */
 public function validate(Property $property, $value)
 {
     if ($value === null) {
         return;
     }
     if ($value === null) {
         return;
     }
     switch ($this->type) {
         case PropertyType::INTEGER:
             $check = is_int($value);
             break;
         case PropertyType::DATETIME:
             $check = $value instanceof \DateTime;
             break;
         case PropertyType::NUMBER:
             $check = is_numeric($value);
             break;
         case PropertyType::STRING:
             $check = is_string($value);
             break;
         case PropertyType::OBJECT:
             $check = is_array($value) || is_object($value);
             break;
         default:
             throw RequirementValidationException::make($property, $this, $value);
             break;
     }
     if (!$check) {
         throw RequirementValidationException::make($property, $this, $value);
     }
 }
Ejemplo n.º 2
0
 /**
  * @param Property $property
  * @param $value
  * @return mixed
  * @throws RequirementValidationException
  */
 public function validate(Property $property, $value)
 {
     if ($value === null) {
         return;
     }
     if (!in_array((string) $value, $this->values)) {
         throw RequirementValidationException::make($property, $this, $value);
     }
 }
Ejemplo n.º 3
0
 /**
  * @param Property $property
  * @param $value
  * @return mixed
  * @throws RequirementValidationException
  */
 public function validate(Property $property, $value)
 {
     if ($value === null) {
         return;
     }
     if (PropertyType::isNumeric($property->getType())) {
         if ($value > $this->length) {
             throw RequirementValidationException::make($property, $this, $value);
         }
     } else {
         if (mb_strlen($value) > $this->length) {
             throw RequirementValidationException::make($property, $this, $value);
         }
     }
 }
Ejemplo n.º 4
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);
     }
 }
Ejemplo n.º 5
0
 /**
  * @param RequirementValidationException $exception
  * @return Message
  */
 public function getErrorMessage(RequirementValidationException $exception) : Message
 {
     $message = sprintf($this->getTemplate(), $exception->getProperty()->getPropertyName());
     return new Message($message, $exception->getRequirement(), $exception->getProperty()->getPropertyName());
 }