Esempio n. 1
0
 /**
  * return errors information if not valid when trying to save
  * @return NULL
  */
 public function getErrors()
 {
     return ModelValidator::getErrors($this);
 }
Esempio n. 2
0
 public static function validate($object, $object_hash = array())
 {
     $errors = null;
     if (!in_array(spl_object_hash($object), $object_hash)) {
         $object_hash[] = spl_object_hash($object);
     }
     $reflection = new \ReflectionClass($object);
     $classname = $reflection->getName();
     $validations = $object->validations;
     if (!empty($validations)) {
         $unique = $equalWith = array();
         foreach ($validations as $field => $rules) {
             foreach ($rules as $key => $value) {
                 if ($value[0] == "unique") {
                     $unique[] = array($field, "message" => $value['message']);
                     if (count($validations[$field]) == 1) {
                         unset($validations[$field]);
                     } else {
                         unset($validations[$field][$key]);
                     }
                 } else {
                     if ($value[0] == "equalWith") {
                         $equalWith[] = array($field, "message" => $value['message'], "with" => $value['with']);
                         if (count($validations[$field]) == 1) {
                             unset($validations[$field]);
                         } else {
                             unset($validations[$field][$key]);
                         }
                     }
                 }
             }
         }
         $errors = Validator::check(static::convertToArray($object), $validations);
         /** Unique checking */
         foreach ($unique as $key => $value) {
             $result = $classname::getRepository()->findOneBy(array($value[0] => $object->{$value}[0]));
             if (!empty($result)) {
                 $errors[$value[0]][] = $value["message"];
             }
         }
         /** EqualWith checking */
         foreach ($equalWith as $key => $value) {
             if ($object->{$value}[0] != $object->{$value}['with']) {
                 $errors[$value[0]][] = $value["message"];
             }
         }
         $reflection = new \ReflectionClass($object);
         $properties = $reflection->getProperties(\ReflectionProperty::IS_PROTECTED);
         try {
             foreach ($properties as $property) {
                 $property->setAccessible(true);
                 if (ModelAnnotation::match($property, array('ManyToMany', 'OneToMany'))) {
                     $relation = $property->getValue($object);
                     foreach ($relation as $item) {
                         if (!in_array(spl_object_hash($item), $object_hash)) {
                             if (!ModelValidator::isValid($item, $object_hash)) {
                                 $errors[$property->getName()][] = $item->getErrors();
                             }
                         }
                     }
                 } elseif (ModelAnnotation::match($property, array('ManyToOne', 'OneToOne'))) {
                     if ($item = $property->getValue($object)) {
                         if (!in_array(spl_object_hash($item), $object_hash)) {
                             if (!ModelValidator::isValid($item, $object_hash)) {
                                 $errors[$property->getName()][] = $item->getErrors();
                             }
                         }
                     }
                 }
             }
         } catch (\ReflectionException $e) {
             die($e->getTrace() . "-" . $e->getMessage());
             continue;
         }
     }
     ModelValidator::$_errors[spl_object_hash($object)] = $errors;
     return $errors;
 }