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/** 23 * A parser for function macros like "{{ITEM.VALUE}.func()}". 24 */ 25class CMacroFunctionParser extends CParser { 26 27 /** 28 * Parser for generic macros. 29 * 30 * @var CMacroParser 31 */ 32 private $macro_parser; 33 34 /** 35 * Parser for trigger functions. 36 * 37 * @var CFunctionParser 38 */ 39 private $function_parser; 40 41 /** 42 * @param array $macros The list of macros, for example ['{ITEM.VALUE}', '{ITEM.LASTVALUE}']. 43 * @param array $options 44 */ 45 public function __construct(array $macros, array $options = []) { 46 $this->macro_parser = new CMacroParser($macros, $options); 47 $this->function_parser = new CFunctionParser(); 48 } 49 50 /** 51 * @param string $source 52 * @param int $pos 53 * 54 * @return int 55 */ 56 public function parse($source, $pos = 0) { 57 $this->length = 0; 58 $this->match = ''; 59 60 $p = $pos; 61 62 if (!isset($source[$p]) || $source[$p] !== '{') { 63 return self::PARSE_FAIL; 64 } 65 $p++; 66 67 if ($this->macro_parser->parse($source, $p) == CParser::PARSE_FAIL) { 68 return self::PARSE_FAIL; 69 } 70 $p += $this->macro_parser->getLength(); 71 72 if (!isset($source[$p]) || $source[$p] !== '.') { 73 return self::PARSE_FAIL; 74 } 75 $p++; 76 77 if ($this->function_parser->parse($source, $p) == CParser::PARSE_FAIL) { 78 return self::PARSE_FAIL; 79 } 80 $p += $this->function_parser->getLength(); 81 82 if (!isset($source[$p]) || $source[$p] !== '}') { 83 return self::PARSE_FAIL; 84 } 85 $p++; 86 87 $this->length = $p - $pos; 88 $this->match = substr($source, $pos, $this->length); 89 90 return (isset($source[$pos + $this->length]) ? self::PARSE_SUCCESS_CONT : self::PARSE_SUCCESS); 91 } 92 93 /** 94 * Returns macro parser. 95 * 96 * @return string 97 */ 98 public function getMacroParser() { 99 return $this->macro_parser; 100 } 101 102 /** 103 * Returns function parser. 104 * 105 * @return string 106 */ 107 public function getFunctionParser() { 108 return $this->function_parser; 109 } 110} 111