Skip to content

Commit 601d98e

Browse files
committed
refactor: normailze $this->rules
1 parent 509b38a commit 601d98e

2 files changed

Lines changed: 32 additions & 12 deletions

File tree

system/Validation/Validation.php

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,18 @@ class Validation implements ValidationInterface
4040
protected $ruleSetInstances = [];
4141

4242
/**
43-
* Stores the actual rules that should
44-
* be ran against $data.
43+
* Stores the actual rules that should be run against $data.
4544
*
4645
* @var array
46+
*
47+
* [
48+
* field1 => [
49+
* 'label' => label,
50+
* 'rules' => [
51+
* rule1, rule2, ...
52+
* ],
53+
* ],
54+
* ]
4755
*/
4856
protected $rules = [];
4957

@@ -133,8 +141,7 @@ public function run(?array $data = null, ?string $group = null, ?string $dbGroup
133141
// Run through each rule. If we have any field set for
134142
// this rule, then we need to run them through!
135143
foreach ($this->rules as $field => $setup) {
136-
// Blast $rSetup apart, unless it's already an array.
137-
$rules = $setup['rules'] ?? $setup;
144+
$rules = $setup['rules'];
138145

139146
if (is_string($rules)) {
140147
$rules = $this->splitRules($rules);
@@ -483,6 +490,14 @@ public function setRules(array $rules, array $errors = []): ValidationInterface
483490
$rule = ['rules' => $rule];
484491
}
485492
}
493+
494+
if (isset($rule['rules']) && is_string($rule['rules'])) {
495+
$rule['rules'] = $this->splitRules($rule['rules']);
496+
}
497+
498+
if (is_string($rule)) {
499+
$rule = ['rules' => $this->splitRules($rule)];
500+
}
486501
}
487502

488503
$this->rules = $rules;

tests/system/Validation/ValidationTest.php

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,12 @@ public function testSetRulesStoresRules(): void
8686
'bar' => 'baz|belch',
8787
];
8888
$this->validation->setRules($rules);
89-
$this->assertSame($rules, $this->validation->getRules());
89+
90+
$expected = [
91+
'foo' => ['rules' => ['bar', 'baz']],
92+
'bar' => ['rules' => ['baz', 'belch']],
93+
];
94+
$this->assertSame($expected, $this->validation->getRules());
9095
}
9196

9297
public function testSetRuleStoresRule()
@@ -97,7 +102,7 @@ public function testSetRuleStoresRule()
97102
$this->assertSame([
98103
'foo' => [
99104
'label' => null,
100-
'rules' => 'bar|baz',
105+
'rules' => ['bar', 'baz'],
101106
],
102107
], $this->validation->getRules());
103108
}
@@ -110,7 +115,7 @@ public function testSetRuleMultipleWithIndividual()
110115
$this->assertSame([
111116
'username' => [
112117
'label' => 'Username',
113-
'rules' => 'required|min_length[3]',
118+
'rules' => ['required', 'min_length[3]'],
114119
],
115120
'password' => [
116121
'label' => 'Password',
@@ -136,11 +141,11 @@ public function testSetRuleAddsRule()
136141
$this->assertSame([
137142
'bar' => [
138143
'label' => null,
139-
'rules' => 'bar|baz',
144+
'rules' => ['bar', 'baz'],
140145
],
141146
'foo' => [
142147
'label' => null,
143-
'rules' => 'foo|foz',
148+
'rules' => ['foo', 'foz'],
144149
],
145150
], $this->validation->getRules());
146151
}
@@ -158,7 +163,7 @@ public function testSetRuleOverwritesRule()
158163
$this->assertSame([
159164
'foo' => [
160165
'label' => null,
161-
'rules' => 'foo|foz',
166+
'rules' => ['foo', 'foz'],
162167
],
163168
], $this->validation->getRules());
164169
}
@@ -176,7 +181,7 @@ public function testSetRuleOverwritesRuleReverse()
176181
$this->assertSame([
177182
'foo' => [
178183
'label' => null,
179-
'rules' => 'bar|baz',
184+
'rules' => ['bar', 'baz'],
180185
],
181186
], $this->validation->getRules());
182187
}
@@ -549,7 +554,7 @@ public function testSetRuleGroup(): void
549554
{
550555
$this->validation->setRuleGroup('groupA');
551556
$this->assertSame([
552-
'foo' => 'required|min_length[5]',
557+
'foo' => ['rules' => ['required', 'min_length[5]']],
553558
], $this->validation->getRules());
554559
}
555560

0 commit comments

Comments
 (0)