normalize() public static method

Set::normalize('foo,bar'); // returns array('foo' => null, 'bar' => null); Set::normalize(array('foo', 'bar' => 'baz'); // returns array('foo' => null, 'bar' => 'baz');
public static normalize ( string | array $list, boolean $assoc = true, string $sep = ',', boolean $trim = true ) : array
$list string | array List to normalize.
$assoc boolean If `true`, `$list` will be converted to an associative array.
$sep string If `$list` is a string, it will be split into an array with `$sep`.
$trim boolean If `true`, separated strings will be trimmed.
return array
Example #1
0
 /**
  * Initializes the record set and uses the database connection to get the column list contained
  * in the query that created this object.
  *
  * @see lithium\data\collection\RecordSet::$_columns
  * @return void
  * @todo The part that uses _handle->schema() should be rewritten so that the column list
  *       is coming from the query object.
  */
 protected function _init()
 {
     parent::_init();
     if ($this->_result) {
         $this->_columns = $this->_columnMap();
         if ($this->_query) {
             $columns = array_filter(array_keys($this->_columns));
             $this->_dependencies = Set::expand(Set::normalize($columns));
             $this->_keyIndex = $this->_keyIndex('');
         }
     }
 }
Example #2
0
 /**
  * Initializes the record set and uses the database connection to get the column list contained
  * in the query that created this object.
  *
  * @see lithium\data\collection\RecordSet::$_columns
  * @return void
  * @todo The part that uses _handle->schema() should be rewritten so that the column list
  *       is coming from the query object.
  */
 protected function _init()
 {
     parent::_init();
     if (!$this->_result) {
         return;
     }
     $this->_columns = $this->_columnMap();
     if (!$this->_query) {
         return;
     }
     $this->_keyIndex = $this->_keyIndex();
     $this->_dependencies = Set::expand(Set::normalize(array_filter(array_keys($this->_columns))));
     $this->_relationships = $this->_query->relationships();
 }
 /**
  * Runs a test group or a specific test file based on the passed
  * parameters.
  *
  * @param string $group If set, this test group is run. If not set, a group test may
  *        also be run by passing the 'group' option to the $options parameter.
  * @param array $options Options array for the test run. Valid options are:
  *        - 'case': The fully namespaced test case to be run.
  *        - 'group': The fully namespaced test group to be run.
  *        - 'filters': An array of filters that the test output should be run through.
  * @return array A compact array of the title, an array of the results, as well
  *         as an additional array of the results after the $options['filters']
  *         have been applied.
  */
 public static function run($group = null, $options = array())
 {
     $defaults = array('title' => $group, 'filters' => array(), 'reporter' => 'text');
     $options += $defaults;
     $items = (array) $group;
     $isCase = preg_match('/Test$/', $group);
     if ($isCase) {
         $items = array(new $group());
     }
     $options['filters'] = Set::normalize($options['filters']);
     $group = static::_group($items);
     $report = static::_report($group, $options);
     $report->run();
     return $report;
 }
Example #4
0
 /**
  * Runs a test group or a specific test file based on the passed
  * parameters.
  *
  * @param string $group If set, this test group is run. If not set, a group test may
  *        also be run by passing the 'group' option to the $options parameter.
  * @param array $options Options array for the test run. Valid options are:
  *        - 'case': The fully namespaced test case to be run.
  *        - 'group': The fully namespaced test group to be run.
  *        - 'filters': An array of filters that the test output should be run through.
  * @return array A compact array of the title, an array of the results, as well
  *         as an additional array of the results after the $options['filters']
  *         have been applied.
  * @filter
  */
 public static function run($group = null, array $options = array())
 {
     $defaults = array('title' => $group, 'filters' => array(), 'reporter' => 'text');
     $options += $defaults;
     $isCase = is_string($group) && preg_match('/Test$/', $group);
     $items = $isCase ? array(new $group()) : (array) $group;
     $options['filters'] = Set::normalize($options['filters']);
     $group = static::_group($items);
     $report = static::_report($group, $options);
     return static::_filter(__FUNCTION__, compact('report'), function ($self, $params, $chain) {
         $environment = Environment::get();
         Environment::set('test');
         $params['report']->run();
         Environment::set($environment);
         return $params['report'];
     });
 }
Example #5
0
 protected static function _prepareMatchParams($parameters)
 {
     foreach (Set::normalize($parameters) as $token => $scope) {
         if (strpos($token, 'T_') !== 0) {
             unset($parameters[$token]);
             foreach (array('before', 'after') as $key) {
                 if (!isset($scope[$key])) {
                     continue;
                 }
                 $items = array();
                 foreach ((array) $scope[$key] as $item) {
                     $items[] = strpos($item, 'T_') !== 0 ? static::token($item) : $item;
                 }
                 $scope[$key] = $items;
             }
             $parameters[static::token($token)] = $scope;
         }
     }
     return $parameters;
 }
Example #6
0
 /**
  * Iterates through relationship types to construct relation map.
  *
  * @return void
  * @todo See if this can be rewritten to be lazy.
  */
 protected static function _relationsToLoad()
 {
     try {
         if (!static::connection()) {
             return;
         }
     } catch (ConfigExcepton $e) {
         return;
     }
     $self = static::_object();
     foreach ($self->_relationTypes as $type) {
         $self->{$type} = Set::normalize($self->{$type});
         foreach ($self->{$type} as $name => $config) {
             $self->_relationsToLoad[$name] = $type;
         }
     }
 }
Example #7
0
	public function testMixedKeyNormalization() {
		$input = array('"string"' => array('before' => '=>'), 1 => array('before' => '=>'));
		$result = Set::normalize($input);
		$this->assertEqual($input, $result);

		$input = 'Foo,Bar,Baz';
		$result = Set::normalize($input);
		$this->assertEqual(array('Foo' => null, 'Bar' => null, 'Baz' => null), $result);

		$input = array('baz' => 'foo', 'bar');
		$result = Set::normalize($input, false);
		$this->assertEqual(array('baz' => 'foo', 'bar' => null), $result);
	}
Example #8
0
 protected function _init()
 {
     parent::_init();
     unset($this->_config['type']);
     $keys = array_keys($this->_config);
     foreach ($this->_initializers as $key) {
         $val = $this->_config[$key];
         if ($val !== null) {
             $this->_config[$key] = is_array($val) ? array() : null;
             $this->{$key}($val);
         }
     }
     if ($list = $this->_config['whitelist']) {
         $this->_config['whitelist'] = array_combine($list, $list);
     }
     if ($this->_entity && !$this->_config['model']) {
         $this->model($this->_entity->model());
     }
     if ($this->_config['with']) {
         if (!($model = $this->model())) {
             throw new ConfigException("The `'with'` option needs a valid binded model.");
         }
         $this->_config['with'] = Set::normalize($this->_config['with']);
     }
     if ($model = $this->model()) {
         $this->alias($this->_config['alias'] ?: $model::meta('name'));
     }
     $this->fields($this->_config['fields']);
     unset($this->_config['entity'], $this->_config['init']);
 }
Example #9
0
 /**
  * Iterates through relationship types to construct relation map.
  *
  * @return void
  * @todo See if this can be rewritten to be lazy.
  */
 protected static function _relations()
 {
     $self = static::_object();
     if (!$self->_meta['connection']) {
         return;
     }
     foreach ($self->_relationTypes as $type => $keys) {
         foreach (Set::normalize($self->{$type}) as $name => $config) {
             static::bind($type, $name, (array) $config);
         }
     }
 }
Example #10
0
 /**
  * Iterates through relationship types to construct relation map.
  *
  * @todo See if this can be rewritten to be lazy.
  * @return void
  */
 protected static function _relationsToLoad()
 {
     try {
         if (!($connection = static::connection())) {
             return;
         }
     } catch (ConfigException $e) {
         return;
     }
     if (!$connection::enabled('relationships')) {
         return;
     }
     $self = static::_object();
     foreach ($self->_relationTypes as $type) {
         $self->{$type} = Set::normalize($self->{$type});
         foreach ($self->{$type} as $name => $config) {
             $self->_relationsToLoad[$name] = $type;
             $fieldName = $self->_relationFieldName($type, $name);
             $self->_relationFieldNames[$fieldName] = $name;
         }
     }
 }
Example #11
0
 public function testMixedKeyNormalization()
 {
     $input = array('"string"' => array('before' => '=>'), 1 => array('before' => '=>'));
     $result = Set::normalize($input);
     $this->assertEqual($input, $result);
 }
Example #12
0
 /**
  * Initializes behaviors from the `$_actsAs` property of the model.
  *
  * @return void
  */
 protected static function _initializeBehaviors()
 {
     $model = get_called_class();
     if (!empty(static::$_initializedBehaviors[$model])) {
         return;
     }
     static::$_initializedBehaviors[$model] = true;
     $self = $model::_object();
     if (!property_exists($self, '_actsAs')) {
         return;
     }
     foreach (Set::normalize($self->_actsAs) as $name => $config) {
         static::bindBehavior($name, $config ?: []);
     }
 }
Example #13
0
 /**
  * Iterates through relationship types to construct relation map.
  *
  * @return void
  * @todo See if this can be rewritten to be lazy.
  */
 protected static function _relations()
 {
     $relations = array();
     $self = static::_instance();
     foreach ($self->_relationTypes as $type => $keys) {
         foreach (Set::normalize($self->{$type}) as $name => $options) {
             $key = Inflector::underscore($type == 'belongsTo' ? $name : $self->_meta['name']);
             $defaults = array('type' => $type, 'class' => $name, 'fields' => true, 'key' => $key . '_id');
             $relations[$name] = (array) $options + $defaults;
         }
     }
     return $relations;
 }
Example #14
0
 /**
  * Creates the database object and set default values for it.
  *
  * Options defined:
  *  - 'database' _string_ Name of the database to use. Defaults to `null`.
  *  - 'host' _string_ Name/address of server to connect to. Defaults to 'localhost'.
  *  - 'login' _string_ Username to use when connecting to server. Defaults to 'root'.
  *  - 'password' _string_ Password to use when connecting to server. Defaults to `''`.
  *  - 'persistent' _boolean_ If true a persistent connection will be attempted, provided the
  *    adapter supports it. Defaults to `true`.
  *
  * @param $config array Array of configuration options.
  * @return Database object.
  */
 public function __construct(array $config = array())
 {
     $defaults = array('persistent' => true, 'host' => 'localhost', 'login' => 'root', 'password' => '', 'database' => null, 'encoding' => null, 'dsn' => null, 'options' => array());
     $this->_strings += array('read' => 'SELECT {:fields} FROM {:source} {:alias} {:joins} {:conditions} {:group} ' . '{:having} {:order} {:limit};{:comment}');
     $this->_strategies += array('joined' => function ($self, $model, $context) {
         $with = $context->with();
         $strategy = function ($me, $model, $tree, $path, $from, &$deps) use($self, $context, $with) {
             foreach ($tree as $name => $childs) {
                 if (!($rel = $model::relations($name))) {
                     throw new QueryException("Model relationship `{$name}` not found.");
                 }
                 $constraints = array();
                 $alias = $name;
                 $relPath = $path ? $path . '.' . $name : $name;
                 if (isset($with[$relPath])) {
                     list($unallowed, $allowed) = Set::slice($with[$relPath], array('alias', 'constraints'));
                     if ($unallowed) {
                         $message = "Only `'alias'` and `'constraints'` are allowed in ";
                         $message .= "`'with'` using the `'joined'` strategy.";
                         throw new QueryException($message);
                     }
                     extract($with[$relPath]);
                 }
                 $to = $context->alias($alias, $relPath);
                 $deps[$to] = $deps[$from];
                 $deps[$to][] = $from;
                 if ($context->relationships($relPath) === null) {
                     $context->relationships($relPath, array('type' => $rel->type(), 'model' => $rel->to(), 'fieldName' => $rel->fieldName(), 'alias' => $to));
                     $self->join($context, $rel, $from, $to, $constraints);
                 }
                 if (!empty($childs)) {
                     $me($me, $rel->to(), $childs, $relPath, $to, $deps);
                 }
             }
         };
         $tree = Set::expand(Set::normalize(array_keys($with)));
         $alias = $context->alias();
         $deps = array($alias => array());
         $strategy($strategy, $model, $tree, '', $alias, $deps);
         $models = $context->models();
         foreach ($context->fields() as $field) {
             list($alias, $field) = $self->invokeMethod('_splitFieldname', array($field));
             $alias = $alias ?: $field;
             if ($alias && isset($models[$alias])) {
                 foreach ($deps[$alias] as $depAlias) {
                     $depModel = $models[$depAlias];
                     $context->fields(array($depAlias => (array) $depModel::meta('key')));
                 }
             }
         }
     }, 'nested' => function ($self, $model, $context) {
         throw new QueryException("This strategy is not yet implemented.");
     });
     parent::__construct($config + $defaults);
 }