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

JMESPathSearchProvider::setUp()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 2.0023

Importance

Changes 0
Metric Value
dl 0
loc 15
ccs 11
cts 12
cp 0.9167
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 9
nc 2
nop 3
crap 2.0023
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
}