lithium\analysis\Parser::match()
Token pattern matching.
Parameters
-
string
$code
Source code to be analyzed.
-
string
$parameters
An array containing token patterns to be matched.
-
array
$options
The list of options to be used when matching
$code
:- 'ignore': An array of language tokens to ignore.
- 'return': If set to 'content' returns an array of matching tokens.
Returns
arrayArray of matching tokens.
Source
public static function match($code, $parameters, array $options = []) {
$defaults = ['ignore' => ['T_WHITESPACE'], 'return' => true];
$options += $defaults;
$parameters = static::_prepareMatchParams($parameters);
$tokens = is_array($code) ? $code : static::tokenize($code, $options);
$results = [];
foreach ($tokens as $i => $token) {
if (!array_key_exists($token['name'], $parameters)) {
if (!in_array('*', $parameters)) {
continue;
}
}
$param = $parameters[$token['name']];
if (isset($param['before']) && $i > 0) {
if (!in_array($tokens[$i - 1]['name'], (array) $param['before'])) {
continue;
}
}
if (isset($param['after']) && $i + 1 < count($tokens)) {
if (!in_array($tokens[$i + 1]['name'], (array) $param['after'])) {
continue;
}
}
$results[] = isset($token[$options['return']]) ? $token[$options['return']] : $token;
}
return $results;
}