SchemaAssertions   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 4
dl 0
loc 40
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A setupSchemaAssertions() 0 4 1
A assertJsonMatchesSchema() 0 6 1
A normalizeSchemaPath() 0 8 3
A validatorPasses() 0 4 1
1
<?php
2
3
namespace HSkrasek\JsonSchema;
4
5
use HSkrasek\JsonSchema\Validator\Errors;
6
use League\JsonGuard\Validator;
7
use League\JsonReference\Dereferencer;
8
use PHPUnit\Framework\Constraint\Constraint;
9
10
trait SchemaAssertions
11
{
12
    /**
13
     * @var Dereferencer
14
     */
15
    private $dereferencer;
16
17
    public function setupSchemaAssertions()
18
    {
19
        $this->dereferencer = Dereferencer::draft4();
20
    }
21
22
    /**
23
     * @param mixed $json
24
     * @param string $schemaPath
25
     */
26
    public function assertJsonMatchesSchema($json, string $schemaPath): void
27
    {
28
        $validator = new Validator($json, $this->dereferencer->dereference($this->normalizeSchemaPath($schemaPath)));
29
30
        static::assertThat($validator, static::validatorPasses(), new Errors($validator->errors()));
31
    }
32
33
    private function normalizeSchemaPath(string $schemaPath): string
34
    {
35
        if (strpos($schemaPath, 'http://') === 0 || strpos($schemaPath, 'https://') === 0) {
36
            return $schemaPath;
37
        }
38
39
        return "file://$schemaPath";
40
    }
41
42
    /**
43
     * @return ValidatorPasses
44
     */
45
    private static function validatorPasses(): Constraint
46
    {
47
        return new ValidatorPasses;
48
    }
49
}
50