-
Notifications
You must be signed in to change notification settings - Fork 0
/
Rules.php
186 lines (156 loc) · 4.1 KB
/
Rules.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
<?php
declare(strict_types=1);
namespace Upmind\ProvisionBase\Provider\DataSet;
use ArrayAccess;
use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Contracts\Support\Jsonable;
use JsonSerializable;
use Upmind\ProvisionBase\Laravel\Html\Form;
use Upmind\ProvisionBase\Laravel\Html\FormElement;
use Upmind\ProvisionBase\Laravel\Html\FormFactory;
/**
* Provision data set validation rules
*/
final class Rules implements ArrayAccess, JsonSerializable, Arrayable, Jsonable
{
/**
* Raw data set rules.
*
* @var array<string[]>
*/
protected $rawRules;
/**
* Expanded data set rules.
*
* @var array<string[]>|null
*/
protected $expandedRules;
/**
* Name of the parent field, if any.
*
* @var string|null
*/
protected $parentField;
/**
* @param array<string[]> $rules Laravel validation rules and nested data set references
*/
public function __construct(array $rules = [])
{
$this->rawRules = $rules;
$this->expandedRules = null;
}
/**
* Create a new rules instance.
*/
public static function fromArray(array $rules): self
{
return new self($rules);
}
/**
* Set the parent field for rule expansion
*/
public function setParentField(?string $parentField): self
{
if ($this->parentField !== $parentField) {
$this->parentField = $parentField ?: null;
$this->expandedRules = null;
}
return $this;
}
/**
* Expand these data set rules' nested references and return a portable rule
* set.
*
* @param string|null $parentField Parent field name for rule expansion
*
* @return array<string[]> Expanded rules
*/
public function expand(?string $parentField = null): array
{
$this->setParentField($parentField);
if (!isset($this->expandedRules)) {
$this->expandedRules = RuleParser::expand($this->rawRules, $this->parentField);
}
return $this->expandedRules;
}
/**
* Return the raw data set rules, optionally for the given field.
*
*@param string|null $field Optionally return rules for this field only
*
* @return array<string[]>|string[] Raw rules
*/
public function raw(?string $field = null): array
{
if (isset($field)) {
return RuleParser::explodeRules($this->rawRules[$field] ?? []);
}
return $this->rawRules;
}
/**
* Get the set parent field name, if any.
*
* @return string|null
*/
public function parentField(): ?string
{
return $this->parentField;
}
public function offsetExists($offset): bool
{
$this->expand($this->parentField);
return array_key_exists($offset, $this->expandedRules);
}
/**
* @return string[]
*/
public function offsetGet($offset): array
{
$this->expand($this->parentField);
return $this->expandedRules[$offset];
}
public function offsetSet($offset, $value): void
{
$this->expand($this->parentField);
$this->expandedRules[$offset] = $value;
}
public function offsetUnset($offset): void
{
$this->expand($this->parentField);
unset($this->expandedRules[$offset]);
}
/**
* @return Form<FormElement>
*/
public function toHtmlForm(): Form
{
return (new FormFactory())->create($this->toArray());
}
/**
* Return the expanded rules in array format.
*
* @return array
*/
public function toArray()
{
$this->expand($this->parentField);
return $this->expandedRules;
}
public function jsonSerialize(): array
{
return $this->toArray();
}
/**
* Return the expanded rules in json format.
*
* @return string
*/
public function toJson($options = 0)
{
return json_encode($this->jsonSerialize(), $options | JSON_THROW_ON_ERROR);
}
public function __debugInfo()
{
return $this->expandedRules ?? $this->rawRules;
}
}