|
1
|
|
|
<?php namespace Limoncello\Flute\Validation\Form\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\Flute\Contracts\Validation\FormRulesInterface; |
|
20
|
|
|
use Limoncello\Flute\Contracts\Validation\FormRulesSerializerInterface; |
|
21
|
|
|
use Limoncello\Flute\Validation\Serialize\RulesSerializer; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @package Limoncello\Flute |
|
25
|
|
|
* |
|
26
|
|
|
* @SuppressWarnings(PHPMD.TooManyPublicMethods) |
|
27
|
|
|
*/ |
|
28
|
|
|
class FormRulesSerializer extends RulesSerializer implements FormRulesSerializerInterface |
|
29
|
|
|
{ |
|
30
|
|
|
/** |
|
31
|
|
|
* @var array |
|
32
|
|
|
*/ |
|
33
|
|
|
private $serializedRules = []; |
|
34
|
|
|
|
|
35
|
|
|
/** Serialized indexes key */ |
|
36
|
|
|
protected const SERIALIZED_RULES = 0; |
|
37
|
|
|
|
|
38
|
|
|
/** Serialized rules key */ |
|
39
|
|
|
protected const SERIALIZED_BLOCKS = self::SERIALIZED_RULES + 1; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @inheritdoc |
|
43
|
|
|
*/ |
|
44
|
30 |
|
public function addRulesFromClass(string $rulesClass): FormRulesSerializerInterface |
|
45
|
|
|
{ |
|
46
|
30 |
|
assert(static::isRulesClass($rulesClass)); |
|
|
|
|
|
|
47
|
|
|
|
|
48
|
30 |
|
$name = $rulesClass; |
|
49
|
|
|
|
|
50
|
|
|
/** @var FormRulesInterface $rulesClass */ |
|
51
|
|
|
|
|
52
|
30 |
|
return $this->addFormRules($name, $rulesClass::getAttributeRules()); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @inheritdoc |
|
57
|
|
|
*/ |
|
58
|
30 |
|
public function addFormRules(string $name, ?array $attributeRules): FormRulesSerializerInterface |
|
59
|
|
|
{ |
|
60
|
30 |
|
assert(!empty($name)); |
|
61
|
30 |
|
assert(static::hasRules($name, $this->serializedRules) === false); |
|
62
|
|
|
|
|
63
|
30 |
|
$this->serializedRules[$name] = $attributeRules === null ? null : $this->addRules($attributeRules); |
|
64
|
|
|
|
|
65
|
30 |
|
return $this; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* @inheritdoc |
|
70
|
|
|
*/ |
|
71
|
30 |
|
public function getData(): array |
|
72
|
|
|
{ |
|
73
|
|
|
return [ |
|
74
|
30 |
|
static::SERIALIZED_RULES => $this->serializedRules, |
|
75
|
30 |
|
static::SERIALIZED_BLOCKS => $this->getBlocks(), |
|
76
|
|
|
]; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @inheritdoc |
|
81
|
|
|
*/ |
|
82
|
3 |
|
public static function readBlocks(array $serializedData): array |
|
83
|
|
|
{ |
|
84
|
3 |
|
return $serializedData[static::SERIALIZED_BLOCKS]; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* @inheritdoc |
|
89
|
|
|
*/ |
|
90
|
30 |
|
public static function hasRules(string $name, array $serializedData): bool |
|
91
|
|
|
{ |
|
92
|
|
|
// the value could be null so we have to check by key existence. |
|
93
|
|
|
return |
|
94
|
30 |
|
array_key_exists(static::SERIALIZED_RULES, $serializedData) === true && |
|
95
|
30 |
|
array_key_exists($name, $serializedData[static::SERIALIZED_RULES]); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* @inheritdoc |
|
100
|
|
|
*/ |
|
101
|
3 |
|
public static function readRules(string $rulesClass, array $serializedData): array |
|
102
|
|
|
{ |
|
103
|
3 |
|
assert(static::hasRules($rulesClass, $serializedData) === true); |
|
104
|
|
|
|
|
105
|
3 |
|
return $serializedData[static::SERIALIZED_RULES][$rulesClass]; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* @inheritdoc |
|
110
|
|
|
*/ |
|
111
|
3 |
|
public static function readRuleMainIndexes(array $ruleIndexes): ?array |
|
112
|
|
|
{ |
|
113
|
3 |
|
return parent::getRulesIndexes($ruleIndexes); |
|
|
|
|
|
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* @inheritdoc |
|
118
|
|
|
*/ |
|
119
|
3 |
|
public static function readRuleStartIndexes(array $ruleIndexes): array |
|
120
|
|
|
{ |
|
121
|
3 |
|
return parent::getRulesStartIndexes($ruleIndexes); |
|
|
|
|
|
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* @inheritdoc |
|
126
|
|
|
*/ |
|
127
|
3 |
|
public static function readRuleEndIndexes(array $ruleIndexes): array |
|
128
|
|
|
{ |
|
129
|
3 |
|
return parent::getRulesEndIndexes($ruleIndexes); |
|
|
|
|
|
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* @param string $rulesClass |
|
134
|
|
|
* |
|
135
|
|
|
* @return bool |
|
136
|
|
|
*/ |
|
137
|
30 |
|
private static function isRulesClass(string $rulesClass): bool |
|
138
|
|
|
{ |
|
139
|
|
|
return |
|
140
|
30 |
|
class_exists($rulesClass) === true && |
|
141
|
30 |
|
in_array(FormRulesInterface::class, class_implements($rulesClass)) === true; |
|
142
|
|
|
} |
|
143
|
|
|
} |
|
144
|
|
|
|
Let’s assume you have a class which uses late-static binding:
}
The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the
getSomeVariable()on that sub-class, you will receive a runtime error:In the case above, it makes sense to update
SomeClassto useselfinstead: