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
|
|
|
use Psr\Container\ContainerInterface; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @package Limoncello\Validation |
26
|
|
|
*/ |
27
|
|
|
class ContextStorage implements ContextStorageInterface, BlockStateInterface, BlockPropertiesInterface |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* @var array |
31
|
|
|
*/ |
32
|
|
|
private $states = []; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var int |
36
|
|
|
*/ |
37
|
|
|
private $currentBlockId = 0; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var |
41
|
|
|
*/ |
42
|
|
|
private $blocks; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var null|ContainerInterface |
46
|
|
|
*/ |
47
|
|
|
private $container; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @param array $blocks |
51
|
|
|
* @param ContainerInterface|null $container |
52
|
|
|
*/ |
53
|
18 |
|
public function __construct(array $blocks, ContainerInterface $container = null) |
54
|
|
|
{ |
55
|
18 |
|
$this->blocks = $blocks; |
56
|
18 |
|
$this->container = $container; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @inheritdoc |
61
|
|
|
*/ |
62
|
1 |
|
public function getStates(): BlockStateInterface |
63
|
|
|
{ |
64
|
1 |
|
return $this; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @inheritdoc |
69
|
|
|
*/ |
70
|
16 |
|
public function getProperties(): BlockPropertiesInterface |
71
|
|
|
{ |
72
|
16 |
|
return $this; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @inheritdoc |
77
|
|
|
*/ |
78
|
1 |
|
public function getContainer(): ?ContainerInterface |
79
|
|
|
{ |
80
|
1 |
|
return $this->container; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @inheritdoc |
85
|
|
|
*/ |
86
|
16 |
|
public function getCurrentBlockId(): int |
87
|
|
|
{ |
88
|
16 |
|
return $this->currentBlockId; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @inheritdoc |
93
|
|
|
*/ |
94
|
18 |
|
public function setCurrentBlockId(int $index): ContextStorageInterface |
95
|
|
|
{ |
96
|
18 |
|
$this->currentBlockId = $index; |
97
|
|
|
|
98
|
18 |
|
return $this; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @inheritdoc |
103
|
|
|
*/ |
104
|
1 |
|
public function clear(): ContextStorageInterface |
105
|
|
|
{ |
106
|
1 |
|
$this->states = []; |
107
|
1 |
|
$this->setCurrentBlockId(0); |
108
|
|
|
|
109
|
1 |
|
return $this; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @inheritdoc |
114
|
|
|
*/ |
115
|
16 |
|
public function getProperty(int $key, $default = null) |
116
|
|
|
{ |
117
|
16 |
|
return $this->blocks[$this->getCurrentBlockId()][BlockSerializer::PROPERTIES][$key] ?? $default; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @inheritdoc |
122
|
|
|
*/ |
123
|
1 |
|
public function getState(int $key, $default = null) |
124
|
|
|
{ |
125
|
1 |
|
return $this->states[$this->getCurrentBlockId()][$key] ?? $default; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @inheritdoc |
130
|
|
|
*/ |
131
|
1 |
|
public function setState(int $key, $value): BlockStateInterface |
132
|
|
|
{ |
133
|
1 |
|
$this->states[$this->getCurrentBlockId()][$key] = $value; |
134
|
|
|
|
135
|
1 |
|
return $this; |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|