/** * @param $property Reflection_Property * @return string[] Possibles properties names */ private function defaultObject(Reflection_Property $property) { $possibles = []; $foreign_class = $this->getForeignClass($property); foreach ($foreign_class->getProperties([T_EXTENDS, T_USE]) as $foreign_property) { $foreign_type = $foreign_property->getType(); if ($foreign_type->isClass() && $foreign_type->isMultiple() && $foreign_type->isInstanceOf($property->getDeclaringClass()) && $foreign_property->getAnnotation('link')->value == Link_Annotation::COLLECTION) { $possibles[$foreign_property->getName()] = $foreign_property; } } return array_keys(Replaces_Annotations::removeReplacedProperties($possibles)); }
/** * 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; }
/** * @return string[] */ protected function getProperties() { // gets all properties from collection element class $class = new Reflection_Class($this->class_name); $properties = $class->getProperties([T_EXTENDS, T_USE]); // remove replaced properties /** @var $properties Reflection_Property[] */ $properties = Replaces_Annotations::removeReplacedProperties($properties); // remove linked class properties $linked_class = $class->getAnnotation('link')->value; if ($linked_class) { foreach (array_keys((new Reflection_Class($linked_class))->getProperties([T_EXTENDS, T_USE])) as $property_name) { unset($properties[$property_name]); } } // remove composite property $property_name = $this->property->getAnnotation('foreign')->value; if (isset($properties[$property_name])) { unset($properties[$property_name]); } // remove static and user-invisible properties foreach ($properties as $property_name => $property) { if ($property->isStatic() || $property->getListAnnotation('user')->has(User_Annotation::INVISIBLE)) { unset($properties[$property_name]); } } // returns properties return $properties; }
/** * Returns object's properties, and their display and value * * @param $template Template * @return Reflection_Property_Value[] */ public function getProperties(Template $template) { $object = reset($template->objects); $properties_filter = $template->getParameter(Parameter::PROPERTIES_FILTER); $class = new Reflection_Class(get_class($object)); $result_properties = []; foreach ($class->accessProperties() as $property_name => $property) { if (!$property->isStatic() && !$property->getListAnnotation('user')->has(User_Annotation::INVISIBLE)) { if (!isset($properties_filter) || in_array($property_name, $properties_filter)) { $property = new Reflection_Property_Value($property->class, $property->name, $object, false, true); $property->final_class = $class->name; $result_properties[$property_name] = $property; } } } return Replaces_Annotations::removeReplacedProperties($result_properties); }