Exemplo n.º 1
0
/**
 * Return true if all the values are the same
 *
 * @return boolean
 */
function is_eql()
{
    $values = func_get_args();
    if (count($values) == 1) {
        return true;
    } elseif (count($values) == 2) {
        $v1 = $values[0];
        $v2 = $values[1];
        if ($v1 instanceof KObject && $v1->inherits('AnDomainEntityAbstract')) {
            return $v1->eql($v2);
        }
        if ($v1 instanceof AnDomainAttributeInterface) {
            return $v1 == $v2;
        } else {
            return $v1 === $v2;
        }
    } else {
        for ($i = 0; $i < count($values) - 2; $i++) {
            for ($j = 1; $j < count($values) - 1; $j++) {
                if (is_eql($values[$i], $values[$j]) === false) {
                    return false;
                }
            }
        }
        return true;
    }
}
Exemplo n.º 2
0
 /**
  * Set the raw value of a property
  * 
  * @param string $name 
  * @param mixed $value 
  * @return void;
  */
 public function set($name, $value = null)
 {
     $property = $this->getEntityDescription()->getProperty($name);
     if (!$property instanceof AnDomainPropertyAbstract) {
         return parent::set($name, $value);
     }
     //if a value is mixin then get its mixer
     if ($value instanceof KMixinAbstract) {
         $value = $value->getMixer();
     }
     $modify = false;
     $name = $property->getName();
     $context = $this->getRepository()->getCommandContext();
     $context['property'] = $property;
     $context['value'] = $value;
     $context['entity'] = $this;
     if ($this->getRepository()->getCommandChain()->run('before.setdata', $context) === false) {
         return $this;
     }
     $value = $context->value;
     if ($property->isSerializable()) {
         $modify = true;
         if ($property->isRelationship() && $property->isManyToOne() && $value) {
             if (!is($value, 'AnDomainEntityAbstract', 'AnDomainEntityProxy')) {
                 throw new AnDomainExceptionType('Value of ' . $property->getName() . ' must be a AnDomainEntityAbstract');
             }
             //if a relationship is a belongs to then make sure the parent
             //is always saved before child
             //example if $topic->author = new author
             //then save the new author first before saving topic
             //only set the dependency
             if ($value->getEntityState() == AnDomain::STATE_NEW) {
                 //save the child before saving the parent
                 $this->getRepository()->getSpace()->setSaveOrder($value, $this);
             }
         }
         //if value is not null do a composite type checking
         if (!is_null($value)) {
             if ($property->isAttribute() && !$property->isScalar()) {
                 if (!is($value, $property->getType())) {
                     throw new AnDomainEntityException('Value of ' . $property->getName() . ' must be a ' . $property->getType() . '. a ' . get_class($value) . ' is given.');
                 }
             }
         }
     } elseif ($property->isRelationship() && $property->isOnetoOne()) {
         $child = $property->getChildProperty();
         $current = $this->get($property->getName());
         //if there's a current value and it's existence depends on
         //the parent entity then remove the current
         if ($current && $child->isRequired()) {
             $current->delete();
         }
         //if a one-to-one relationship then there must be a child key for the property
         //then must set the inverse
         if ($value) {
             $value->set($child->getName(), $this);
         }
         $this->_data[$property->getName()] = $value;
     } elseif ($property->isRelationship() && ($property->isManyToMany() || $property->isOneToMany())) {
         $current = $this->get($name);
         if ($current instanceof AnDomainDecoratorOnetomany) {
             $values = KConfig::unbox($value);
             //can be an KObjectArray or KObjectSet object
             if ($values instanceof KObject && $values instanceof Iterator) {
                 $current->delete();
                 foreach ($values as $value) {
                     $current->insert($value);
                 }
             }
         }
     }
     //only modify if the current value is differnet than the new value
     $modify = $modify && !is_eql($this->get($name), $value);
     if ($modify) {
         //lets bring them back to their orignal type
         if (!is_null($value) && $property->isAttribute() && $property->isScalar()) {
             settype($value, $property->getType());
         }
         if ($this->getEntityState() != AnDomain::STATE_NEW) {
             //store the original value for future checking
             if (!isset($this->_modified[$name])) {
                 $this->_modified[$name] = array('old' => $this->get($name));
             }
             $this->_modified[$name]['new'] = $value;
             //check if the new value is the same as the old one then remove the
             if (is_eql($this->_modified[$name]['old'], $this->_modified[$name]['new'])) {
                 //if there are no modified then reset the entity
                 unset($this->_modified[$name]);
                 if (count($this->_modified) === 0) {
                     $this->reset();
                 }
             }
         }
         $this->_data[$property->getName()] = $value;
         $this->getRepository()->getSpace()->setEntityState($this, AnDomain::STATE_MODIFIED);
         //only track modifications for the updated entities
         if ($this->getEntityState() !== AnDomain::STATE_MODIFIED) {
             $this->_modified = array();
         }
     }
     return $this;
 }