Example #1
0
File: Entity.php Project: eix/core
 public function update(array $data, $isAtomic = true)
 {
     $validationStatus = array();
     // Check all values before modifying the object.
     foreach ($data as $name => $value) {
         $fieldValidationStatus = $this->validate($name, $value);
         if ($fieldValidationStatus) {
             $validationStatus[$name] = $fieldValidationStatus;
         } elseif (!$isAtomic) {
             // If the field is valid and the operation does not need
             // to be atomic, keep the value.
             $this->{$name} = $value;
         }
     }
     if (empty($validationStatus)) {
         // If the operation is not atomic, the valid values have
         // already been set. Otherwise, all the values can now
         // be safely assigned.
         if ($isAtomic) {
             // The fields' values are valid, the object can be updated.
             foreach ($data as $name => $value) {
                 // Check each value before modifying the object.
                 $this->{$name} = $value;
             }
         }
     } else {
         // If there is something in the validation status, the validation
         // failed. Load the validation status into an exception.
         $exception = new Validators\Exception();
         $exception->setValidationStatus($validationStatus);
         throw $exception;
     }
     // If the entity has an now an ID, register it with its Factory.
     $id = @$data['id'];
     if ($id && !$this->getFactory()->getEntity($id)) {
         $this->getFactory()->registerEntity($this);
     }
 }