1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace Limoncello\Validation\Execution; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Copyright 2015-2020 [email protected] |
7
|
|
|
* |
8
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
9
|
|
|
* you may not use this file except in compliance with the License. |
10
|
|
|
* You may obtain a copy of the License at |
11
|
|
|
* |
12
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
13
|
|
|
* |
14
|
|
|
* Unless required by applicable law or agreed to in writing, software |
15
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
16
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
17
|
|
|
* See the License for the specific language governing permissions and |
18
|
|
|
* limitations under the License. |
19
|
|
|
*/ |
20
|
|
|
|
21
|
|
|
use Limoncello\Validation\Contracts\Execution\BlockPropertiesInterface; |
22
|
|
|
use Limoncello\Validation\Contracts\Execution\BlockStateInterface; |
23
|
|
|
use Limoncello\Validation\Contracts\Execution\ContextStorageInterface; |
24
|
|
|
use Psr\Container\ContainerInterface; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @package Limoncello\Validation |
28
|
|
|
*/ |
29
|
|
|
class ContextStorage implements ContextStorageInterface, BlockStateInterface, BlockPropertiesInterface |
30
|
|
|
{ |
31
|
|
|
/** |
32
|
|
|
* @var array |
33
|
|
|
*/ |
34
|
|
|
private $states = []; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var int |
38
|
|
|
*/ |
39
|
|
|
private $currentBlockId = 0; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var |
43
|
|
|
*/ |
44
|
|
|
private $blocks; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var null|ContainerInterface |
48
|
|
|
*/ |
49
|
|
|
private $container; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @param array $blocks |
53
|
|
|
* @param ContainerInterface|null $container |
54
|
|
|
*/ |
55
|
21 |
|
public function __construct(array $blocks, ContainerInterface $container = null) |
56
|
|
|
{ |
57
|
21 |
|
$this->blocks = $blocks; |
58
|
21 |
|
$this->container = $container; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @inheritdoc |
63
|
|
|
*/ |
64
|
1 |
|
public function getStates(): BlockStateInterface |
65
|
|
|
{ |
66
|
1 |
|
return $this; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @inheritdoc |
71
|
|
|
*/ |
72
|
18 |
|
public function getProperties(): BlockPropertiesInterface |
73
|
|
|
{ |
74
|
18 |
|
return $this; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @inheritdoc |
79
|
|
|
*/ |
80
|
2 |
|
public function getContainer(): ?ContainerInterface |
81
|
|
|
{ |
82
|
2 |
|
return $this->container; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @inheritdoc |
87
|
|
|
*/ |
88
|
18 |
|
public function getCurrentBlockId(): int |
89
|
|
|
{ |
90
|
18 |
|
return $this->currentBlockId; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @inheritdoc |
95
|
|
|
*/ |
96
|
21 |
|
public function setCurrentBlockId(int $index): ContextStorageInterface |
97
|
|
|
{ |
98
|
21 |
|
$this->currentBlockId = $index; |
99
|
|
|
|
100
|
21 |
|
return $this; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @inheritdoc |
105
|
|
|
*/ |
106
|
1 |
|
public function clear(): ContextStorageInterface |
107
|
|
|
{ |
108
|
1 |
|
$this->states = []; |
109
|
1 |
|
$this->setCurrentBlockId(0); |
110
|
|
|
|
111
|
1 |
|
return $this; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @inheritdoc |
116
|
|
|
*/ |
117
|
18 |
|
public function getProperty(int $key, $default = null) |
118
|
|
|
{ |
119
|
18 |
|
return $this->blocks[$this->getCurrentBlockId()][BlockSerializer::PROPERTIES][$key] ?? $default; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @inheritdoc |
124
|
|
|
*/ |
125
|
1 |
|
public function getState(int $key, $default = null) |
126
|
|
|
{ |
127
|
1 |
|
return $this->states[$this->getCurrentBlockId()][$key] ?? $default; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @inheritdoc |
132
|
|
|
*/ |
133
|
1 |
|
public function setState(int $key, $value): BlockStateInterface |
134
|
|
|
{ |
135
|
1 |
|
$this->states[$this->getCurrentBlockId()][$key] = $value; |
136
|
|
|
|
137
|
1 |
|
return $this; |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|