lithium\util\Set::normalize PHP Method

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
    public static function normalize($list, $assoc = true, $sep = ',', $trim = true)
    {
        if (is_string($list)) {
            $list = explode($sep, $list);
            $list = $trim ? array_map('trim', $list) : $list;
            return $assoc ? static::normalize($list) : $list;
        }
        if (!is_array($list)) {
            return $list;
        }
        $keys = array_keys($list);
        $count = count($keys);
        $numeric = true;
        if (!$assoc) {
            for ($i = 0; $i < $count; $i++) {
                if (!is_int($keys[$i])) {
                    $numeric = false;
                    break;
                }
            }
        }
        if (!$numeric || $assoc) {
            $newList = array();
            for ($i = 0; $i < $count; $i++) {
                if (is_int($keys[$i]) && is_scalar($list[$keys[$i]])) {
                    $newList[$list[$keys[$i]]] = null;
                } else {
                    $newList[$keys[$i]] = $list[$keys[$i]];
                }
            }
            $list = $newList;
        }
        return $list;
    }

Usage Example

 /**
  * 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('');
         }
     }
 }
All Usage Examples Of lithium\util\Set::normalize