Debian Codegraph @

lib/3rdParty/composer/mtdowling/jmespath.php/src/AstRuntime.php

raw symbols SCIP
1 <?php
2 namespace JmesPath;
3
4 /**
5 * Uses an external tree visitor to interpret an AST.
6 */
7 class AstRuntime
8 {
9 private $parser;
10 private $interpreter;
11 private $cache = [];
12
13 public function __construct(
14 ?Parser $parser = null,
15 ?callable $fnDispatcher = null
16 ) {
17 $fnDispatcher = $fnDispatcher ?: FnDispatcher::getInstance();
18 $this->interpreter = new TreeInterpreter($fnDispatcher);
19 $this->parser = $parser ?: new Parser();
20 }
21
22 /**
23 * Returns data from the provided input that matches a given JMESPath
24 * expression.
25 *
26 * @param string $expression JMESPath expression to evaluate
27 * @param mixed $data Data to search. This data should be data that
28 * is similar to data returned from json_decode
29 * using associative arrays rather than objects.
30 *
31 * @return mixed Returns the matching data or null
32 */
33 public function __invoke($expression, $data)
34 {
35 if (!isset($this->cache[$expression])) {
36 // Clear the AST cache when it already holds 1024 entries.
37 if (count($this->cache) >= 1024) {
38 $this->cache = [];
39 }
40 $this->cache[$expression] = $this->parser->parse($expression);
41 }
42
43 return $this->interpreter->visit($this->cache[$expression], $data);
44 }
45 }