lithium\action\Dispatcher::applyRules PHP Méthode

applyRules() public static méthode

Attempts to apply a set of formatting rules from $_rules to a $params array, where each formatting rule is applied if the key of the rule in $_rules is present and not empty in $params. Also performs sanity checking against $params to ensure that no value matching a rule is present unless the rule check passes.
public static applyRules ( array &$params ) : array
$params array An array of route parameters to which rules will be applied.
Résultat array Returns the `$params` array with formatting rules applied to array values.
    public static function applyRules(&$params)
    {
        $values = array();
        $rules = static::$_rules;
        if (!$params) {
            return false;
        }
        if (isset($params['controller']) && is_string($params['controller'])) {
            $controller = $params['controller'];
            if (strpos($controller, '.') !== false) {
                list($library, $controller) = explode('.', $controller);
                $controller = $library . '.' . Inflector::camelize($controller);
                $params += compact('library');
            } elseif (strpos($controller, '\\') === false) {
                $controller = Inflector::camelize($controller);
                if (isset($params['library'])) {
                    $controller = "{$params['library']}.{$controller}";
                }
            }
            $values = compact('controller');
        }
        $values += $params;
        if (is_callable($rules)) {
            $rules = $rules($params);
        }
        foreach ($rules as $rule => $value) {
            if (!isset($values[$rule])) {
                continue;
            }
            foreach ($value as $k => $v) {
                if (is_callable($v)) {
                    $values[$k] = $v($values);
                    continue;
                }
                $match = preg_replace('/\\{:\\w+\\}/', '@', $v);
                $match = preg_replace('/@/', '.+', preg_quote($match, '/'));
                if (preg_match('/' . $match . '/i', $values[$k])) {
                    continue;
                }
                $values[$k] = String::insert($v, $values);
            }
        }
        return $values;
    }

Usage Example

Exemple #1
0
 public function testApplyRulesNamespacingCollision()
 {
     $params = array('library' => 'li3_one', 'controller' => 'li3_two.test', 'action' => 'test');
     $expected = array('library' => 'li3_one', 'controller' => 'li3_two.Test', 'action' => 'test');
     $this->assertEqual($expected, Dispatcher::applyRules($params));
     $params = array('library' => 'li3_one', 'controller' => 'li3_two\\Test', 'action' => 'test');
     $expected = array('library' => 'li3_one', 'controller' => 'li3_two\\Test', 'action' => 'test');
     $this->assertEqual($expected, Dispatcher::applyRules($params));
 }