Passed
Pull Request — master (#661)
by
unknown
05:46
created

JsonValidator::validate()   A

Complexity

Conditions 3
Paths 9

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 11
nc 9
nop 1
dl 0
loc 16
rs 9.9
c 0
b 0
f 0
1
<?php
2
3
namespace OldSound\RabbitMqBundle\RabbitMq\Validator;
4
5
use Swaggest\JsonSchema\Context;
6
use Swaggest\JsonSchema\Schema;
7
use Swaggest\JsonSchema\Exception;
8
use Swaggest\JsonSchema\RemoteRef\Preloaded;
9
10
class JsonValidator implements ValidatorInterface
11
{
12
    private $definitions = null;
13
    private $schema = null;
14
    private $schema_url = null;
15
16
    public function setSchema($schema, $additionalProperties = array()) {
17
        $this->schema = $schema;
18
        if(isset($additionalProperties['definitions']) && isset($additionalProperties['schema_url'])){
19
            $this->definitions = $additionalProperties['definitions'];
20
            $this->schema_url = $additionalProperties['schema_url'];
21
        }
22
    }
23
24
25
    public function validate($msg)
26
    {
27
        try{
28
            $options = new Context();
29
30
            if ($this->definitions != null) {
31
                $refProvider = new Preloaded();
32
                $refProvider->setSchemaData($this->schema_url, json_decode(file_get_contents($this->definitions, true)));
33
                $options->remoteRefProvider = $refProvider;
34
            }
35
36
            $schema = Schema::import(json_decode(file_get_contents($this->schema, true)), $options);
37
            $schema->in(json_decode($msg));
38
            return null;
39
        }catch (Exception $e){
40
            return $e->getMessage();
41
        }
42
    }
43
44
    public function getContentType() {
45
        return "application/json";
46
    }
47
}
48