/** * Set identifier unique */ public function uniqueIdentifier() { if (isset($this->identifier)) { /** @var $search Identifier */ $search = Search_Object::create(Identifier::class); $search->name = $this->identifier->name; if ($find = Dao::searchOne($search)) { Dao::replace($this->identifier, $find, false); } else { Dao::disconnect($this->identifier); } } }
/** * 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; }
/** * Be sure that recipients are unique into data storage * - they can be common to several emails * - modification of recipients is not allowed */ private function uniqueRecipents() { /** @var $search Recipient */ $search = Search_Object::create(Recipient::class); $recipients = array_merge([$this->from, $this->reply_to, $this->return_path], $this->to, $this->copy_to, $this->blind_copy_to); $already = []; foreach ($recipients as $recipient) { if (isset($recipient)) { $search->email = $recipient->email; $search->name = $recipient->name; if (isset($already[strval($search)])) { Dao::replace($recipient, $already[strval($search)], false); } else { $already[strval($search)] = $recipient; if ($find = Dao::searchOne($recipient)) { Dao::replace($recipient, $find, false); } else { Dao::disconnect($recipient); } } } } }