Completed
Pull Request — master (#1)
by Guillaume
07:09
created

JsonValidator::check()   A

Complexity

Conditions 2
Paths 3

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 7
nc 3
nop 2
dl 0
loc 11
rs 9.4285
c 1
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of the hogosha-monitor package
5
 *
6
 * Copyright (c) 2016 Guillaume Cavana
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 *
11
 * Feel free to edit as you please, and have fun.
12
 *
13
 * @author Guillaume Cavana <[email protected]>
14
 */
15
16
namespace Hogosha\Monitor\Validator;
17
18
use Hogosha\Monitor\Exception\ValidatorException;
19
use Symfony\Component\PropertyAccess\Exception\RuntimeException;
20
use Symfony\Component\PropertyAccess\PropertyAccessor;
21
22
/**
23
 * Class JsonValidator.
24
 */
25
class JsonValidator implements ValidatorInterface
26
{
27
    /**
28
     * {@inheritdoc}
29
     */
30
    public function check($value, $match)
31
    {
32
        $accessor = new PropertyAccessor();
33
34
        try {
35
            $json = $this->decode($value);
36
            $accessor->getValue($json, $match);
37
        } catch (RuntimeException $e) {
38
            throw new ValidatorException($e->getMessage());
39
        }
40
    }
41
42
    private function decode($content)
43
    {
44
        $result = json_decode($content);
45
        if (json_last_error() !== JSON_ERROR_NONE) {
46
            throw new ValidatorException('This json is not valid');
47
        }
48
49
        return $result;
50
    }
51
}
52