Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ offering an easy-to-use and intuitive API to validate user input or other data i
- [validate](#validate)
- [assert](#assert)
- [isValid](#isvalid)
- [getConstraints](#getconstraints)
- [toArray](#toarray)
- [addNamespace](#addnamespace)
- [setTranslator](#settranslator)
- [Custom Constraints](#custom-constraints)
Expand Down Expand Up @@ -154,21 +154,21 @@ if (!Validator::email()->isValid($email)) {
}
```

### `getConstraints`
### `toArray`

```php
use Symfony\Component\Validator\Constraint;

/** @return Constraint[] */
getConstraints(): array
toArray(): array
```

Returns an array with all added constraints.

```php
use ProgrammatorDev\FluentValidator\Validator;

$constraints = Validator::notBlank()->email()->getConstraints();
$constraints = Validator::notBlank()->email()->toArray();
```

It is useful for `Composite` constraints (i.e., a constraint that is composed of other constraints)
Expand All @@ -180,7 +180,7 @@ use ProgrammatorDev\FluentValidator\Validator;
// validate that array should have at least one value
// and each value should be between 0 and 100
$errors = Validator::count(min: 1)
->all(Validator::range(min: 0, max: 100)->getConstraints())
->all(Validator::range(min: 0, max: 100)->toArray())
->validate($value);
```

Expand Down
2 changes: 1 addition & 1 deletion src/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public function isValid(mixed $value, string|GroupSequence|array|null $groups =
return $violations->count() === 0;
}

public function getConstraints(): array
public function toArray(): array
{
return $this->constraints;
}
Expand Down
4 changes: 2 additions & 2 deletions tests/ValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,9 @@ public function testIsValid(): void
$this->assertTrue($this->validator->isValid(18));
}

public function testGetConstraints(): void
public function testToArray(): void
{
$constraints = $this->validator->getConstraints();
$constraints = $this->validator->toArray();

$this->assertInstanceOf(NotBlank::class, $constraints[0]);
$this->assertInstanceOf(GreaterThanOrEqual::class, $constraints[1]);
Expand Down