Passed
Push — master ( def5ca...29125e )
by Viacheslav
05:46
created

InvalidValue::getFailedSubSchema()   A

Complexity

Conditions 6
Paths 6

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 6

Importance

Changes 0
Metric Value
eloc 10
dl 0
loc 18
ccs 11
cts 11
cp 1
rs 9.2222
c 0
b 0
f 0
cc 6
nc 6
nop 1
crap 6
1
<?php
2
3
namespace Swaggest\JsonSchema;
4
5
use Swaggest\JsonDiff\JsonPointer;
6
use Swaggest\JsonSchema\Exception\Error;
7
use Swaggest\JsonSchema\Exception\LogicException;
8
use Swaggest\JsonSchema\Path\PointerUtil;
9
use Swaggest\JsonSchema\Structure\ObjectItemContract;
10
11
class InvalidValue extends Exception
12
{
13
    public $error;
14
    public $path;
15
16 1264
    public function addPath($path)
17
    {
18 1264
        if ($this->error === null) {
19 1248
            $this->error = $this->message;
20
        }
21 1264
        $this->path = $path;
22 1264
        if ('#' !== $this->path) {
23 792
            $this->message .= ' at ' . $path;
24
        }
25 1264
    }
26
27
    const INVALID_VALUE = 1;
28
    const NOT_IMPLEMENTED = 2;
29
30
31 3
    public function inspect()
32
    {
33 3
        $error = new Error();
34 3
        $error->error = $this->error;
35 3
        $error->processingPath = $this->path;
36 3
        $error->dataPointer = PointerUtil::getDataPointer($error->processingPath);
37 3
        $error->schemaPointers = PointerUtil::getSchemaPointers($error->processingPath);
38 3
        if ($this instanceof LogicException) {
39 2
            if ($this->subErrors !== null) {
40 1
                foreach ($this->subErrors as $subError) {
41 1
                    $error->subErrors[] = $subError->inspect();
42
                }
43
            }
44
        }
45 3
        return $error;
46
    }
47
48 2
    public function getSchemaPointer()
49
    {
50 2
        return PointerUtil::getSchemaPointer($this->path);
51
    }
52
53
    /**
54
     * @param Schema $schema
55
     * @return bool|Schema
56
     * @throws \Swaggest\JsonDiff\Exception
57
     */
58 2
    public function getFailedSubSchema(Schema $schema)
59
    {
60 2
        $schemaPointer = $this->getSchemaPointer();
61 2
        if ($schema instanceof ObjectItemContract) {
0 ignored issues
show
introduced by
$schema is always a sub-type of Swaggest\JsonSchema\Structure\ObjectItemContract.
Loading history...
62 2
            $refs = $schema->getFromRefs();
63 2
            if ($refs !== null) {
64 1
                foreach ($refs as $ref) {
65 1
                    if (substr($schemaPointer, 0, strlen($ref)) === $ref) {
66 1
                        $schemaPointer = substr($schemaPointer, strlen($ref));
67
                    }
68
                }
69
            }
70
        }
71 2
        if (!(bool)$schemaPointer) {
72 1
            return $schema;
73
        }
74
75 1
        return JsonPointer::getByPointer($schema, $this->getSchemaPointer());
76
    }
77
78
79 1
    public function getDataPointer()
80
    {
81 1
        return PointerUtil::getDataPointer($this->path);
82
    }
83
84
}