예제 #1
0
파일: Mapper.php 프로젝트: brandonlamb/spot
 /**
  * Run set validation rules on fields
  * @param \Spot\Entity\EntitInterface $entity
  * @return bool
  * @todo A LOT more to do here... More validation, break up into classes with rules, etc.
  */
 public function validate(EntityInterface $entity)
 {
     return true;
     $entityName = $entity->toString();
     $v = new \Valitron\Validator($entity->data());
     // Check validation rules on each feild
     foreach ($this->entityManager->fields($entityName) as $field => $fieldAttrs) {
         // Required field
         if (isset($fieldAttrs['required']) && true === $fieldAttrs['required']) {
             $v->rule('required', $field);
         }
         // Unique field
         if (isset($fieldAttrs['unique']) && true === $fieldAttrs['unique']) {
             if ($this->first($entityName, [$field => $entity->{$field}]) !== false) {
                 $entity->error($field, "" . ucwords(str_replace('_', ' ', $field)) . " '" . $entity->{$field} . "' is already taken.");
             }
         }
         // Valitron validation rules
         if (isset($fieldAttrs['validation']) && is_array($fieldAttrs['validation'])) {
             foreach ($fieldAttrs['validation'] as $rule => $ruleName) {
                 $params = [];
                 if (is_string($rule)) {
                     $params = $ruleName;
                     $ruleName = $rule;
                 }
                 $params = array_merge([$ruleName, $field], $params);
                 call_user_func_array([$v, 'rule'], $params);
             }
         }
     }
     !$v->validate() && $entity->errors($v->errors(), false);
     // Return error result
     return !$entity->hasErrors();
 }