/** * Guess value for @link using the type of the property (@var) * - property is a Date_Time (or a child class) : @link will be 'DateTime' * - property is a single object : @link will be 'Object' * - property is a collection object with '@var Object[] All' : @link will be 'All' * - property is a collection object with '@var Object[] Collection' : @link will be 'Collection' * - property is a collection object with '@var Object[] Map' : @link will be 'Map' * - property is a collection object without telling anything : * - @link will be 'Collection' if the object is a Component * - @link will be 'Map' if the object is not a Component * * @param $property Reflection_Property * @return string returned guessed value for @link */ private function guessValue(Reflection_Property $property) { if ($property->getType()->isMultiple()) { /** @var $var_annotation Var_Annotation */ $value = lParse($property->getAnnotation('var'), SP); if (empty($value)) { $value = isA($property->getType()->getElementTypeAsString(), Component::class) ? self::COLLECTION : self::MAP; } } else { $value = isA($property->getType()->asString(), Date_Time::class) ? self::DATETIME : self::OBJECT; } return $value; }
/** * @param $property Reflection_Property * @param $value mixed * @param $preprop string */ public function __construct(Reflection_Property $property = null, $value = null, $preprop = null) { if (isset($property)) { $name = $property->pathAsField(); if (strpos($name, '[')) { $preprop2 = lLastParse($name, '['); $preprop = $preprop ? $preprop . '[' . lParse($preprop2, '[') . '[' . rParse($preprop2, '[') : $preprop2; $name = lParse(rLastParse($name, '['), ']'); } parent::__construct($name, $property->getType(), $value, $preprop); $this->null = $property->getAnnotation('null')->value; $this->property = $property; $this->readonly = $property->getListAnnotation('user')->has(User_Annotation::READONLY); } else { parent::__construct(null, null, $value, $preprop); } }
/** * @param $loop Loop * @param $content string * @param $j integer * @return integer the length of the end tag var name */ protected function parseLoopVarName(Loop $loop, &$content, &$j) { $search_var_name = $loop->var_name; if (substr($loop->var_name, -1) == '>') { $end_last = true; $loop->var_name = substr($loop->var_name, 0, -1); } while (($k = strpos($loop->var_name, '{')) !== false) { $l = strpos($loop->var_name, '}'); $this->parseVar($loop->var_name, $k + 1, $l); } $loop->force_equality = $loop->var_name[0] === '='; if ($loop->force_equality) { $loop->var_name = substr($loop->var_name, 1); } $loop->force_condition = substr($loop->var_name, -1) === '?'; if ($loop->force_condition) { $loop->var_name = substr($loop->var_name, 0, -1); } if (strpos($loop->var_name, ':')) { list($loop->var_name, $loop->has_expr) = explode(':', $loop->var_name); $search_var_name = lParse($search_var_name, ':'); if (($sep = strpos($loop->has_expr, '-')) !== false) { $loop->from = substr($loop->has_expr, 0, $sep); $loop->to = substr($loop->has_expr, $sep + 1); } else { $loop->from = $loop->to = $loop->has_expr; } $loop->to = $loop->to == '' ? null : $loop->to; } else { $expr = null; $loop->from = 0; $loop->to = null; } $length2 = strlen($search_var_name); $j = isset($end_last) ? strrpos($content, '<!--' . $search_var_name . '-->', $j + 3) : strpos($content, '<!--' . $search_var_name . '-->', $j + 3); return $length2; }
/** * @return string */ protected static function customId() { return lParse(strtolower(Namespaces::shortClassName(get_called_class())), '_settings'); }
/** * Returns the middle part of the string, between $begin_sep and $end_sep * * If separators are arrays, it will search the first separator, then the next one, etc. * * @example echo mParse('He eats, drinks and then sleep', [', ', SP], ' then ') * Will result in 'and' * It looks what is after ', ' and then what is after the next space * The returned value stops before ' then ' * @param $str string * @param $begin_sep string|string[] * @param $end_sep string|string[] * @param $cnt integer * @return string */ function mParse($str, $begin_sep, $end_sep, $cnt = 1) { // if $begin_sep is an array, rParse each $begin_sep element if (is_array($begin_sep)) { $sep = array_pop($begin_sep); foreach ($begin_sep as $beg) { $str = rParse($str, $beg, $cnt); $cnt = 1; } $begin_sep = $sep; } // if $end_sep is an array, lParse each $end_sep element, starting from the last one if (is_array($end_sep)) { $end_sep = array_reverse($end_sep); $sep = array_pop($end_sep); foreach ($end_sep as $end) { $str = lParse($str, $end); } $end_sep = $sep; } return lParse(rParse($str, $begin_sep, $cnt), $end_sep); }
/** * Replace all references to $replaced by references to $replacement into the database. * Already loaded objects will not be changed. * * @param $replaced object * @param $replacement object * @return boolean true if replacement has been done, false if something went wrong */ public function replaceReferences($replaced, $replacement) { $table_name = $this->storeNameOf(get_class($replaced)); $replaced_id = $this->getObjectIdentifier($replaced); $replacement_id = $this->getObjectIdentifier($replacement); if ($replaced_id && $replacement_id && $table_name) { foreach (Foreign_Key::buildReferences($this->connection, $table_name) as $foreign_key) { $foreign_table_name = lParse($foreign_key->getConstraint(), DOT); $foreign_field_name = $foreign_key->getFields()[0]; $query = 'UPDATE ' . BQ . $foreign_table_name . BQ . LF . 'SET ' . BQ . $foreign_field_name . BQ . ' = ' . $replacement_id . LF . 'WHERE ' . BQ . $foreign_field_name . BQ . ' = ' . $replaced_id; $this->query($query); if ($this->connection->last_errno) { $error = true; } } return isset($error) ? false : true; } return false; }
/** * @param $builder Object_Builder_Array * @param $null_if_empty boolean * @param $object object * @param $property_name string * @param $value mixed * @return boolean */ private function buildSubObjectMultiple($object, $property_name, $value, $null_if_empty, Object_Builder_Array $builder) { $is_null = $null_if_empty; if (is_array($value)) { // keys are numeric : multiple values case foreach ($value as $key => $element) { if (is_numeric($number = lParse($key, DOT, 1, false))) { $values[$number][rParse($key, DOT)] = $element; } else { unset($values); break; } } // single value case if (!isset($values)) { $values = [$value]; } // build values foreach ($values as $element) { $element = $builder->build($element, null, $null_if_empty); if (isset($element)) { // call property getter if exist (do not remove this !) $object->{$property_name}; array_push($object->{$property_name}, $element); $is_null = false; } } } return $is_null; }
/** * @param $query string * @return string[]|null */ private function guessContext($query) { $context = []; // first clause between `...` is probably the name of the table $table_name = mParse($query, BQ, BQ); if ($table_name) { $class_name = Dao::classNameOf($table_name); if ($class_name) { $context[] = $class_name; } } // every JOIN `...` may be the name of a table $joins = explode('JOIN ' . BQ, $query); array_shift($joins); foreach ($joins as $join) { $table_name = lParse($join, BQ); if ($table_name) { $class_name = Dao::classNameOf($table_name); if ($class_name) { $context[] = $class_name; } } } return $context ? $context : null; }