Completed
Pull Request — master (#126)
by
unknown
01:29
created

InvalidSchemaException::invalidParameter()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 4
dl 0
loc 10
ccs 5
cts 5
cp 1
crap 1
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace League\JsonGuard\Exception;
4
5
final class InvalidSchemaException extends \RuntimeException
6
{
7
    /**
8
     * @var string
9
     */
10
    private $keyword;
11
12
    /**
13
     * @var string
14
     */
15
    private $pointer;
16
17
    /**
18
     * @param string $message
19
     * @param string $keyword
20
     * @param string $pointer
21
     */
22 86
    public function __construct($message, $keyword, $pointer)
23
    {
24 86
        parent::__construct($message);
25
26 86
        $this->keyword = $keyword;
27 86
        $this->pointer = $pointer;
28 86
    }
29
30
    /**
31
     * @param string $actualType
32
     * @param array  $allowedTypes
33
     * @param string $keyword
34
     * @param string $pointer
35
     *
36
     * @return \League\JsonGuard\Exception\InvalidSchemaException
37
     */
38 58
    public static function invalidParameterType($actualType, array $allowedTypes, $keyword, $pointer)
39
    {
40 58
        $message = sprintf(
41 58
            'Value has type "%s" but must be one of: "%s"',
42 58
            $actualType,
43 58
            implode(', ', $allowedTypes)
44 29
        );
45
46 58
        return new self($message, $keyword, $pointer);
47
    }
48
49
    /**
50
     * @param string $actualParameter
51
     * @param array  $allowedParameter
52
     * @param string $keyword
53
     * @param string $pointer
54
     *
55
     * @return \League\JsonGuard\Exception\InvalidSchemaException
56 18
     */
57
    public static function invalidParameter($actualParameter, array $allowedParameter, $keyword, $pointer)
58 18
    {
59 18
        $message = sprintf(
60 9
            'Value is "%s" but must be one of: "%s"',
61 9
            $actualParameter,
62
            implode(', ', $allowedParameter)
63 18
        );
64
65
        return new self($message, $keyword, $pointer);
66
    }
67
68
    /**
69
     * @param integer $value
70
     * @param string  $keyword
71
     * @param string  $pointer
72 8
     *
73
     * @return \League\JsonGuard\Exception\InvalidSchemaException
74 8
     */
75 8
    public static function negativeValue($value, $keyword, $pointer)
76 8
    {
77 4
        $message = sprintf(
78 4
            'Integer value "%d" must be greater than, or equal to, 0',
79
            $value
80
        );
81
82
        return new self($message, $keyword, $pointer);
83
    }
84
85
    /**
86
     * @param string $keyword
87
     * @param string $pointer
88
     *
89
     * @return \League\JsonGuard\Exception\InvalidSchemaException
90
     */
91
    public static function emptyArray($keyword, $pointer)
92
    {
93
        return new self(
94
            'Array must have at least one element',
95
            $keyword,
96
            $pointer
97
        );
98
    }
99
100
    /**
101 8
     * @param string $property
102
     * @param string $keyword
103 8
     * @param string $pointer
104
     *
105
     * @return \League\JsonGuard\Exception\InvalidSchemaException
106
     */
107
    public static function missingProperty($property, $keyword, $pointer)
108
    {
109 8
        $message = sprintf(
110
            'The schema must contain the property %s',
111 8
            $property
112
        );
113
114
        return new self($message, $keyword, $pointer);
115
    }
116
117
    /**
118
     * @return string
119
     */
120
    public function getKeyword()
121
    {
122
        return $this->keyword;
123
    }
124
125
    /**
126
     * @return string
127
     */
128
    public function getPointer()
129
    {
130
        return $this->pointer;
131
    }
132
}
133