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

JsonValidator::setSchema()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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