1 | <?php |
||
17 | class Validator implements ValidatorInterface |
||
18 | { |
||
19 | /** |
||
20 | * @var string JSON schema location |
||
21 | */ |
||
22 | private $schema; |
||
23 | /** |
||
24 | * @var Validator Validator |
||
25 | */ |
||
26 | private $validator; |
||
27 | |||
28 | /** |
||
29 | * Constructor |
||
30 | * |
||
31 | * @param Validator $validator Validator |
||
32 | * @param string $schema JSON schema |
||
33 | */ |
||
34 | 4 | public function __construct($validator, $schema) |
|
39 | |||
40 | /** |
||
41 | * Validate raw JSON definition |
||
42 | * |
||
43 | * @param string $json JSON definition |
||
44 | * @return ValidationExceptionError[] |
||
45 | * @throws InvalidJsonException If JSON is not valid |
||
46 | */ |
||
47 | 4 | public function validateJsonDefinition($json) |
|
48 | { |
||
49 | 4 | $json = json_decode($json); |
|
50 | 4 | if (json_last_error() !== JSON_ERROR_NONE) { |
|
51 | 1 | throw new InvalidJsonException(sprintf('Malformed JSON: %s', $this->getJsonLastErrorMessage())); |
|
52 | } |
||
53 | 3 | if (!is_object($json)) { |
|
54 | 1 | throw new InvalidJsonException('JSON value must be an object'); |
|
55 | } |
||
56 | |||
57 | 2 | return $this->validate($json, $this->schema); |
|
58 | } |
||
59 | |||
60 | /** |
||
61 | * validate a json structure with a schema |
||
62 | * |
||
63 | * @param object $json the json |
||
64 | * @param object $schema the schema |
||
65 | * |
||
66 | * @return ValidationExceptionError[] errors |
||
67 | */ |
||
68 | 2 | public function validate($json, $schema) |
|
69 | { |
||
70 | 2 | $this->validator->reset(); |
|
71 | 2 | if (is_string($schema)) { |
|
72 | $this->validator->validate($json, (object) ['$ref' => $schema]); |
||
73 | } else { |
||
74 | 2 | $this->validator->validate($json, $schema); |
|
75 | } |
||
76 | |||
77 | 2 | if ($this->validator->isValid()) { |
|
78 | 1 | return []; |
|
79 | } |
||
80 | |||
81 | 1 | return $this->getErrors(); |
|
82 | } |
||
83 | |||
84 | /** |
||
85 | * Wraps the array exception in our own class |
||
86 | * |
||
87 | * @return ValidationExceptionError[] |
||
88 | */ |
||
89 | 1 | public function getErrors() |
|
97 | |||
98 | /** |
||
99 | * Get JSON decode last error message |
||
100 | * |
||
101 | * @return string |
||
102 | */ |
||
103 | 1 | private function getJsonLastErrorMessage() |
|
119 | } |
||
120 |
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.