/** * Create a clone of the object, using a built class if needed * * @param $object object * @param $class_name string if set, the new object will use the matching built class * this class name must inherit from the object's class * @param $properties_values array some properties values for the cloned object * @param $same_identifier boolean * @return object */ public static function createClone($object, $class_name = null, $properties_values = [], $same_identifier = true) { $class_name = self::className($class_name); $source_class_name = get_class($object); if (!isset($class_name)) { $class_name = self::className($source_class_name); } if ($class_name !== $source_class_name) { // initialises cloned object $clone = self::create($class_name); $destination_class = new Link_Class($class_name); // deactivate AOP if (isset($clone->_)) { $save_aop = $clone->_; unset($clone->_); } // copy official properties values from the source object $properties = (new Reflection_Class($source_class_name))->accessProperties(); foreach ($properties as $property) { if (!isset($save_aop[$property->name])) { $property->setValue($clone, $property->getValue($object)); } } // copy unofficial properties values from the source object (ie AOP properties aliases) // clone collection objects using the destination collection property type $clone_collection = []; foreach (get_object_vars($object) as $property_name => $value) { if ($property_name !== '_' && !isset($properties[$property_name])) { $clone->{$property_name} = $value; if (isset($properties[rtrim($property_name, '_')])) { $property = $properties[rtrim($property_name, '_')]; if ($property->getAnnotation('link') == Link_Annotation::COLLECTION) { $element_class_from = $property->getType()->getElementTypeAsString(); $property = $destination_class->getProperty($property->name); $element_class_to = $property->getType()->getElementTypeAsString(); if ($element_class_to != $element_class_from) { $clone_collection[substr($property_name, 0, -1)] = $element_class_to; } } } } } // reactivate AOP if (isset($save_aop)) { $clone->_ = $save_aop; } foreach ($clone_collection as $property_name => $element_class_to) { $elements = []; foreach ($object->{$property_name} as $key => $element) { $elements[$key] = Builder::createClone($element, $element_class_to, [], $same_identifier); } $clone->{$property_name} = $elements; } // linked class object to link class object : store source object to linked object $destination_class = new Link_Class($class_name); if ($linked_class_name = $destination_class->getLinkedClassName()) { if ($linked_class_name == $source_class_name) { $destination_class->getLinkProperty()->setValue($clone, $object); } } // identify destination object = source object, or disconnect destination object if ($same_identifier) { Dao::replace($clone, $object, false); } else { Dao::disconnect($clone); } } else { $clone = clone $object; } // copy added properties values to the cloned object if ($properties_values) { $properties = (new Reflection_Class($class_name))->accessProperties(); foreach ($properties_values as $property_name => $value) { $properties[$property_name]->setValue($clone, $value); } } return $clone; }
/** * Write an object into data source * * If object was originally read from data source, corresponding data will be overwritten * If object was not originally read from data source nor linked to it using replace(), a new * record will be written into data source using this object's data. * If object is null (all properties null or unset), the object will be removed from data source * * TODO LOWEST factorize this to become SOLID * * @param $object object object to write into data source * @param $options Option[] some options for advanced write * @return object the written object if written, or null if the object could not be written */ public function write($object, $options = []) { if ($this->beforeWrite($object, $options)) { if (Null_Object::isNull($object)) { $this->disconnect($object); } $class = new Link_Class(get_class($object)); $id_property = 'id'; foreach ($options as $option) { if ($option instanceof Only) { $only = isset($only) ? array_merge($only, $option->properties) : $option->properties; } } do { /** @var $link Class_\Link_Annotation */ $link = $class->getAnnotation('link'); if ($link->value) { $link_property = $link->getLinkClass()->getLinkProperty(); $link_object = $link_property->getValue($object); if (!$link_object) { $id_link_property = 'id_' . $link_property->name; $object->{$id_link_property} = $this->write($link_object, $options); } } $table_columns_names = array_keys($this->getStoredProperties($class)); $write_collections = []; $write_maps = []; $write = []; $aop_getter_ignore = Getter::$ignore; Getter::$ignore = true; $exclude_properties = $link->value ? array_keys((new Reflection_Class($link->value))->getProperties([T_EXTENDS, T_USE])) : []; /** @var $properties Reflection_Property[] */ $properties = $class->accessProperties(); $properties = Replaces_Annotations::removeReplacedProperties($properties); foreach ($properties as $property) { if (!isset($only) || in_array($property->name, $only)) { if (!$property->isStatic() && !in_array($property->name, $exclude_properties)) { $value = isset($object->{$property}) ? $property->getValue($object) : null; $property_is_null = $property->getAnnotation('null')->value; if (is_null($value) && !$property_is_null) { $value = ''; } if (in_array($property->name, $table_columns_names)) { $element_type = $property->getType()->getElementType(); // write basic if ($element_type->isBasic()) { $write[$property->getAnnotation('storage')->value] = is_array($value) ? json_encode($value) : $value; } elseif ($property->getAnnotation('store')->value == 'string') { $write[$property->getAnnotation('storage')->value] = is_array($value) ? serialize($value) : strval($value); } else { $column_name = 'id_' . $property->name; if (is_object($value)) { $value_class = new Link_Class(get_class($value)); $id_value = $value_class->getLinkedClassName() && !$element_type->asReflectionClass()->getAnnotation('link')->value ? 'id_' . $value_class->getCompositeProperty()->name : 'id'; $object->{$column_name} = $this->getObjectIdentifier($value, $id_value); if (empty($object->{$column_name})) { $object->{$column_name} = $this->getObjectIdentifier($this->write($value), $id_value); } } $write['id_' . $property->getAnnotation('storage')->value] = $property_is_null && !isset($object->{$column_name}) ? null : intval($object->{$column_name}); } } elseif (is_array($value) && $property->getAnnotation('link')->value == Link_Annotation::COLLECTION) { $write_collections[] = [$property, $value]; } elseif (is_array($value) && $property->getAnnotation('link')->value == Link_Annotation::MAP) { foreach ($value as $key => $val) { if (!is_object($val)) { $val = Dao::read($val, $property->getType()->getElementTypeAsString()); if (isset($val)) { $value[$key] = $val; } else { unset($value[$key]); } } } $write_maps[] = [$property, $value]; } } } } Getter::$ignore = $aop_getter_ignore; if ($write) { // link class : id is the couple of composite properties values if ($link->value) { $search = []; foreach ($link->getLinkProperties() as $property) { $property_name = $property->getName(); $column_name = $property->getType()->isClass() ? 'id_' : ''; $column_name .= $properties[$property_name]->getAnnotation('storage')->value; if (isset($write[$column_name])) { $search[$property_name] = $write[$column_name]; } elseif (isset($write[$property_name])) { $search[$property_name] = $write[$column_name]; } else { trigger_error("Can't search {$property_name}", E_USER_ERROR); } } if ($this->search($search, $class->name)) { $id = []; foreach ($search as $property_name => $value) { $column_name = $properties[$property_name]->getAnnotation('storage')->value; if (isset($write['id_' . $column_name])) { $column_name = 'id_' . $column_name; } $id[$column_name] = $value; unset($write[$column_name]); } } else { $id = null; } } else { $id = $this->getObjectIdentifier($object, $id_property); } if ($write) { $this->setContext($class->name); if (empty($id)) { $this->disconnect($object); $id = $this->query(Sql\Builder::buildInsert($class->name, $write)); if (!empty($id)) { $this->setObjectIdentifier($object, $id); } } else { $this->query(Sql\Builder::buildUpdate($class->name, $write, $id)); } } } foreach ($write_collections as $write) { list($property, $value) = $write; $this->writeCollection($object, $property, $value); } foreach ($write_maps as $write) { list($property, $value) = $write; $this->writeMap($object, $property, $value); } // if link class : write linked object too $id_property = $link->value ? 'id_' . $class->getCompositeProperty()->name : null; $class = $link->value ? new Link_Class($link->value) : null; } while ($class && !Null_Object::isNull($object, $class->name)); /** @var $after_writes Method_Annotation[] */ $after_writes = (new Reflection_Class(get_class($object)))->getAnnotations('after_write'); foreach ($after_writes as $after_write) { if ($after_write->call($object, [$this, $options]) === false) { break; } } return $object; } return null; }