Beispiel #1
0
 /**
  * Get scenario
  * @see ScenariosInterface
  * @param ScenariosInterface|AnnotatedInterface $model
  * @return string Scenario, by default ScenariosInterface::Insert
  */
 public static function getScenario(AnnotatedInterface $model)
 {
     if ($model instanceof ScenariosInterface) {
         return $model->getScenario();
     }
     return ScenariosInterface::Insert;
 }
Beispiel #2
0
 /**
  * Get document root
  * @return AnnotatedInterface Root document
  * @Ignored
  */
 public function getRoot()
 {
     if ($this->_owner instanceof OwneredInterface && $this->_owner !== null) {
         return $this->_owner->getRoot();
     } else {
         return $this;
     }
 }
Beispiel #3
0
 private function getModelCriteria()
 {
     $criteria = null;
     if ($this->model instanceof WithCriteriaInterface) {
         $criteria = $this->model->getDbCriteria();
     } elseif ($this->model instanceof CriteriaAwareInterface) {
         $criteria = $this->model->getCriteria();
     }
     if (empty($criteria)) {
         return new Criteria();
     }
     return $criteria;
 }
Beispiel #4
0
 private function validateEntity($name, $validators)
 {
     $valid = [];
     foreach ($validators as $validatorMeta) {
         // Filter out validators based on scenarios
         if (!empty($validatorMeta->on)) {
             $on = (array) $validatorMeta->on;
             $enabled = false;
             foreach ($on as $scenario) {
                 if ($scenario === ScenarioManager::getScenario($this->model)) {
                     $enabled = true;
                     break;
                 }
             }
             if (!$enabled) {
                 continue;
             }
         }
         if (!empty($validatorMeta->except)) {
             $except = (array) $validatorMeta->except;
             $enabled = true;
             foreach ($except as $scenario) {
                 if ($scenario === ScenarioManager::getScenario($this->model)) {
                     $enabled = false;
                     break;
                 }
             }
             if (!$enabled) {
                 continue;
             }
         }
         // Create validator and validate
         $validator = Factory::create($this->model, $validatorMeta, $name);
         if ($validator->isValid($this->model, $name)) {
             $valid[] = true;
         } else {
             $valid[] = false;
             $this->errors[$name] = array_merge($this->errors[$name], $validator->getErrors());
             // Set errors to model instance if it implements ValidatableInterface
             if ($this->model instanceof ValidatableInterface) {
                 $this->model->setErrors($this->errors);
             }
         }
     }
     return count($valid) === array_sum($valid);
 }