tokens; } /** * Add a token to the result. * * @param CParserResult $token */ public function addTokens(array $tokens): void { $this->tokens = array_merge($this->tokens, $tokens); } /** * Auxiliary method for getTokensOfTypes(). * * @param array $tokens * @param array $types * * @return array */ private static function _getTokensOfTypes(array $tokens, array $types) { $result = []; foreach ($tokens as $token) { if (in_array($token['type'], $types)) { $result[] = $token; } if ($token['type'] == CExpressionParserResult::TOKEN_TYPE_EXPRESSION) { $result = array_merge($result, self::_getTokensOfTypes($token['data']['tokens'], $types)); } elseif ($token['type'] == CExpressionParserResult::TOKEN_TYPE_MATH_FUNCTION) { foreach ($token['data']['parameters'] as $parameter) { $result = array_merge($result, self::_getTokensOfTypes($parameter['data']['tokens'], $types)); } } } return $result; } /** * Returns all tokens include nested of the given types. * * @param array $types * * @return array */ public function getTokensOfTypes(array $types): array { return self::_getTokensOfTypes($this->tokens, $types); } /** * Return list hosts found in parsed trigger expression. * * @return array */ public function getHosts(): array { $hist_functions = $this->getTokensOfTypes([CExpressionParserResult::TOKEN_TYPE_HIST_FUNCTION]); $hosts = []; foreach ($hist_functions as $hist_function) { $host = $hist_function['data']['parameters'][0]['data']['host']; $hosts[$host] = $host; } return array_values($hosts); } }