Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 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
    private $cachedCount = 0;
13
 
14
    public function __construct(
15
        Parser $parser = null,
16
        callable $fnDispatcher = null
17
    ) {
18
        $fnDispatcher = $fnDispatcher ?: FnDispatcher::getInstance();
19
        $this->interpreter = new TreeInterpreter($fnDispatcher);
20
        $this->parser = $parser ?: new Parser();
21
    }
22
 
23
    /**
24
     * Returns data from the provided input that matches a given JMESPath
25
     * expression.
26
     *
27
     * @param string $expression JMESPath expression to evaluate
28
     * @param mixed  $data       Data to search. This data should be data that
29
     *                           is similar to data returned from json_decode
30
     *                           using associative arrays rather than objects.
31
     *
32
     * @return mixed Returns the matching data or null
33
     */
34
    public function __invoke($expression, $data)
35
    {
36
        if (!isset($this->cache[$expression])) {
37
            // Clear the AST cache when it hits 1024 entries
38
            if (++$this->cachedCount > 1024) {
39
                $this->cache = [];
40
                $this->cachedCount = 0;
41
            }
42
            $this->cache[$expression] = $this->parser->parse($expression);
43
        }
44
 
45
        return $this->interpreter->visit($this->cache[$expression], $data);
46
    }
47
}