Skip to content

Commit 9e37da9

Browse files
authored
Allow use AtLeast as class attribute (#443)
1 parent c6e6122 commit 9e37da9

3 files changed

Lines changed: 28 additions & 1 deletion

File tree

src/Rule/AtLeast.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
*
2020
* @psalm-import-type WhenType from WhenInterface
2121
*/
22-
#[Attribute(Attribute::TARGET_PROPERTY | Attribute::IS_REPEATABLE)]
22+
#[Attribute(Attribute::TARGET_CLASS | Attribute::TARGET_PROPERTY | Attribute::IS_REPEATABLE)]
2323
final class AtLeast implements RuleWithOptionsInterface, SkipOnErrorInterface, WhenInterface, SkipOnEmptyInterface
2424
{
2525
use SkipOnEmptyTrait;

tests/Rule/AtLeastTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Yiisoft\Validator\Tests\Rule\Base\RuleWithOptionsTestTrait;
1313
use Yiisoft\Validator\Tests\Rule\Base\SkipOnErrorTestTrait;
1414
use Yiisoft\Validator\Tests\Rule\Base\WhenTestTrait;
15+
use Yiisoft\Validator\Tests\Support\Data\AtLeastDto;
1516

1617
final class AtLeastTest extends RuleTestCase
1718
{
@@ -153,6 +154,9 @@ public function __construct()
153154
['obj' => ['attr1' => null, 'attr2' => 1]],
154155
['obj' => new AtLeast(['attr2'])],
155156
],
157+
'class attribute' => [
158+
new AtLeastDto(1),
159+
],
156160
];
157161
}
158162

@@ -225,6 +229,11 @@ public function dataValidationFailed(): array
225229
['data' => new AtLeast(['attr1', 'attr2'], min: 2, message: 'Attribute - {attribute}, min - {min}.')],
226230
['data' => ['Attribute - data, min - 2.']],
227231
],
232+
'class attribute' => [
233+
new AtLeastDto(),
234+
null,
235+
['' => ['The data must have at least "1" filled attributes.']],
236+
],
228237
];
229238
}
230239

tests/Support/Data/AtLeastDto.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Yiisoft\Validator\Tests\Support\Data;
6+
7+
use Yiisoft\Validator\Rule\AtLeast;
8+
9+
#[AtLeast(['a', 'b', 'c'])]
10+
final class AtLeastDto
11+
{
12+
public function __construct(
13+
public ?int $a = null,
14+
public ?int $b = null,
15+
public ?int $c = null,
16+
) {
17+
}
18+
}

0 commit comments

Comments
 (0)