1
|
|
|
<?php declare (strict_types = 1); |
2
|
|
|
|
3
|
|
|
namespace Limoncello\Flute\Validation\Form\Execution; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Copyright 2015-2019 [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\Common\Reflection\ClassIsTrait; |
22
|
|
|
use Limoncello\Flute\Contracts\Validation\FormRulesInterface; |
23
|
|
|
use Limoncello\Flute\Contracts\Validation\FormRulesSerializerInterface; |
24
|
|
|
use Limoncello\Flute\Validation\Serialize\RulesSerializer; |
25
|
|
|
use function array_key_exists; |
26
|
|
|
use function assert; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @package Limoncello\Flute |
30
|
|
|
* |
31
|
|
|
* @SuppressWarnings(PHPMD.TooManyPublicMethods) |
32
|
|
|
*/ |
33
|
|
|
class FormRulesSerializer extends RulesSerializer implements FormRulesSerializerInterface |
34
|
|
|
{ |
35
|
|
|
use ClassIsTrait; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var array |
39
|
|
|
*/ |
40
|
|
|
private $serializedRules = []; |
41
|
|
|
|
42
|
|
|
/** Serialized indexes key */ |
43
|
|
|
protected const SERIALIZED_RULES = 0; |
44
|
|
|
|
45
|
|
|
/** Serialized rules key */ |
46
|
35 |
|
protected const SERIALIZED_BLOCKS = self::SERIALIZED_RULES + 1; |
47
|
|
|
|
48
|
35 |
|
/** |
49
|
|
|
* @inheritdoc |
50
|
35 |
|
*/ |
51
|
|
|
public function addRulesFromClass(string $rulesClass): FormRulesSerializerInterface |
52
|
|
|
{ |
53
|
|
|
assert(static::classImplements($rulesClass, FormRulesInterface::class)); |
54
|
35 |
|
|
55
|
|
|
$name = $rulesClass; |
56
|
|
|
|
57
|
|
|
/** @var FormRulesInterface $rulesClass */ |
58
|
|
|
|
59
|
|
|
return $this->addFormRules($name, $rulesClass::getAttributeRules()); |
60
|
35 |
|
} |
61
|
|
|
|
62
|
35 |
|
/** |
63
|
35 |
|
* @inheritdoc |
64
|
|
|
*/ |
65
|
35 |
|
public function addFormRules(string $name, ?array $attributeRules): FormRulesSerializerInterface |
66
|
|
|
{ |
67
|
35 |
|
assert(!empty($name)); |
68
|
|
|
assert(static::hasRules($name, $this->serializedRules) === false); |
69
|
|
|
|
70
|
|
|
$this->serializedRules[$name] = $attributeRules === null ? null : $this->addRules($attributeRules); |
71
|
|
|
|
72
|
|
|
return $this; |
73
|
35 |
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
35 |
|
* @inheritdoc |
77
|
35 |
|
*/ |
78
|
|
|
public function getData(): array |
79
|
|
|
{ |
80
|
|
|
return [ |
81
|
|
|
static::SERIALIZED_RULES => $this->serializedRules, |
82
|
|
|
static::SERIALIZED_BLOCKS => $this->getBlocks(), |
83
|
|
|
]; |
84
|
4 |
|
} |
85
|
|
|
|
86
|
4 |
|
/** |
87
|
|
|
* @inheritdoc |
88
|
|
|
*/ |
89
|
|
|
public static function readBlocks(array $serializedData): array |
90
|
|
|
{ |
91
|
|
|
return $serializedData[static::SERIALIZED_BLOCKS]; |
92
|
35 |
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @inheritdoc |
96
|
35 |
|
*/ |
97
|
35 |
|
public static function hasRules(string $name, array $serializedData): bool |
98
|
|
|
{ |
99
|
|
|
// the value could be null so we have to check by key existence. |
100
|
|
|
return |
101
|
|
|
array_key_exists(static::SERIALIZED_RULES, $serializedData) === true && |
102
|
|
|
array_key_exists($name, $serializedData[static::SERIALIZED_RULES]); |
103
|
4 |
|
} |
104
|
|
|
|
105
|
4 |
|
/** |
106
|
|
|
* @inheritdoc |
107
|
4 |
|
*/ |
108
|
|
|
public static function readRules(string $rulesClass, array $serializedData): array |
109
|
|
|
{ |
110
|
|
|
assert(static::hasRules($rulesClass, $serializedData) === true); |
111
|
|
|
|
112
|
|
|
return $serializedData[static::SERIALIZED_RULES][$rulesClass]; |
113
|
4 |
|
} |
114
|
|
|
|
115
|
4 |
|
/** |
116
|
|
|
* @inheritdoc |
117
|
|
|
*/ |
118
|
|
|
public static function readRuleMainIndexes(array $ruleIndexes): ?array |
119
|
|
|
{ |
120
|
|
|
return parent::getRulesIndexes($ruleIndexes); |
|
|
|
|
121
|
4 |
|
} |
122
|
|
|
|
123
|
4 |
|
/** |
124
|
|
|
* @inheritdoc |
125
|
|
|
*/ |
126
|
|
|
public static function readRuleStartIndexes(array $ruleIndexes): array |
127
|
|
|
{ |
128
|
|
|
return parent::getRulesStartIndexes($ruleIndexes); |
|
|
|
|
129
|
4 |
|
} |
130
|
|
|
|
131
|
4 |
|
/** |
132
|
|
|
* @inheritdoc |
133
|
|
|
*/ |
134
|
|
|
public static function readRuleEndIndexes(array $ruleIndexes): array |
135
|
|
|
{ |
136
|
|
|
return parent::getRulesEndIndexes($ruleIndexes); |
|
|
|
|
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|
This check looks for a call to a parent method whose name is different than the method from which it is called.
Consider the following code:
The
getFirstName()
method in theSon
calls the wrong method in the parent class.