Passed
Pull Request — master (#9)
by Rafael
03:29
created

JMESPathSearchProvider   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 17
Duplicated Lines 0 %

Test Coverage

Coverage 91.67%

Importance

Changes 0
Metric Value
wmc 2
dl 0
loc 17
ccs 11
cts 12
cp 0.9167
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 15 2
1
<?php
2
/*******************************************************************************
3
 *  This file is part of the GraphQL Bundle package.
4
 *
5
 *  (c) YnloUltratech <[email protected]>
6
 *
7
 *  For the full copyright and license information, please view the LICENSE
8
 *  file that was distributed with this source code.
9
 ******************************************************************************/
10
11
namespace Ynlo\GraphQLBundle\Behat\Transformer\ExpressionLanguage;
12
13
use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
14
use function JmesPath\search;
15
16
/**
17
 * Add support to search using JMESPath expressions
18
 *
19
 * @require mtdowling/jmespath.php
20
 *
21
 * @example "{ search('data.items[*].title', response) }" => ["Title1", "Title2", "Title3"]
22
 *
23
 * @see     http://jmespath.org/examples.html
24
 * @see     http://jmespath.org/tutorial.html
25
 */
26
class JMESPathSearchProvider implements ExpressionPreprocessorInterface
27
{
28 1
    public function setUp(ExpressionLanguage $el, string &$expression, array &$values)
29
    {
30 1
        if (preg_match('/search\(/', $expression)) {
31 1
            $el->register(
32 1
                'search',
33 1
                function ($expression, $data) {
34
                    return sprintf('$search(%s, %s)', $expression, $data);
35 1
                },
36 1
                function (array $variables, $expression, $data) {
37 1
                    return $variables['search']($expression, $data);
38 1
                }
39
            );
40
41 1
            $values['search'] = function ($expression, $data) {
42 1
                return search($expression, $data);
43
            };
44
        }
45
    }
46
}