lithium\util\Set::expand PHP Method

expand() public static method

Accepts a one-dimensional array where the keys are separated by a delimiter.
public static expand ( array $data, array $options = [] ) : array
$data array The one-dimensional array to expand.
$options array The options used when expanding the array: - `'separator'` _string_: The delimiter to use when separating keys. Defaults to `'.'`.
return array Returns a multi-dimensional array expanded from a one dimensional dot-separated array.
    public static function expand(array $data, array $options = array())
    {
        $defaults = array('separator' => '.');
        $options += $defaults;
        $result = array();
        foreach ($data as $key => $val) {
            if (strpos($key, $options['separator']) === false) {
                if (!isset($result[$key])) {
                    $result[$key] = $val;
                }
                continue;
            }
            list($path, $key) = explode($options['separator'], $key, 2);
            $path = is_numeric($path) ? (int) $path : $path;
            $result[$path][$key] = $val;
        }
        foreach ($result as $key => $value) {
            if (is_array($value)) {
                $result[$key] = static::expand($value, $options);
            }
        }
        return $result;
    }

Usage Example

Beispiel #1
0
 /**
  * Magic method to make Controller callable.
  *
  * @see lithium\action\Dispatcher::_callable()
  * @param \lithium\action\Request $request
  * @param array $dispatchParams Array of params after being parsed by router.
  * @param array $options Some basic options for this controller.
  * @return string
  * @filter
  */
 public function __invoke($request, $dispatchParams, array $options = array())
 {
     $dispatchParamsDefaults = array('args' => array());
     $dispatchParams += $dispatchParamsDefaults;
     $defaults = array('format' => 'html', 'timeout' => 0);
     $options += (array) $request->query + $defaults;
     $params = compact('request', 'dispatchParams', 'options');
     return $this->_filter(__METHOD__, $params, function ($self, $params) {
         $request = $params['request'];
         $options = $params['options'];
         $params = $params['dispatchParams'];
         set_time_limit((int) $options['timeout']);
         $group = join('\\', (array) $params['args']);
         if ($group === "all") {
             $group = Group::all();
             $options['title'] = 'All Tests';
         }
         $self->invokeMethod('_saveCtrlContext');
         $report = Dispatcher::run($group, $options);
         $self->invokeMethod('_restoreCtrlContext');
         $filters = Libraries::locate('test.filter');
         $menu = Libraries::locate('tests', null, array('filter' => '/cases|integration|functional/', 'exclude' => '/mocks/'));
         sort($menu);
         $menu = Set::expand(array_combine($menu, $menu), array('separator' => "\\"));
         $result = compact('request', 'report', 'filters', 'menu');
         return $report->render('layout', $result);
     });
 }
All Usage Examples Of lithium\util\Set::expand