mirror of
https://github.com/SociallyDev/Spaces-API.git
synced 2025-08-20 13:23:47 -07:00
spaces.php
This commit is contained in:
parent
7755490b81
commit
eefa32741e
845 changed files with 50409 additions and 0 deletions
47
aws/JmesPath/AstRuntime.php
Normal file
47
aws/JmesPath/AstRuntime.php
Normal file
|
@ -0,0 +1,47 @@
|
|||
<?php
|
||||
namespace JmesPath;
|
||||
|
||||
/**
|
||||
* Uses an external tree visitor to interpret an AST.
|
||||
*/
|
||||
class AstRuntime
|
||||
{
|
||||
private $parser;
|
||||
private $interpreter;
|
||||
private $cache = [];
|
||||
private $cachedCount = 0;
|
||||
|
||||
public function __construct(
|
||||
Parser $parser = null,
|
||||
callable $fnDispatcher = null
|
||||
) {
|
||||
$fnDispatcher = $fnDispatcher ?: FnDispatcher::getInstance();
|
||||
$this->interpreter = new TreeInterpreter($fnDispatcher);
|
||||
$this->parser = $parser ?: new Parser();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns data from the provided input that matches a given JMESPath
|
||||
* expression.
|
||||
*
|
||||
* @param string $expression JMESPath expression to evaluate
|
||||
* @param mixed $data Data to search. This data should be data that
|
||||
* is similar to data returned from json_decode
|
||||
* using associative arrays rather than objects.
|
||||
*
|
||||
* @return mixed|null Returns the matching data or null
|
||||
*/
|
||||
public function __invoke($expression, $data)
|
||||
{
|
||||
if (!isset($this->cache[$expression])) {
|
||||
// Clear the AST cache when it hits 1024 entries
|
||||
if (++$this->cachedCount > 1024) {
|
||||
$this->cache = [];
|
||||
$this->cachedCount = 0;
|
||||
}
|
||||
$this->cache[$expression] = $this->parser->parse($expression);
|
||||
}
|
||||
|
||||
return $this->interpreter->visit($this->cache[$expression], $data);
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue