Skip to content

Commit

Permalink
Improve docs (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
vjik committed Dec 22, 2022
1 parent 554b624 commit 7c113cd
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 8 deletions.
32 changes: 24 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,20 +34,31 @@ composer require vjik/yii-validator-symfony-rule
Wrap a symfony constraints to Yii rule `SymfonyRule` enough. For example:

```php
use Symfony\Component\Validator\Constraints\Email;
use Symfony\Component\Validator\Constraints\NotBlank;
use Symfony\Component\Validator\Constraints\CssColor;
use Symfony\Component\Validator\Constraints\NotEqualTo;
use Symfony\Component\Validator\Constraints\Positive;
use Vjik\Yii\ValidatorSymfonyRule\SymfonyRule;
use Yiisoft\Validator\Rule\HasLength;
use Yiisoft\Validator\Rule\Required;

final class PropertyAttribute
final class Car
{
#[SymfonyRule(new NotBlank())]
#[Required]
#[HasLength(min: 3, skipOnEmpty: true)]
public string $name = '';


#[Required]
#[SymfonyRule(
new CssColor(CssColor::RGB), // Symfony constraint
skipOnEmpty: true,
)]
public string $cssColor = '#1123';

#[SymfonyRule([
new NotBlank(),
new Email(),
new Positive(), // Symfony constraint
new NotEqualTo(13), // Symfony constraint
])]
public string $email = '';
public int $number = 13;
}
```

Expand All @@ -62,6 +73,11 @@ Defaults to `false`.

**$when** — The closure that allow to apply rule under certain conditions only. Defaults to `null`.

## `SymfonyRuleHandler` parameters

`$symfonyValidator` — Symfony validator instance. Defaults to validator created by
`Symfony\Component\Validator\Validation::createValidator()`.

## Testing

### Unit testing
Expand Down
4 changes: 4 additions & 0 deletions src/SymfonyRuleHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ final class SymfonyRuleHandler implements RuleHandlerInterface
{
private SymfonyValidatorInterface $symfonyValidator;

/**
* @param SymfonyValidatorInterface|null $symfonyValidator Symfony validator instance. When `null` the default
* validator created by {@see Validation::createValidator()} will be used.
*/
public function __construct(?SymfonyValidatorInterface $symfonyValidator = null)
{
$this->symfonyValidator = $symfonyValidator ?? Validation::createValidator();
Expand Down
15 changes: 15 additions & 0 deletions tests/Php81/SymfonyRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,27 @@
namespace Vjik\Yii\ValidatorSymfonyRule\Tests\Php81;

use PHPUnit\Framework\TestCase;
use Vjik\Yii\ValidatorSymfonyRule\Tests\Support\Car;
use Vjik\Yii\ValidatorSymfonyRule\Tests\Support\ClassAttribute;
use Vjik\Yii\ValidatorSymfonyRule\Tests\Support\PropertyAttribute;
use Yiisoft\Validator\Validator;

final class SymfonyRuleTest extends TestCase
{
public function testCar(): void
{
$result = (new Validator())->validate(new Car());

$this->assertSame(
[
'name' => ['Value cannot be blank.'],
'cssColor' => ['This value is not a valid CSS color.'],
'number' => ['This value should not be equal to 13.'],
],
$result->getErrorMessagesIndexedByPath(),
);
}

public function testPropertyAttribute(): void
{
$result = (new Validator())->validate(new PropertyAttribute());
Expand Down
32 changes: 32 additions & 0 deletions tests/Support/Car.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

declare(strict_types=1);

namespace Vjik\Yii\ValidatorSymfonyRule\Tests\Support;

use Symfony\Component\Validator\Constraints\CssColor;
use Symfony\Component\Validator\Constraints\NotEqualTo;
use Symfony\Component\Validator\Constraints\Positive;
use Vjik\Yii\ValidatorSymfonyRule\SymfonyRule;
use Yiisoft\Validator\Rule\HasLength;
use Yiisoft\Validator\Rule\Required;

final class Car
{
#[Required]
#[HasLength(min: 3, skipOnEmpty: true)]
public string $name = '';

#[Required]
#[SymfonyRule(
new CssColor(CssColor::RGB),
skipOnEmpty: true,
)]
public string $cssColor = '#1123';

#[SymfonyRule([
new Positive(),
new NotEqualTo(13),
])]
public int $number = 13;
}

0 comments on commit 7c113cd

Please sign in to comment.