|
1
|
|
|
<?php namespace Limoncello\Validation\Execution; |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Copyright 2015-2017 [email protected] |
|
5
|
|
|
* |
|
6
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
7
|
|
|
* you may not use this file except in compliance with the License. |
|
8
|
|
|
* You may obtain a copy of the License at |
|
9
|
|
|
* |
|
10
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
|
11
|
|
|
* |
|
12
|
|
|
* Unless required by applicable law or agreed to in writing, software |
|
13
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
|
14
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
15
|
|
|
* See the License for the specific language governing permissions and |
|
16
|
|
|
* limitations under the License. |
|
17
|
|
|
*/ |
|
18
|
|
|
|
|
19
|
|
|
use Limoncello\Validation\Contracts\Execution\BlockPropertiesInterface; |
|
20
|
|
|
use Limoncello\Validation\Contracts\Execution\BlockStateInterface; |
|
21
|
|
|
use Limoncello\Validation\Contracts\Execution\ContextStorageInterface; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @package Limoncello\Validation |
|
25
|
|
|
*/ |
|
26
|
|
|
class ContextStorage implements ContextStorageInterface, BlockStateInterface, BlockPropertiesInterface |
|
27
|
|
|
{ |
|
28
|
|
|
/** |
|
29
|
|
|
* @var array |
|
30
|
|
|
*/ |
|
31
|
|
|
private $states = []; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @var int |
|
35
|
|
|
*/ |
|
36
|
|
|
private $currentBlockId = 0; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @var |
|
40
|
|
|
*/ |
|
41
|
|
|
private $blocks; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @param $blocks |
|
45
|
|
|
*/ |
|
46
|
|
|
public function __construct(array $blocks) |
|
47
|
|
|
{ |
|
48
|
|
|
$this->blocks = $blocks; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @inheritdoc |
|
53
|
|
|
*/ |
|
54
|
|
|
public function getStates(): BlockStateInterface |
|
55
|
|
|
{ |
|
56
|
|
|
return $this; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @inheritdoc |
|
61
|
|
|
*/ |
|
62
|
|
|
public function getProperties(): BlockPropertiesInterface |
|
63
|
|
|
{ |
|
64
|
|
|
return $this; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @inheritdoc |
|
69
|
|
|
*/ |
|
70
|
|
|
public function getCurrentBlockId(): int |
|
71
|
|
|
{ |
|
72
|
|
|
return $this->currentBlockId; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @inheritdoc |
|
77
|
|
|
*/ |
|
78
|
|
|
public function setCurrentBlockId(int $index): ContextStorageInterface |
|
79
|
|
|
{ |
|
80
|
|
|
$this->currentBlockId = $index; |
|
81
|
|
|
|
|
82
|
|
|
return $this; |
|
|
|
|
|
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* @inheritdoc |
|
87
|
|
|
*/ |
|
88
|
|
|
public function clear(): ContextStorageInterface |
|
89
|
|
|
{ |
|
90
|
|
|
$this->states = []; |
|
91
|
|
|
$this->setCurrentBlockId(0); |
|
92
|
|
|
|
|
93
|
|
|
return $this; |
|
|
|
|
|
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* @inheritdoc |
|
98
|
|
|
*/ |
|
99
|
|
|
public function getProperty(int $key, $default = null) |
|
100
|
|
|
{ |
|
101
|
|
|
return $this->blocks[$this->getCurrentBlockId()][BlockSerializer::PROPERTIES][$key] ?? $default; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* @inheritdoc |
|
106
|
|
|
*/ |
|
107
|
|
|
public function getState(int $key, $default = null) |
|
108
|
|
|
{ |
|
109
|
|
|
return $this->states[$this->getCurrentBlockId()][$key] ?? $default; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* @inheritdoc |
|
114
|
|
|
*/ |
|
115
|
|
|
public function setState(int $key, $value): BlockStateInterface |
|
116
|
|
|
{ |
|
117
|
|
|
$this->states[$this->getCurrentBlockId()][$key] = $value; |
|
118
|
|
|
|
|
119
|
|
|
return $this; |
|
|
|
|
|
|
120
|
|
|
} |
|
121
|
|
|
} |
|
122
|
|
|
|
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_functionexpects aPostobject, and outputs the author of the post. The base classPostreturns a simple string and outputting a simple string will work just fine. However, the child classBlogPostwhich is a sub-type ofPostinstead decided to return anobject, and is therefore violating the SOLID principles. If aBlogPostwere passed tomy_function, PHP would not complain, but ultimately fail when executing thestrtouppercall in its body.