Skip to content

Commit a65fb22

Browse files
committed
Added unique_values validation rule
1 parent 564fa35 commit a65fb22

4 files changed

Lines changed: 96 additions & 0 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
- `ClassFinder::using()`
1717
- `ClassFinder::with()`
1818
* Added `ClassFinder::findWith()` method to to enable finding classes using a certain attribute.
19+
* Added `unique_values` validation rule.
1920

2021
#### Deprecations
2122

src/mako/validator/Validator.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@
8080
use mako\validator\rules\session\Token;
8181
use mako\validator\rules\Str;
8282
use mako\validator\rules\TimeZone;
83+
use mako\validator\rules\UniqueValues;
8384
use mako\validator\rules\URL;
8485
use mako\validator\rules\UUID;
8586

@@ -173,6 +174,7 @@ class Validator
173174
'string' => Str::class,
174175
'time_zone' => TimeZone::class,
175176
'token' => Token::class,
177+
'unique_values' => UniqueValues::class,
176178
'unique' => Unique::class,
177179
'url' => URL::class,
178180
'uuid' => UUID::class,
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
/**
4+
* @copyright Frederic G. Østby
5+
* @license http://www.makoframework.com/license
6+
*/
7+
8+
namespace mako\validator\rules;
9+
10+
use Override;
11+
12+
use function array_unique;
13+
use function count;
14+
use function sprintf;
15+
16+
/**
17+
* Unique values rule.
18+
*/
19+
class UniqueValues extends Rule implements RuleInterface
20+
{
21+
/**
22+
* I18n parameters.
23+
*/
24+
protected array $i18nParameters = ['unique'];
25+
26+
/**
27+
* {@inheritDoc}
28+
*/
29+
#[Override]
30+
public function validate(mixed $value, string $field, array $input): bool
31+
{
32+
return count($value) === count(array_unique($value));
33+
}
34+
35+
/**
36+
* {@inheritDoc}
37+
*/
38+
#[Override]
39+
public function getErrorMessage(string $field): string
40+
{
41+
return sprintf('The %s field must only contain unique values.', $field);
42+
}
43+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
/**
4+
* @copyright Frederic G. Østby
5+
* @license http://www.makoframework.com/license
6+
*/
7+
8+
namespace mako\tests\unit\validator\rules;
9+
10+
use mako\tests\TestCase;
11+
use mako\validator\rules\UniqueValues;
12+
use PHPUnit\Framework\Attributes\Group;
13+
14+
#[Group('unit')]
15+
class UniqueValuesTest extends TestCase
16+
{
17+
/**
18+
*
19+
*/
20+
public function testValidatesWhenEmpty(): void
21+
{
22+
$rule = new UniqueValues;
23+
24+
$this->assertFalse($rule->validateWhenEmpty());
25+
}
26+
27+
/**
28+
*
29+
*/
30+
public function testWithValidValue(): void
31+
{
32+
$rule = new UniqueValues;
33+
34+
$this->assertTrue($rule->validate([], '', []));
35+
$this->assertTrue($rule->validate([1, 2, 3], '', []));
36+
$this->assertTrue($rule->validate([1, 2, 3, 4], '', []));
37+
}
38+
39+
/**
40+
*
41+
*/
42+
public function testWithInvalidValue(): void
43+
{
44+
$rule = new UniqueValues;
45+
46+
$this->assertFalse($rule->validate([1, 2, 3, 3], '', []));
47+
48+
$this->assertSame('The foobar field must only contain unique values.', $rule->getErrorMessage('foobar'));
49+
}
50+
}

0 commit comments

Comments
 (0)