Passed
Pull Request — master (#661)
by
unknown
10:11
created

JsonValidator::setSchema()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 3
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace OldSound\RabbitMqBundle\RabbitMq;
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
    public $definitions = null;
13
    public $schema = null;
14
    public $schema_url = null;
15
16
    public function setSchema($schema, $schema_url=null, $definitions=null) {
17
        $this->definitions = $definitions;
18
        $this->schema = $schema;
19
        $this->schema_url = $schema_url;
20
    }
21
22
23
    public function validate($msg)
24
    {
25
        try{
26
            $options = new Context();
27
28
            if ($this->definitions != null) {
29
                $refProvider = new Preloaded();
30
                $refProvider->setSchemaData($this->schema_url, json_decode(file_get_contents($this->definitions, true)));
31
                $options->remoteRefProvider = $refProvider;
32
            }
33
34
            $schema = Schema::import(json_decode(file_get_contents($this->schema, true)), $options);
35
            $schema->in($msg);
36
            return null;
37
        }catch (Exception $e){
38
            return $e->getMessage();
39
        }
40
    }
41
42
    public function getContentType() {
43
        return "application/json";
44
    }
45
}
46