Skip to content

Commit 2302691

Browse files
committed
Add validation rule "existMany"
1 parent 3a5db33 commit 2302691

File tree

5 files changed

+117
-0
lines changed

5 files changed

+117
-0
lines changed

src/Languages/en/validation.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
*/
1010
return [
1111
'exist' => 'The {field} field does not exist.',
12+
'existMany' => 'The {field} field has at least one value that does not exist.',
1213
'notUnique' => 'The {field} field is not registered.',
1314
'unique' => 'The {field} field has already been registered.',
1415
];

src/Languages/es/validation.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
*/
1010
return [
1111
'exist' => 'El campo {field} no existe.',
12+
'existMany' => 'El campo {field} tiene al menos un valor que no existe.',
1213
'notUnique' => 'El campo {field} no está registrado.',
1314
'unique' => 'El campo {field} ya se ha registrado. ',
1415
];

src/Languages/pt-br/validation.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
*/
1010
return [
1111
'exist' => 'O campo {field} não existe.',
12+
'existMany' => 'O campo {field} tem pelo menos um valor que não existe.',
1213
'notUnique' => 'O campo {field} não está registrado.',
1314
'unique' => 'O campo {field} já foi registrado.',
1415
];

src/Validator.php

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
*/
1010
namespace Framework\MVC;
1111

12+
use Framework\Helpers\ArraySimple;
1213
use LogicException;
1314

1415
/**
@@ -136,4 +137,60 @@ public static function exist(
136137
->run()
137138
->fetch()->count > 0;
138139
}
140+
141+
/**
142+
* Validates many values exists in database table.
143+
*
144+
* @since 3.10
145+
*
146+
* @param string $field
147+
* @param array<string,mixed> $data
148+
* @param string $tableColumn
149+
* @param string $connection
150+
*
151+
* @return bool
152+
*/
153+
public static function existMany(
154+
string $field,
155+
array $data,
156+
string $tableColumn,
157+
string $connection = 'default'
158+
) : bool {
159+
$values = ArraySimple::value($field, $data);
160+
if ($values === null) {
161+
return true;
162+
}
163+
if ( ! \is_array($values)) {
164+
return false;
165+
}
166+
foreach ($values as $value) {
167+
if ( ! \is_scalar($value)) {
168+
return false;
169+
}
170+
}
171+
[$table, $column] = \array_pad(\explode('.', $tableColumn, 2), 2, '');
172+
if ($column === '') {
173+
$column = $field;
174+
}
175+
if ($connection === '') {
176+
throw new LogicException(
177+
'The connection parameter must be set to be able to connect the database'
178+
);
179+
}
180+
$database = App::database($connection);
181+
foreach ($values as $value) {
182+
$count = $database // @phpstan-ignore-line
183+
->select()
184+
->expressions(['count' => static fn () => 'COUNT(*)'])
185+
->from($table)
186+
->whereEqual($column, $value)
187+
->limit(1)
188+
->run()
189+
->fetch()->count;
190+
if ($count < 1) {
191+
return false;
192+
}
193+
}
194+
return true;
195+
}
139196
}

tests/ValidatorTest.php

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,14 @@ public function testExistNoDataValue() : void
4141
self::assertArrayHasKey('username', $validation->getErrors());
4242
}
4343

44+
public function testExistManyNoDataValue() : void
45+
{
46+
$validation = App::validation();
47+
$validation->setRule('username', 'existMany:Users');
48+
self::assertTrue($validation->validate([]));
49+
self::assertEmpty($validation->getErrors());
50+
}
51+
4452
public function testUniqueNoDataValue() : void
4553
{
4654
$validation = App::validation();
@@ -73,6 +81,41 @@ public function testExist() : void
7381
self::assertArrayHasKey('username', $validation->getErrors());
7482
}
7583

84+
public function testExistMany() : void
85+
{
86+
$validation = App::validation();
87+
$validation->setRule('usernames', 'existMany:Users.username');
88+
$data = [
89+
'usernames' => [
90+
'foo',
91+
'bar',
92+
],
93+
];
94+
self::assertTrue($validation->validate($data));
95+
self::assertEmpty($validation->getErrors());
96+
$data = [
97+
'usernames' => [
98+
'foo',
99+
'bazz',
100+
],
101+
];
102+
self::assertFalse($validation->validate($data));
103+
self::assertArrayHasKey('usernames', $validation->getErrors());
104+
$data = [
105+
'usernames' => 'invalid', // it should be an array
106+
];
107+
self::assertFalse($validation->validate($data));
108+
self::assertArrayHasKey('usernames', $validation->getErrors());
109+
$data = [
110+
'usernames' => [
111+
['invalid'], // it should be scalar
112+
'bazz',
113+
],
114+
];
115+
self::assertFalse($validation->validate($data));
116+
self::assertArrayHasKey('usernames', $validation->getErrors());
117+
}
118+
76119
public function testUnique() : void
77120
{
78121
$validation = App::validation();
@@ -168,6 +211,20 @@ public function testExistWithoutConnection() : void
168211
$validation->validate($data);
169212
}
170213

214+
public function testExistManyWithoutConnection() : void
215+
{
216+
$validation = App::validation();
217+
$validation->setRule('usernames', 'existMany:Users,');
218+
$this->expectException(\LogicException::class);
219+
$this->expectExceptionMessage(
220+
'The connection parameter must be set to be able to connect the database'
221+
);
222+
$data = [
223+
'usernames' => ['foo'],
224+
];
225+
$validation->validate($data);
226+
}
227+
171228
public function testUniqueWithoutConnection() : void
172229
{
173230
$validation = App::validation();

0 commit comments

Comments
 (0)