1<?php
2/*
3** Zabbix
4** Copyright (C) 2001-2021 Zabbix SIA
5**
6** This program is free software; you can redistribute it and/or modify
7** it under the terms of the GNU General Public License as published by
8** the Free Software Foundation; either version 2 of the License, or
9** (at your option) any later version.
10**
11** This program is distributed in the hope that it will be useful,
12** but WITHOUT ANY WARRANTY; without even the implied warranty of
13** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14** GNU General Public License for more details.
15**
16** You should have received a copy of the GNU General Public License
17** along with this program; if not, write to the Free Software
18** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
19**/
20
21/**
22 * Class for storing the result returned by the trigger expression parser.
23 */
24class CTriggerExprParserResult extends CParserResult {
25
26	const TOKEN_TYPE_OPEN_BRACE = 0;
27	const TOKEN_TYPE_CLOSE_BRACE = 1;
28	const TOKEN_TYPE_OPERATOR = 2;
29	const TOKEN_TYPE_NUMBER = 3;
30	const TOKEN_TYPE_FUNCTION_MACRO = 4;
31	const TOKEN_TYPE_MACRO = 5;
32	const TOKEN_TYPE_USER_MACRO = 6;
33	const TOKEN_TYPE_LLD_MACRO = 7;
34	const TOKEN_TYPE_STRING = 8;
35	const TOKEN_TYPE_FUNCTIONID_MACRO = 9;
36	const TOKEN_TYPE_FUNCTION = 10;
37
38	/**
39	 * Array of expression tokens.
40	 *
41	 * Each token contains the following values:
42	 * - type   - token type
43	 * - value  - the token string itself
44	 * - pos    - position of the token in the source string
45	 * - length - length of the token
46	 * - data   - an array containing additional information about the token
47	 *
48	 * The following "data" information can be available depending on the type of the token.
49	 * For self::TOKEN_TYPE_FUNCTION_MACRO tokens:
50	 * - host           - host name
51	 * - item           - item key
52	 * - function       - the function string, e.g., "function(param1, param2)"
53	 * - functionName   - function name without parameters
54	 * - functionParams - array of function parameters
55	 * For self::TOKEN_TYPE_NUMBER tokens:
56	 * - suffix         - a time or byte suffix
57	 *
58	 * @var array
59	 */
60	protected $tokens = [];
61
62	/**
63	 * Return the expression tokens.
64	 *
65	 * @see CTriggerExprParserResult::$token    for the structure of a token array
66	 *
67	 * @return array
68	 */
69	public function getTokens() {
70		return $this->tokens;
71	}
72
73	/**
74	 * Add a token to the result.
75	 *
76	 * @param string        $type       token type
77	 * @param string        $value      token string
78	 * @param string        $pos        position of the token in the source string
79	 * @param string        $length     length of the token
80	 * @param array|null    $data       additional token information
81	 */
82	public function addToken($type, $value, $pos, $length, array $data = null) {
83		$this->tokens[] = [
84			'type' => $type,
85			'value' => $value,
86			'pos' => $pos,
87			'length' => $length,
88			'data' => $data
89		];
90	}
91
92	/**
93	 * Returns all tokens of the given type.
94	 *
95	 * @param $type
96	 *
97	 * @return array
98	 */
99	public function getTokensByType($type) {
100		$result = [];
101
102		foreach ($this->tokens as $token) {
103			if ($token['type'] == $type) {
104				$result[] = $token;
105			}
106		}
107
108		return $result;
109	}
110
111	/**
112	 * Check whether the expression contains at least one token of the given type.
113	 *
114	 * @param int $type
115	 *
116	 * @return bool
117	 */
118	public function hasTokenOfType($type) {
119		foreach ($this->tokens as $token) {
120			if ($token['type'] == $type) {
121				return true;
122			}
123		}
124
125		return false;
126	}
127}
128