Completed
Pull Request — master (#74)
by Viacheslav
07:39
created

InvalidValue::getFailedSubSchema()   A

Complexity

Conditions 6
Paths 6

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

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