lithium\action\Dispatcher::applyRules()

public static method

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.

Parameters

  • array $params

    An array of route parameters to which rules will be applied.

Returns

array

Returns the $params array with formatting rules applied to array values.

Source

	public static function applyRules(&$params) {
		$values = [];
		$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] = Text::insert($v, $values);
			}
		}
		return $values;
	}