public function testIfWillSetDeleteScenarioOnDeletedObject()
 {
     $model = new BaseAttributesScenario();
     (new EntityManager($model))->save();
     (new EntityManager($model))->delete();
     $this->assertSame(ScenarioManager::getScenario($model), ScenariosInterface::Delete);
 }
 public function testIfWillSetDeleteScenarioOnDeletedObject()
 {
     $model = new DocumentBaseAttributes();
     $model->save();
     $model->delete();
     $this->assertSame(ScenarioManager::getScenario($model), ScenariosInterface::Delete);
 }
 public function testIfWillFailValidationExceptInsertAndRegister()
 {
     $model = new ModelWithValidationExceptInsertAndRegister();
     $validator = new Validator($model);
     $valid = $validator->validate();
     $this->assertTrue($valid);
     ScenarioManager::setScenario($model, 'register');
     $valid2 = $validator->validate();
     $this->assertTrue($valid2);
     ScenarioManager::setScenario($model, 'update');
     $valid3 = $validator->validate();
     $this->assertFalse($valid3);
 }
Beispiel #4
0
 /**
  * Restore trashed item
  * @return boolean
  * @throws Exception
  * @Ignored
  */
 public function restore()
 {
     if (!$this instanceof TrashInterface) {
         // When trying to restore normal document instead of trash item
         throw new Exception(sprintf('Restore can be performed only on `%s` instance', TrashInterface::class));
     }
     $em = new EntityManager($this->data);
     // Set scenario to `restore` for model, which is just about to be restored
     ScenarioManager::setScenario($this->data, TrashInterface::ScenarioRestore);
     if (!Event::valid($this->data, TrashInterface::EventBeforeRestore)) {
         return false;
     }
     $saved = $em->save();
     if (!$saved) {
         return false;
     }
     $finder = new Finder($this->data);
     $model = $finder->find(PkManager::prepareFromModel($this->data));
     if (!$model) {
         return false;
     }
     $eventAfter = new RestoreEvent();
     $eventAfter->setTrashed($this->data);
     $eventAfter->setTrash($this);
     if (!Event::valid($model, TrashInterface::EventAfterRestore, $eventAfter)) {
         return false;
     }
     $trashEm = new EntityManager($this);
     $this->data = null;
     // Use deleteOne, to avoid beforeDelete event,
     // which should be raised only when really removing document:
     // when emtying trash
     return $trashEm->deleteOne(PkManager::prepareFromModel($this));
 }
Beispiel #5
0
 /**
  * Validates the attribute of the object.
  * If there is any error, the error message is added to the object.
  * @param AnnotatedInterface $model the object being validated
  * @param string $attribute the attribute being validated
  */
 public function isValid(AnnotatedInterface $model, $attribute)
 {
     $value = $model->{$attribute};
     if ($this->allowEmpty && empty($value)) {
         return true;
     }
     $className = empty($this->className) ? get_class($model) : $this->className;
     $compareModel = new $className();
     $criteria = (new Criteria())->decorateWith($compareModel);
     $criteria->addCond($attribute, '==', $value);
     if ($this->criteria !== []) {
         $criteria->mergeWith($this->criteria);
     }
     ScenarioManager::setScenario($compareModel, ValidatorInterface::ScenarioValidate);
     $finder = new Finder($compareModel);
     $found = $finder->find($criteria);
     // Not found entirely
     if (null === $found) {
         return true;
     }
     // Same pk
     if (PkManager::compare($found, $model)) {
         return true;
     }
     $label = ManganMeta::create($model)->field($attribute)->label;
     $this->addError('msgTaken', ['{attribute}' => $label, '{value}' => $value]);
     return false;
 }
Beispiel #6
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);
 }
Beispiel #7
0
 /**
  * This method is invoked before deleting a record.
  * The default implementation raises the {@link onBeforeDelete} event.
  * You may override this method to do any preparation work for record deletion.
  * Make sure you call the parent implementation so that the event is raised properly.
  * @return boolean whether the record should be deleted. Defaults to true.
  * @since v1.0
  */
 private function _beforeDelete()
 {
     $result = Event::valid($this->model, EntityManagerInterface::EventBeforeDelete);
     if ($result) {
         (new Signal())->emit(new BeforeDelete($this->model));
         ScenarioManager::setScenario($this->model, ScenariosInterface::Delete);
     }
     return $result;
 }
Beispiel #8
0
 /**
  * Creates an model with the given attributes.
  * This method is internally used by the find methods.
  * @param mixed[] $data attribute values (column name=>column value)
  * @return AnnotatedInterface|null the newly created document. The class of the object is the same as the model class.
  * Null is returned if the input data is false.
  * @since v1.0
  */
 protected function populateRecord($data)
 {
     if ($data !== null) {
         if (!empty($data['$err'])) {
             throw new ManganException(sprintf("There is an error in query: %s", $data['$err']));
         }
         $model = RawArray::toModel($data, $this->model);
         ScenarioManager::setScenario($model, ScenariosInterface::Update);
         Event::trigger($model, FinderInterface::EventAfterFind);
         return $model;
     } else {
         return null;
     }
 }