Пример #1
0
 /**
  * Returns true if the property value into the object is validated into the object context
  *
  * @param $object object
  * @return boolean
  */
 public function validate($object)
 {
     $validated = true;
     foreach ($this->property->getAnnotations() as $annotation_name => $annotation) {
         if ($annotation instanceof Template\Property_Validator) {
             /** @var $annotation Template\Property_Validator|Property_Validate_Annotation */
             $validated_annotation = $annotation->validate($object);
             if ($annotation->valid === true) {
                 $annotation->valid = Validate::INFORMATION;
             }
             if ($annotation->valid === false) {
                 $annotation->valid = Validate::ERROR;
             }
             if (is_null($annotation->valid)) {
                 return null;
             } else {
                 if (!$validated_annotation) {
                     $this->report[] = $annotation;
                 }
                 $validated = $validated && $validated_annotation;
             }
         }
     }
     return $validated;
 }
Пример #2
0
 /**
  * Property select controller, starting from a given root class
  *
  * @param $parameters Parameters
  * - first : the root reference class name (ie a business object)
  * - second : if set, the selected property path into the root reference class name
  * @param $form array  not used
  * @param $files array not used
  * @return mixed
  */
 public function run(Parameters $parameters, $form, $files)
 {
     $class_name = Set::elementClassNameOf($parameters->shiftUnnamed());
     $property_path = $parameters->shiftUnnamed();
     if (empty($property_path)) {
         $top_property = new Property();
         $top_property->class = $class_name;
         $properties = $this->getProperties(new Reflection_Class($class_name));
         foreach ($properties as $property) {
             $property->path = $property->name;
         }
     } else {
         $top_property = new Reflection_Property($class_name, $property_path);
         $properties = $this->getProperties(new Reflection_Class($top_property->getType()->getElementTypeAsString()), $top_property->final_class);
         foreach ($properties as $property) {
             $property->path = $property_path . DOT . $property->name;
         }
         if (!$parameters->getRawParameter(Parameter::CONTAINER)) {
             $parameters->set(Parameter::CONTAINER, 'subtree');
         }
     }
     $objects = $parameters->getObjects();
     array_unshift($objects, $top_property);
     $objects['properties'] = $properties;
     $objects['class_name'] = $class_name;
     $objects['display_full_path'] = false;
     /**
      * Objects for the view :
      * first        Property the property object (with selected property name, or not)
      * 'properties' Reflection_Property[] all properties from the reference class
      */
     return View::run($objects, $form, $files, Property::class, 'select');
 }
Пример #3
0
 /**
  * @param $property Reflection_Property
  * @param $value    mixed
  * @return Line[]
  */
 private static function checkProperty(Reflection_Property $property, $value)
 {
     $report_lines = [];
     if (is_null($value)) {
         if (!$property->getAnnotation('null')) {
             self::checkValue($report_lines, false, $property, new Null_Annotation(false, $property), $value);
         }
     } else {
         foreach ($property->getAnnotations() as $annotation_name => $annotation) {
             switch ($annotation_name) {
                 case 'min_length':
                     self::checkValue($report_lines, strlen($value) >= $annotation->value, $property, $annotation, $value);
                     break;
                 case 'max_length':
                     self::checkValue($report_lines, strlen($value) <= $annotation->value, $property, $annotation, $value);
                     break;
                 case 'min_value':
                     self::checkValue($report_lines, $value >= $annotation->value, $property, $annotation, $value);
                     break;
                 case 'max_value':
                     self::checkValue($report_lines, $value <= $annotation->value, $property, $annotation, $value);
                     break;
                     //case 'precision':
                     //case 'signed':
                 //case 'precision':
                 //case 'signed':
                 case 'var':
                     self::checkVar($report_lines, $property, $annotation, $value);
                     break;
             }
         }
     }
     return $report_lines;
 }
Пример #4
0
 /**
  * @return string
  */
 function table()
 {
     if (!isset($this->table)) {
         $master_table = Dao::storeNameOf($this->property->class);
         $foreign_table = Dao::storeNameOf($this->property->getType()->getElementTypeAsString());
         $this->table = $master_table < $foreign_table ? $master_table . '_' . $foreign_table : $foreign_table . '_' . $master_table;
     }
     return $this->table;
 }
Пример #5
0
 /**
  * @param $object         object
  * @param $foreign_object object
  * @return string
  */
 public function buildQuery($object, $foreign_object)
 {
     list($table, $field1, $field2, $id1, $id2) = Map::sqlElementsOf($object, $this->property, $foreign_object);
     if ($this->property->getType()->getElementTypeAsString() == 'object') {
         $class_field = substr($field2, 3) . '_class';
         return 'INSERT INTO' . SP . BQ . $table . BQ . LF . 'SET ' . $field1 . ' = ' . $id1 . ', ' . $field2 . ' = ' . $id2 . ', ' . $class_field . ' = ' . Value::escape(get_class($foreign_object));
     } else {
         return 'INSERT INTO' . SP . BQ . $table . BQ . LF . 'SET ' . $field1 . ' = ' . $id1 . ', ' . $field2 . ' = ' . $id2;
     }
 }
Пример #6
0
 /**
  * @return string
  */
 public function build()
 {
     $properties_name = [];
     foreach ($this->property->getFinalClass()->getProperties([T_EXTENDS, T_USE]) as $property) {
         if (!$property->isStatic()) {
             $properties_name[$property->name] = $property->name;
         }
     }
     return new Select($this->name, $properties_name, $this->property->name);
 }
Пример #7
0
 /**
  * @param $property Reflection_Property
  * @param $map      object[]
  */
 public function __construct(Reflection_Property $property, $map)
 {
     $this->property = $property;
     $this->map = $map;
     $this->class_name = $this->property->getType()->getElementTypeAsString();
     $class = new Reflection_Class($this->class_name);
     /** @var $representative Representative_Annotation */
     $representative = $class->getListAnnotation('representative');
     $this->properties = $representative->getProperties();
 }
Пример #8
0
 /**
  * Get current environment name possible values
  *
  * @return mixed[]
  */
 public function values()
 {
     $type = $this->property->getType();
     if ($type->isClass()) {
         return Dao::readAll($this->property->getType()->asString(), [Dao::sort()]);
     } elseif ($values = $this->property->getListAnnotation('values')->values()) {
         return array_combine($values, $values);
     } else {
         trigger_error("Unable to get {$this->property->name} environment values from type " . $type->asString(), E_USER_ERROR);
         return null;
     }
 }
Пример #9
0
 /**
  * @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;
 }
Пример #10
0
 /**
  * @param $object   object
  * @param $property Reflection_Property
  * @return Standard_Cell
  */
 protected function buildCell($object, Reflection_Property $property)
 {
     if (!isset($this->template)) {
         $this->template = new Html_Template();
     }
     $value = $property->getValue($object);
     $preprop = $this->preprop ? $this->preprop . '[' . $this->property->name . ']' : $this->property->name;
     $builder = new Html_Builder_Property($property, $value, $preprop . '[]');
     $input = $builder->setTemplate($this->template)->build();
     if ($property->name == reset($this->properties)->name && !(new Reflection_Class($this->class_name))->getAnnotation('link')->value) {
         $property_builder = new Html_Builder_Property();
         $property_builder->setTemplate($this->template);
         $id_input = new Input($preprop . '[id][' . $property_builder->template->nextCounter($preprop . '[id][]') . ']', isset($object->id) ? $object->id : null);
         $id_input->setAttribute('type', 'hidden');
         $input = $id_input . $input;
     }
     return new Standard_Cell($input);
 }
Пример #11
0
 /**
  * Constructs a reflection property with value
  *
  * @example
  * $pv = new Reflection_Property_Value('Class_Name', 'property_name', $object);
  *
  * @param $class_name    string
  * @param $property_name string
  * @param $object        object|mixed the object containing the value, or the value itself
  *        (in this case set $final_value tu true)
  * @param $final_value   boolean set to true if $object is a final value instead of the object
  *        containing the valued property
  * @param $user          boolean set to true if the property value will be used into an user
  *        display (ie an HTML template)
  */
 public function __construct($class_name, $property_name, $object = null, $final_value = false, $user = false)
 {
     parent::__construct($class_name, $property_name);
     $this->final_value = $final_value;
     $this->path = $property_name;
     if (!isset($this->object)) {
         $this->object = $object;
     } else {
         echo 'DEAD CODE ? object is set for ' . $class_name . '::' . $property_name . BR;
     }
     $this->user = $user;
 }
Пример #12
0
 /**
  * This cleanup method is called after loading and getting the current value
  * in order to avoid crashes when some components of the setting disappeared in the meantime.
  *
  * @return integer number of changes made during cleanup : if 0, then cleanup was not necessary
  */
 public function cleanup()
 {
     $changes_count = 0;
     $class_name = $this->getClassName();
     foreach ($this->classes as $key => $class) {
         if ($class->property_path && !Reflection_Property::exists($class_name, join(DOT, $class->property_path))) {
             unset($this->classes[$key]);
             $changes_count++;
         } else {
             $changes_count += $class->cleanup();
         }
     }
     return $changes_count;
 }
Пример #13
0
 /**
  * @param $value mixed
  * @return string
  */
 public function formatValue($value)
 {
     $type = $this->property->getType();
     if ($type->isDateTime()) {
         return $this->formatDateTime($value);
     } else {
         switch ($type) {
             case Type::BOOLEAN:
                 return $this->formatBoolean($value);
             case Type::FLOAT:
                 return $this->formatFloat($value);
             case Type::INTEGER:
                 return $this->formatInteger($value);
             case Type::STRING:
                 return $this->formatString($value);
         }
         return $this->formatDefault($value);
     }
 }
Пример #14
0
 /**
  * @param $multiline boolean keep this value empty, it is not used because the @multiline
  *        annotation is automatically used
  * @param $values    string[] keep this value empty, it is not used because the @values annotation
  *        is automatically used
  * @return Element
  */
 protected function buildString($multiline = false, $values = null)
 {
     $values_captions = [];
     $values = $this->property->getListAnnotation('values')->values();
     foreach ($values as $value) {
         $values_captions[$value] = Names::propertyToDisplay($value);
     }
     if ($values_captions && !in_array($this->value, $values_captions)) {
         $values_captions[$this->value] = $this->value;
     }
     $element = parent::buildString($this->property->getAnnotation('multiline')->value, $values_captions);
     if ($this->property->getAnnotation('mandatory')->value) {
         $element->setAttribute('required', true);
     }
     if ($this->property->getAnnotation('password')->value) {
         $element->setAttribute('type', 'password');
         $element->setAttribute('value', strlen($this->value) ? Password::UNCHANGED : '');
     }
     return $element;
 }
Пример #15
0
 /**
  * @param $class_name              string
  * @param $property_path           string
  * @param $use_reverse_translation boolean if true, will try reverse translation of property names
  * @param $properties_alias        string[] $property_path = string[string $property_alias]
  * @return string
  */
 public static function propertyPathOf($class_name, $property_path, $use_reverse_translation = false, $properties_alias = null)
 {
     if (isset($properties_alias) && isset($properties_alias[$property_path])) {
         $property_path = $properties_alias[$property_path];
     } elseif ($use_reverse_translation) {
         $property_class_name = $class_name;
         $property_names = [];
         foreach (explode(DOT, $property_path) as $property_name) {
             if ($asterisk = substr($property_name, -1) == '*') {
                 $property_name = substr($property_name, 0, -1);
             }
             $property = null;
             $property_name = Names::displayToProperty($property_name);
             try {
                 $property = new Reflection_Property($property_class_name, $property_name);
             } catch (ReflectionException $e) {
                 $translated_property_name = Names::displayToProperty(Loc::rtr($property_name, $property_class_name));
                 try {
                     $property = new Reflection_Property($property_class_name, $translated_property_name);
                     $property_name = $translated_property_name;
                 } catch (ReflectionException $e) {
                 }
             }
             $property_names[] = $property_name . ($asterisk ? '*' : '');
             if (!isset($property)) {
                 break;
             }
             $property_class_name = $property->getType()->getElementTypeAsString();
         }
         $property_path = join(DOT, $property_names);
     }
     return $property_path;
 }
Пример #16
0
 /**
  * Cleanup outdated properties from the list setting
  *
  * @return integer number of changes made during cleanup : if 0, then cleanup was not necessary
  */
 public function cleanup()
 {
     $changes_count = 0;
     // properties path
     foreach ($this->properties_path as $key => $property_path) {
         if (!Reflection_Property::exists($this->class_name, $property_path)) {
             unset($this->properties_path[$key]);
             $changes_count++;
         }
     }
     if ($changes_count) {
         $this->properties_path = array_values($this->properties_path);
     }
     // search
     foreach (array_keys($this->search) as $property_path) {
         if (!Reflection_Property::exists($this->class_name, $property_path)) {
             unset($this->search[$property_path]);
             $changes_count++;
         }
     }
     // sort
     if ($this->sort) {
         foreach ($this->sort->columns as $key => $property_path) {
             if (!Reflection_Property::exists($this->class_name, $property_path)) {
                 unset($this->sort->columns[$key]);
                 $changes_count++;
             }
         }
     }
     return $changes_count;
 }
Пример #17
0
 /**
  * @param $description     string
  * @param $class_name      string
  * @param $property_name   string
  * @param $annotation_name string
  * @param $assumed_value   mixed
  */
 private function testAnnotation($description, $class_name, $property_name, $annotation_name, $assumed_value)
 {
     $property = new Reflection_Property($class_name, $property_name);
     $annotation = $property->getAnnotation($annotation_name);
     $this->assume($class_name . DOT . $property_name . AT . $annotation_name . SP . DQ . $assumed_value . DQ . SP . '(' . $description . ')', $annotation->value, $assumed_value);
 }
Пример #18
0
 /**
  * @param $object   object
  * @param $property Reflection_Property
  * @param $map      object[]
  */
 private function writeMap($object, Reflection_Property $property, $map)
 {
     // old map
     $class = new Link_Class(get_class($object));
     $composite_property_name = $class->getAnnotation('link')->value ? $class->getCompositeProperty()->name : null;
     $old_object = Search_Object::create(Link_Class::linkedClassNameOf($object));
     $this->setObjectIdentifier($old_object, $this->getObjectIdentifier($object, $composite_property_name));
     $aop_getter_ignore = Getter::$ignore;
     Getter::$ignore = false;
     $old_map = $property->getValue($old_object);
     Getter::$ignore = $aop_getter_ignore;
     // map properties : write each of them
     $insert_builder = new Map_Insert($property);
     $id_set = [];
     foreach ($map as $element) {
         $id = $this->getObjectIdentifier($element) ?: $this->getObjectIdentifier($this->write($element));
         if (!isset($old_map[$id])) {
             $query = $insert_builder->buildQuery($object, $element);
             $this->connection->query($query);
         } else {
             $id_set[$id] = true;
         }
     }
     // remove old unused elements
     $delete_builder = new Map_Delete($property);
     foreach ($old_map as $old_element) {
         $id = $this->getObjectIdentifier($old_element);
         if (!isset($id_set[$id])) {
             $query = $delete_builder->buildQuery($object, $old_element);
             $this->connection->query($query);
         }
     }
 }
Пример #19
0
 /**
  * @param $object        object
  * @param $property      Reflection_Property
  * @param $value         mixed
  * @param $null_if_empty boolean
  * @return boolean
  */
 private function buildSubObject($object, Reflection_Property $property, $value, $null_if_empty)
 {
     $is_null = $null_if_empty;
     $property_name = $property->name;
     $type = $property->getType();
     if (!isset($this->builders[$property_name])) {
         $this->builders[$property_name] = new Object_Builder_Array($type->getElementTypeAsString(), $this->from_form);
     }
     $builder = $this->builders[$property_name];
     if ($type->isMultiple()) {
         $is_null = $this->buildSubObjectMultiple($object, $property_name, $value, $null_if_empty, $builder);
     } else {
         $value = $builder->build($value, null, $null_if_empty);
         if (isset($value)) {
             $object->{$property_name} = $value;
             $is_null = false;
         }
     }
     return $is_null;
 }
Пример #20
0
 /**
  * Test property @getter : setting annotation value
  */
 public function testGetterAnnotationSet()
 {
     $this->method('@getter : setting annotation value');
     $property = new Reflection_Property(self::class, 'property');
     // @getter methodName
     $this->assume('methodName', (new Getter_Annotation('testGetterAnnotation', $property))->value, __CLASS__ . '::testGetterAnnotation');
     // @getter Local_Class_Name::methodName
     $this->assume('Local_Class_Name::methodName', (new Getter_Annotation('User_Annotation::has', $property))->value, User_Annotation::class . '::has');
     // @getter Distant\Class\Full\Path::methodName
     $this->assume('Distant\\Class\\Full\\Path\\Class_Name::methodName', (new Getter_Annotation(Annoted::class . '::has', $property))->value, Annoted::class . '::has');
     // use Distant\Class\Full\Path\Class_Name
     // @getter Class_Name::methodName
     $this->assume('use Class_Name::methodName', (new Getter_Annotation('Annoted::has', $property))->value, Annoted::class . '::has');
     // default value for getter when there is a @link annotation
     $this->assume('default value when @link', $property->getAnnotation('getter')->value, Getter::class . '::getCollection');
 }
Пример #21
0
 /**
  * @param $class_name    string a class name
  * @param $property_name string a property name or a property path starting from the class
  * @return boolean true if the property exists
  */
 public static function exists($class_name, $property_name)
 {
     if (strpos($property_name, DOT) !== false) {
         $properties_name = explode(DOT, $property_name);
         foreach (array_slice($properties_name, 0, -1) as $property_name) {
             if (!property_exists($class_name, $property_name)) {
                 return false;
             }
             $property = new Reflection_Property($class_name, $property_name);
             $class_name = $property->getType()->getElementTypeAsString();
         }
         $property_name = end($properties_name);
     }
     return property_exists($class_name, $property_name);
 }
Пример #22
0
 /**
  * Gets mysql expression for a property type
  *
  * @param $property Reflection_Property
  * @return string
  */
 private static function propertyTypeToMysql(Reflection_Property $property)
 {
     $property_type = $property->getType();
     if ($property_type->isBasic() || $property->getAnnotation('store')->value == 'string') {
         if ($property_type->hasSize()) {
             /** @var integer $max_length */
             $max_length = $property->getAnnotation('max_length')->value;
             $max_value = $property->getAnnotation('max_value')->value;
             $min_value = $property->getAnnotation('min_value')->value;
             $precision = $property->getAnnotation('precision')->value;
             $signed = $property->getAnnotation('signed')->value;
             $signed_length = $max_length + ($signed ? 1 : 0);
             if (!isset($max_length)) {
                 if ($property_type->isNumeric()) {
                     $max_length = 18;
                     $signed_length = 18;
                 } else {
                     $max_length = 255;
                 }
             }
             if ($property_type->isInteger()) {
                 if (!isset($signed) && $max_value < 0 || $min_value < 0) {
                     $signed = true;
                 }
                 return $max_length <= 3 && $min_value >= -128 && $max_value <= 127 && $signed ? 'tinyint(' . $signed_length . ')' : ($max_length <= 3 && $min_value >= 0 && $max_value <= 255 && !$signed ? 'tinyint(' . $max_length . ') unsigned' : ($max_length <= 5 && $min_value >= -32768 && $max_value <= 32767 ? 'smallint(' . $signed_length . ')' : ($max_length <= 5 && $min_value >= 0 && $max_value <= 65535 ? 'smallint(' . $max_length . ') unsigned' : ($max_length <= 7 && $min_value >= -8388608 && $max_value <= 8388607 ? 'mediumint(' . $signed_length . ')' : ($max_length <= 8 && $min_value >= 0 && $max_value <= 16777215 ? 'mediumint(' . $max_length . ') unsigned' : ($max_length <= 10 && $min_value >= -2147483648.0 && $max_value <= 2147483647 ? 'int(' . $signed_length . ')' : ($max_length <= 10 && $min_value >= 0 && $max_value <= 4294967295.0 ? 'int(' . $max_length . ') unsigned' : ($max_length <= 19 && $min_value >= -9.223372036854776E+18 && $max_value <= 9.223372036854776E+18 ? 'bigint(' . $signed_length . ')' : 'bigint(' . $max_length . ') unsigned'))))))));
             } elseif ($property_type->isFloat()) {
                 return $precision ? 'decimal(' . $signed_length . ', ' . $precision . ')' : 'double';
             } elseif ($property->getAnnotation('binary')->value) {
                 return $max_length <= 65535 ? 'blob' : ($max_length <= 16777215 ? 'mediumblob' : 'longblob');
             } else {
                 return $max_length <= 3 ? 'char(' . $max_length . ')' : ($max_length <= 255 ? 'varchar(' . $max_length . ')' : ($max_length <= 65535 ? 'text' : ($max_length <= 16777215 ? 'mediumtext' : 'longtext'))) . ' CHARACTER SET utf8 COLLATE utf8_general_ci';
             }
         }
         switch ($property_type->asString()) {
             case Type::_ARRAY:
                 return null;
             case Type::BOOLEAN:
                 return 'tinyint(1)';
             case Type::_CALLABLE:
                 return null;
             case Type::null:
             case Type::NULL:
                 return null;
             case Type::RESOURCE:
                 return null;
             case DateTime::class:
             case Date_Time::class:
                 return 'datetime';
             default:
                 return 'char(255)';
         }
     } elseif ($property_type->asString() === Type::STRING_ARRAY) {
         /** @var $values string[] */
         $values = [];
         foreach ($property->getListAnnotation('values')->values() as $key => $value) {
             $values[$key] = str_replace(Q, Q . Q, $value);
         }
         return $values ? ($property->getAnnotation('set')->value ? 'set' : 'enum') . "('" . join("','", $values) . "')" : 'text';
     } else {
         return 'bigint(18) unsigned';
     }
 }
Пример #23
0
 /**
  * @param $property Reflection_Property
  * @return array[]
  */
 private function getPropertyBlocks(Reflection_Property $property)
 {
     $blocks = [];
     if ($property->getListAnnotation('integrated')->has('block')) {
         $blocks[$property->path] = $property->path;
     }
     foreach ($property->getListAnnotation('block')->values() as $block) {
         $blocks[$block] = $block;
     }
     return $blocks;
 }
Пример #24
0
 /**
  * Generic getter for an object
  *
  * @param $stored     mixed actual value of the object, or identifier to an object, or null
  * @param $class_name string the object class name
  * @param $object     object the parent object
  * @param $property   string|Reflection_Property the parent property
  * @return object
  */
 public static function getObject(&$stored, $class_name, $object = null, $property = null)
 {
     if (!(self::$ignore || is_object($stored))) {
         if ($property instanceof Reflection_Property) {
             $property_name = $property->name;
         } elseif (is_string($property) && is_object($object)) {
             $property_name = $property;
             $property = new Reflection_Property(get_class($object), $property_name);
         }
         if (is_object($object) && isset($property_name)) {
             $id_property_name = 'id_' . $property_name;
             if (isset($object->{$id_property_name})) {
                 $stored = $object->{$id_property_name};
             }
         }
         if (isset($stored)) {
             if (isset($property) && $property->getAnnotation('store')->value == 'string') {
                 /** @var $stored_object Stringable */
                 $stored_object = Builder::create($property->getType()->asString());
                 $stored_object->fromString($stored);
                 $stored = $stored_object;
             } else {
                 $stored = isset($property) && ($dao = $property->getAnnotation('dao')->value) ? Dao::get($dao)->read($stored, $class_name) : Dao::read($stored, $class_name);
             }
         }
     }
     return $stored;
 }
Пример #25
0
 /**
  * @param $join                 Join
  * @param $master_path          string
  * @param $master_property_name string
  * @param $foreign_path         string
  * @return string the foreign class name
  */
 private function addSimpleJoin(Join $join, $master_path, $master_property_name, $foreign_path)
 {
     $foreign_class_name = null;
     $master_property = $this->getProperty($master_path, $master_property_name);
     if ($master_property) {
         $foreign_type = $master_property->getType();
         if ($foreign_type->isMultiple() && $foreign_type->getElementTypeAsString() == 'string') {
             // TODO : string[] can have multiple implementations, depending on database engine
             // linked strings table, mysql set.. should find a way to make this common without
             // knowing anything about the specific
             $foreign_class_name = $foreign_type->asString();
         } elseif (!$foreign_type->isBasic()) {
             $join->mode = $master_property->getAnnotation('mandatory')->value ? Join::INNER : Join::LEFT;
             // force LEFT if any of the properties in the master property path is not mandatory
             if ($join->mode == Join::INNER && $master_path) {
                 $root_class = new Reflection_Class($this->classes['']);
                 $property_path = '';
                 foreach (explode(DOT, $master_path . DOT . $master_property_name) as $property_name) {
                     $property_path .= ($property_path ? DOT : '') . $property_name;
                     $property = $root_class->getProperty($property_path);
                     if (!$property || !$property->getAnnotation('mandatory')->value) {
                         $join->mode = Join::LEFT;
                         break;
                     }
                 }
             }
             if ($foreign_type->isMultiple()) {
                 $foreign_class_name = $foreign_type->getElementTypeAsString();
                 $foreign_property_name = $master_property->getAnnotation('foreign')->value;
                 if (property_exists($foreign_class_name, $foreign_property_name) && $master_property->getAnnotation('link')->value != Link_Annotation::MAP) {
                     $foreign_property = new Reflection_Property($foreign_class_name, $foreign_property_name);
                     $join->foreign_column = 'id_' . $foreign_property->getAnnotation('storage')->value;
                     $join->master_column = 'id';
                 } else {
                     $this->addLinkedJoin($join, $master_path, $foreign_path, $foreign_class_name, $master_property);
                 }
             } else {
                 $foreign_class_name = $foreign_type->asString();
                 $join->master_column = 'id_' . $master_property->getAnnotation('storage')->value;
                 $join->foreign_column = 'id';
             }
         }
     }
     return $foreign_class_name;
 }
Пример #26
0
 /**
  * Builds column name from a property
  *
  * If property data type is an object, the property name will be prefixed with 'id_'.
  * Array properties will return null as no column should be associated to them.
  *
  * @param $property Reflection_Property
  * @return string|null
  */
 public static function buildColumnName(Reflection_Property $property)
 {
     $type = $property->getType();
     return $type->isBasic() ? $property->getAnnotation('storage')->value : ($type->isMultiple() ? null : 'id_' . $property->getAnnotation('storage')->value);
 }
Пример #27
0
 /**
  * Prepares path_classes if it is null
  * Must be called after prepareColumns()
  *
  * @param $property_name string
  */
 private function preparePathClass($property_name)
 {
     $property = new Reflection_Property($this->class_name, $property_name);
     $class_name = $property->getType()->getElementTypeAsString();
     $this->path_classes[$property_name] = $class_name;
 }
Пример #28
0
 /**
  * @param $property Reflection_Property
  * @return boolean
  */
 public function hasRange(Reflection_Property $property)
 {
     $type_string = $property->getType()->asString();
     return in_array($type_string, [Date_Time::class, Type::INTEGER, Type::STRING]);
 }
Пример #29
0
 /**
  * Changes an array into an object
  *
  * You should set only public and non-static properties values
  *
  * @param $class_name string
  * @param $array      array
  * @return object
  */
 public static function fromArray($class_name, $array)
 {
     $object = self::create($class_name);
     if (isset($array)) {
         foreach ($array as $property_name => $value) {
             if (is_array($value)) {
                 $property = new Reflection_Property($class_name, $property_name);
                 if ($property->getType()->isClass()) {
                     $property_class_name = $property->getType()->getElementTypeAsString();
                     if ($property->getType()->isMultiple()) {
                         foreach ($value as $key => $val) {
                             $value[$key] = self::fromArray($property_class_name, $val);
                         }
                     } else {
                         $value = self::fromArray($property_class_name, $value);
                     }
                     $property->setValue($object, $value);
                 }
             }
             $object->{$property_name} = $value;
         }
     }
     return $object;
 }
Пример #30
0
 /**
  * Builds import settings using a recursive array coming from an input form
  *
  * @param $worksheet  array
  * @return Import_Settings
  */
 public static function buildForm($worksheet)
 {
     $main_class_name = null;
     $settings = new Import_Settings();
     if (isset($worksheet['name'])) {
         $settings->name = $worksheet['name'];
     }
     if (isset($worksheet['classes'])) {
         foreach ($worksheet['classes'] as $property_path => $class) {
             if (ctype_upper($property_path[0])) {
                 // the first element is always the main class name
                 $class_name = $main_class_name = $property_path;
                 $settings->class_name = $class_name;
                 $property_path = '';
             } else {
                 // property paths for next elements
                 $property_path = str_replace('>', DOT, $property_path);
                 $property = new Reflection_Property($main_class_name, $property_path);
                 $class_name = $property->getType()->getElementTypeAsString();
             }
             $settings->classes[$property_path] = self::buildFormClass($class_name, $property_path, $class);
         }
     }
     return $settings;
 }