Beispiel #1
0
 public function maps_to($prototype)
 {
     if (!($class_name = Core_Types::real_class_name_for($prototype))) {
         throw new Core_InvalidArgumentTypeException('prototype', $prototype);
     }
     if (Core_Types::is_object($prototype)) {
         $this->prototype = $prototype;
     } else {
         $this->prototype = new $class_name();
     }
     if (Core_Types::is_subclass_of('DB.SQL.Entity', $this->prototype)) {
         DB_SQL::db()->map_class(Core_Types::real_class_name_for($this->prototype), $this);
     }
     return $this;
 }
Beispiel #2
0
 public function delete($object)
 {
     if ($this->is_frozen()) {
         throw new Core_ReadOnlyObjectException($this);
     } else {
         $is_object = Core_Types::is_object($object);
         foreach ($this as $k => &$v) {
             if ($is_object && $v === $object || !$is_object && $v == $object) {
                 unset($this[$k]);
             }
         }
         return $this;
     }
 }
Beispiel #3
0
 /**
  * Возвращает reflection для заданного объекта или класса
  *
  * @param  $object
  *
  * @return mixed
  */
 public static function reflection_for($object)
 {
     if (Core_Types::is_string($object)) {
         return new ReflectionClass(self::real_class_name_for($object));
     }
     if (Core_Types::is_object($object)) {
         return new ReflectionObject($object);
     }
     throw new Core_InvalidArgumentTypeException('object', $object);
 }
Beispiel #4
0
 /**
  * @param  $object
  *
  * @return string
  */
 protected function stringify($object)
 {
     switch (true) {
         case $object instanceof Core_StringifyInterface:
             return $object->as_string();
         case $object instanceof ArrayObject:
         case $object instanceof stdClass:
         default:
             return var_export($object, true);
         case Core_Types::is_object($object):
             return sprintf('%s(%s)', Core_Types::class_name_for($object, true), spl_object_hash($object));
     }
 }
Beispiel #5
0
 /**
  * Устанавливает прототип для возвращаемого результата
  *
  * @params object|string $prototype Прототип объекта,
  * используемого для хранения объектов записей, или имя класса такого объекта
  * @params boolean $ignore_type игнорировать ли колонку type
  *
  * @throws Core_BadArgumentTypeException Если параметр $prototype имеет недопустимый тип
  *
  * @return self
  */
 public function as_object($prototype, $ignore_type = false)
 {
     $this->ignore_type = (bool) $ignore_type;
     if (Core_Types::is_string($prototype)) {
         $this->prototype = Core_Types::reflection_for($prototype)->newInstance();
     } elseif (Core_Types::is_object($prototype)) {
         $this->prototype = $prototype;
     } else {
         throw new Core_BadArgumentTypeException('prototype');
     }
     return $this;
 }
Beispiel #6
0
 /**
  * Возвращает значение атрибута объекта.
  *
  * @param mixed   $object       Если не является объектом или массивом, то становится возвращаемым значением.
  * @param string  $attribute    имя атрибута
  * @param boolean $array_access флаг индексного доступа к объекту
  *
  * @return mixed
  */
 protected function value_of_attribute($object, $attribute, $array_access = false)
 {
     return Core_Types::is_object($object) ? $array_access ? $object[$attribute] : $object->{$attribute} : (Core_Types::is_array($object) ? $object[$attribute] : $object);
 }
Beispiel #7
0
 public function append($delegate, $name = null)
 {
     if (!Core_Types::is_object($delegate)) {
         throw new Core_InvalidArgumentTypeException('delegate', $delegate);
     }
     $this->objects[$name] = $delegate;
     $this->reflections[$name] = Core_Types::reflection_for($delegate);
     return $this;
 }
Beispiel #8
0
 /**
  * @param string $name
  * @param array  $args
  *
  * @return mixed
  */
 public function __call($name, $args)
 {
     if (!Core_Regexps::match('{_url$}', $name)) {
         throw new Core_MissingMethodException($name);
     }
     $name = Core_Strings::replace($name, 'single_', 'single-');
     $url = '';
     $args = Core_Arrays::reverse($args);
     $parms = Core_Types::is_array($args[0]) ? Core_Arrays::shift($args) : array();
     $parts = Core_Arrays::reverse(Core_Strings::split_by('_', Core_Regexps::replace('{_url$}', '', $name)));
     $last_idx = count($parts) - 1;
     $target = false;
     foreach ($parts as $idx => $part) {
         $part = Core_Strings::replace($part, '-', '_');
         if ($target) {
             if (isset($this->single_names[$part])) {
                 $url = '/' . $this->single_names[$part] . '/' . (Core_Types::is_object($arg = Core_Arrays::shift($args)) ? $arg->id : (string) $arg) . $url;
             } elseif ($idx == $last_idx && (isset($this->resources[$target]) && isset($this->resources[$target]['collection']) && isset($this->resources[$target]['collection'][$part])) || isset($this->single_names[$target]) && isset($this->resources[$this->single_names[$target]]['instance']) && isset($this->resources[$this->single_names[$target]]['instance'][$part])) {
                 $url .= "/{$part}";
             } else {
                 throw new Core_MissingMethodException($name);
             }
         } else {
             if (isset($this->resources[$part])) {
                 $url = "/{$part}{$url}";
                 $target = $part;
             } elseif (isset($this->single_names[$part])) {
                 $id = Core_Arrays::shift($args);
                 $url = '/' . $this->single_names[$part] . '/' . (Core_Types::is_object($id) ? $id->id : (string) $id) . $url;
                 $target = $part;
             } else {
                 throw new Core_MissingMethodException($name);
             }
         }
     }
     return $this->add_keyword_parameters($this->add_path($url) . (isset($args[0]) ? '.' . $args[0] : ".{$this->default_format}"), $parms);
 }