JsonParser   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 8
dl 0
loc 33
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A evaluate() 0 12 3
A validate() 0 4 1
1
<?php
2
3
namespace Rezzza\RestApiBehatExtension\Json;
4
5
use JsonSchema\Validator;
6
use JsonSchema\SchemaStorage;
7
use JsonSchema\Uri\UriRetriever;
8
use JsonSchema\Uri\UriResolver;
9
use Symfony\Component\PropertyAccess\PropertyAccess;
10
11
class JsonParser
12
{
13
    private $evaluationMode;
14
15
    private $propertyAccessor;
16
17
    public function __construct($evaluationMode)
18
    {
19
        $this->evaluationMode = $evaluationMode;
20
        $this->propertyAccessor = PropertyAccess::createPropertyAccessorBuilder()
21
            ->enableExceptionOnInvalidIndex()
22
            ->getPropertyAccessor()
23
        ;
24
    }
25
26
    public function evaluate(Json $json, $expression)
27
    {
28
        if ($this->evaluationMode === 'javascript') {
29
            $expression = str_replace('->', '.', $expression);
30
        }
31
32
        try {
33
            return $json->read($expression, $this->propertyAccessor);
34
        } catch (\Exception $e) {
35
            throw new \Exception(sprintf('Failed to evaluate expression "%s"', $expression), 0, $e);
36
        }
37
    }
38
39
    public function validate(Json $json, JsonSchema $schema)
40
    {
41
        return $schema->validate($json, new Validator, new SchemaStorage(new UriRetriever, new UriResolver));
42
    }
43
}
44