Completed
Pull Request — master (#55)
by Michal
06:03
created

JsonOutput::validate()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
c 0
b 0
f 0
rs 9.8333
cc 3
nc 3
nop 1
1
<?php
2
3
namespace Tomaj\NetteApi\Output;
4
5
use JsonSchema\Validator;
6
use Tomaj\NetteApi\Response\JsonApiResponse;
7
8
class JsonOutput implements OutputInterface
9
{
10
    private $schemaValidator;
11
12
    private $code;
13
14
    private $schema;
15
16
    private $description;
17
18
    public function __construct($code, $schema, $description = null)
19
    {
20
        $this->schemaValidator = new Validator();
21
        $this->code = $code;
22
        $this->schema = $schema;
23
        $this->description = $description;
24
    }
25
26
    public function validate($response)
27
    {
28
        if (!$response instanceof JsonApiResponse) {
29
            return false;
30
        }
31
32
        if ($this->code !== $response->getCode()) {
33
            return false;
34
        }
35
        $value = json_decode(json_encode($response->getPayload()));
36
        $this->schemaValidator->validate($value, json_decode($this->schema));
37
        return $this->schemaValidator->isValid();
38
    }
39
}
40