Example #1
0
 /**
  * Run all validations
  *
  * @param integer $failOn Severity level
  *
  * @return boolean
  */
 public function validate($failOn = Validator\Rule::ERROR)
 {
     $this->warnings = [];
     $this->errors = [];
     foreach ($this->rules as $rule) {
         if ($rule instanceof Validator\Condition) {
             // Condition
             $condition = $rule;
             if ($condition->validate()) {
                 // Conditions rules
                 if (!$condition->getValidator()->validate($failOn)) {
                     $this->errors = array_merge($this->errors, $rule->getValidator()->getErrors());
                 }
                 $this->warnings = array_merge($this->warnings, $rule->getValidator()->getWarnings());
             }
         } else {
             // Rule
             if (!$rule->validate()) {
                 if ($rule->getSeverity() <= $failOn) {
                     $this->errors[] = $rule;
                 } else {
                     $this->warnings[] = $rule;
                 }
             }
         }
     }
     // Run nested validators - every entity may have its own validator
     foreach ($this->entity->getData() as $propertyName => $value) {
         if ($value instanceof Entity) {
             $validator = $value->getValidator();
             if (!$validator->validate($failOn)) {
                 foreach ($validator->getErrors() as $error) {
                     $rule = clone $error;
                     $rule->setPath(array_merge([$propertyName], $rule->getPath()));
                     $this->errors[] = $rule;
                 }
             }
             foreach ($validator->getWarnings() as $warning) {
                 $rule = clone $warning;
                 $rule->setPath(array_merge([$propertyName], $rule->getPath()));
                 $this->warnings[] = $rule;
             }
         }
     }
     return count($this->errors) === 0;
 }
Example #2
0
 private function _saveAssociated($primaryValue, Entity $entity)
 {
     foreach ($entity->getChanges() as $name => $associated) {
         Entity\Reflection::load($entity)->getProperty($name)->getOption(Entity\Reflection\Property::OPTION_ASSOC)->saveChanges($primaryValue, $this->connection, $associated);
     }
 }
Example #3
0
 /**
  * Convert entity to simple array
  *
  *  @param Entity $entity
  *
  *  @return array
  */
 public function unmapEntity(Entity $entity)
 {
     $output = [];
     foreach ($entity->getData() as $propertyName => $value) {
         $property = Entity\Reflection::load($entity)->getProperty($propertyName);
         // Skip associations & readonly
         if ($property->hasOption(Entity\Reflection\Property::OPTION_ASSOC) || !$property->isWritable()) {
             continue;
         }
         $output[$property->getName(true)] = $this->unmapValue($property, $value);
     }
     return $output;
 }