示例#1
0
 /**
  * @param DOMElement $element
  * @param mixed $data
  */
 protected function buildXml($element, $data)
 {
     if (is_object($data)) {
         $child = new DOMElement(ObjectHelper::normalizeNamespace(get_class($data)));
         $element->appendChild($child);
         if ($data instanceof Arrayable) {
             $this->buildXml($child, $data->toArray());
         } else {
             $array = [];
             foreach ($data as $name => $value) {
                 $array[$name] = $value;
             }
             $this->buildXml($child, $array);
         }
     } elseif (is_array($data)) {
         foreach ($data as $name => $value) {
             if (is_int($name) && is_object($value)) {
                 $this->buildXml($element, $value);
             } elseif (is_array($value) || is_object($value)) {
                 $child = new DOMElement(is_int($name) ? $this->itemTag : $name);
                 $element->appendChild($child);
                 $this->buildXml($child, $value);
             } else {
                 $child = new DOMElement(is_int($name) ? $this->itemTag : $name);
                 $element->appendChild($child);
                 $child->appendChild(new DOMText((string) $value));
             }
         }
     } else {
         $element->appendChild(new DOMText((string) $data));
     }
 }
示例#2
0
 /**
  * Declares the name of the Sphinx index associated with this AR class.
  * By default this method returns the class name as the index name by calling {@see \rock\helpers\Inflector::camel2id()}.
  * For example, 'Article' becomes 'article', and 'StockItem' becomes
  * 'stock_item'. You may override this method if the index is not named after this convention.
  * @return string the index name
  */
 public static function indexName()
 {
     return Inflector::camel2id(ObjectHelper::basename(get_called_class()), '_');
 }
示例#3
0
 /**
  * Count events of class (with parents).
  *
  * @param string|object $class
  * @return int
  */
 public static function countClass($class)
 {
     $class = ObjectHelper::getClass($class);
     $count = 0;
     do {
         if (!empty(self::$events[$class])) {
             $count += count(static::$events[$class]);
         }
     } while (($class = get_parent_class($class)) !== false);
     return $count;
 }
示例#4
0
 /**
  * Declares the name of the database table associated with this AR class.
  * By default this method returns the class name as the table name by calling {@see \rock\helpers\Inflector::camel2id()}
  * with prefix {@see \rock\db\Connection::$tablePrefix}.
  * For example if {@see \rock\db\Connection::$tablePrefix} is 'tbl_', 'Customer' becomes 'tbl_customer',
  * and 'OrderItem' becomes 'tbl_order_item'. You may override this method
  * if the table is not named after this convention.
  *
  * @return string the table name
  */
 public static function tableName()
 {
     return '{{%' . Inflector::camel2id(ObjectHelper::basename(get_called_class()), '_') . '}}';
 }
示例#5
0
 /**
  * Registers a path alias.
  *
  * A path alias is a short name representing a long path (a file path, a URL, etc.)
  * For example, we use '@rock' as the alias of the path to the Rock framework directory.
  *
  * A path alias must start with the character '@' so that it can be easily differentiated
  * from non-alias paths.
  *
  * Note that this method does not check if the given path exists or not. All it does is
  * to associate the alias with the path.
  *
  *
  * @param string $alias the alias name (e.g. "@rock"). It must start with a '@' character.
  * It may contain the forward slash '/' which serves as boundary character when performing
  * alias translation by {@see \rock\base\Alias::getAlias()}.
  * @param string $path the path corresponding to the alias. Trailing '/' and '\' characters
  * will be trimmed. This can be
  *
  * - a directory or a file path (e.g. `/tmp`, `/tmp/main.txt`)
  * - a URL (e.g. `http://www.site.com`)
  * - a path alias (e.g. `@rock/base`). In this case, the path alias will be converted into the
  *   actual path first by calling {@see \rock\base\Alias::getAlias()}.
  *
  * @param bool $trailingTrim any trailing '/' and '\' characters in the given path/url will be trimmed.
  * @throws \Exception if $path is an invalid alias.
  * @see getAlias()
  */
 public static function setAlias($alias, $path, $trailingTrim = true)
 {
     if (strncmp($alias, '@', 1)) {
         $alias = '@' . $alias;
     }
     $delimiter = ObjectHelper::isNamespace($alias) ? '\\' : '/';
     $pos = strpos($alias, $delimiter);
     $root = $pos === false ? $alias : substr($alias, 0, $pos);
     if ($path !== null) {
         if (!strncmp($path, '@', 1)) {
             $path = static::getAlias($path);
         } elseif ($trailingTrim) {
             $path = rtrim($path, '\\/');
         }
         if (!isset(static::$aliases[$root])) {
             if ($pos === false) {
                 static::$aliases[$root] = $path;
             } else {
                 static::$aliases[$root] = [$alias => $path];
             }
         } elseif (is_string(static::$aliases[$root])) {
             if ($pos === false) {
                 static::$aliases[$root] = $path;
             } else {
                 static::$aliases[$root] = [$alias => $path, $root => static::$aliases[$root]];
             }
         } else {
             static::$aliases[$root][$alias] = $path;
             krsort(static::$aliases[$root]);
         }
     } elseif (isset(static::$aliases[$root])) {
         if (is_array(static::$aliases[$root])) {
             unset(static::$aliases[$root][$alias]);
         } elseif ($pos === false) {
             unset(static::$aliases[$root]);
         }
     }
 }
示例#6
0
 /**
  * Returns the list of fields that should be returned by default by {@see \rock\components\ArrayableTrait::toArray()} when no specific fields are specified.
  *
  * A field is a named element in the returned array by {@see \rock\components\ArrayableTrait::toArray()}.
  *
  * This method should return an array of field names or field definitions.
  * If the former, the field name will be treated as an object property name whose value will be used
  * as the field value. If the latter, the array key should be the field name while the array value should be
  * the corresponding field definition which can be either an object property name or a PHP callable
  * returning the corresponding field value. The signature of the callable should be:
  *
  * ```php
  * function ($field, $model) {
  *     // return field value
  * }
  * ```
  *
  * For example, the following code declares four fields:
  *
  * - `email`: the field name is the same as the property name `email`;
  * - `firstName` and `lastName`: the field names are `firstName` and `lastName`, and their
  *   values are obtained from the `first_name` and `last_name` properties;
  * - `fullName`: the field name is `fullName`. Its value is obtained by concatenating `first_name`
  *   and `last_name`.
  *
  * ```php
  * return [
  *     'email',
  *     'firstName' => 'first_name',
  *     'lastName' => 'last_name',
  *     'fullName' => function () {
  *         return $this->first_name . ' ' . $this->last_name;
  *     },
  * ];
  * ```
  *
  * In this method, you may also want to return different lists of fields based on some context
  * information. For example, depending on the privilege of the current application user,
  * you may return different sets of visible fields or filter out some fields.
  *
  * The default implementation of this method returns the public object member variables.
  *
  * @return array the list of field names or field definitions.
  * @see toArray()
  */
 public function fields()
 {
     $fields = array_keys(ObjectHelper::getObjectVars($this));
     return array_combine($fields, $fields);
 }