Skip to content

Commit 7a5034c

Browse files
authored
Merge pull request #5 from programmatordev/1.x
v0.2.0
2 parents c1146af + c57bc25 commit 7a5034c

File tree

4 files changed

+14
-18
lines changed

4 files changed

+14
-18
lines changed

README.md

+5-5
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ offering an easy-to-use and intuitive API to validate user input or other data i
2424
- [validate](#validate)
2525
- [assert](#assert)
2626
- [isValid](#isvalid)
27-
- [getConstraints](#getconstraints)
27+
- [toArray](#toarray)
2828
- [addNamespace](#addnamespace)
2929
- [setTranslator](#settranslator)
3030
- [Custom Constraints](#custom-constraints)
@@ -154,21 +154,21 @@ if (!Validator::email()->isValid($email)) {
154154
}
155155
```
156156

157-
### `getConstraints`
157+
### `toArray`
158158

159159
```php
160160
use Symfony\Component\Validator\Constraint;
161161

162162
/** @return Constraint[] */
163-
getConstraints(): array
163+
toArray(): array
164164
```
165165

166166
Returns an array with all added constraints.
167167

168168
```php
169169
use ProgrammatorDev\FluentValidator\Validator;
170170

171-
$constraints = Validator::notBlank()->email()->getConstraints();
171+
$constraints = Validator::notBlank()->email()->toArray();
172172
```
173173

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

src/Factory/ConstraintFactory.php

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ public function create(string $constraintName, array $arguments = []): Constrain
1717
foreach ($this->namespaces as $namespace) {
1818
$class = sprintf('%s\%s', $namespace, $constraintName);
1919

20+
// if class exists and is an instance of Constraint
2021
if (class_exists($class) && is_a($class, Constraint::class, true)) {
2122
return new $class(...$arguments);
2223
}

src/Validator.php

+4-3
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,11 @@ public function assert(mixed $value, ?string $name = null, string|GroupSequence|
6969
$violations = $this->validate($value, $name, $groups);
7070

7171
if ($violations->count() > 0) {
72-
$message = $violations->get(0)->getMessage();
72+
$violation = $violations->get(0);
73+
$message = $violation->getMessage();
7374

7475
if ($name !== null) {
75-
$message = sprintf('%s: %s', $name, $message);
76+
$message = sprintf('%s: %s', $violation->getPropertyPath(), $message);
7677
}
7778

7879
throw new ValidationFailedException($message, $value, $violations);
@@ -86,7 +87,7 @@ public function isValid(mixed $value, string|GroupSequence|array|null $groups =
8687
return $violations->count() === 0;
8788
}
8889

89-
public function getConstraints(): array
90+
public function toArray(): array
9091
{
9192
return $this->constraints;
9293
}

tests/ValidatorTest.php

+4-10
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,10 @@ public function testConstraintThatDoesNotExist(): void
4040

4141
public function testValidate(): void
4242
{
43-
// test fail
4443
$violations = $this->validator->validate(16);
4544
$this->assertInstanceOf(ConstraintViolationList::class, $violations);
4645
$this->assertCount(1, $violations);
4746

48-
// test success
4947
$violations = $this->validator->validate(18);
5048
$this->assertInstanceOf(ConstraintViolationList::class, $violations);
5149
$this->assertCount(0, $violations);
@@ -65,15 +63,13 @@ public function testAssertSuccess(): void
6563

6664
public function testIsValid(): void
6765
{
68-
// test fail
6966
$this->assertFalse($this->validator->isValid(16));
70-
// test success
7167
$this->assertTrue($this->validator->isValid(18));
7268
}
7369

74-
public function testGetConstraints(): void
70+
public function testToArray(): void
7571
{
76-
$constraints = $this->validator->getConstraints();
72+
$constraints = $this->validator->toArray();
7773

7874
$this->assertInstanceOf(NotBlank::class, $constraints[0]);
7975
$this->assertInstanceOf(GreaterThanOrEqual::class, $constraints[1]);
@@ -84,22 +80,20 @@ public function testCustomConstraint(): void
8480
{
8581
Validator::addNamespace('ProgrammatorDev\FluentValidator\Test\Constraint');
8682

87-
// test fail
8883
$this->assertFalse(Validator::containsAlphanumeric()->isValid('!'));
89-
// test success
9084
$this->assertTrue(Validator::containsAlphanumeric()->isValid('v4l1d'));
9185
}
9286

9387
public function testSetTranslator(): void
9488
{
9589
// by default, error is in English
9690
$violations = $this->validator->validate('');
97-
$this->assertEquals('This value should not be blank.', $violations[0]->getMessage());
91+
$this->assertEquals('This value should not be blank.', $violations->get(0)->getMessage());
9892

9993
// set translator and then try again
10094
Validator::setTranslator(new Translator('pt'));
10195
// now error is in Portuguese
10296
$violations = $this->validator->validate('');
103-
$this->assertEquals('Este valor não deveria ser vazio.', $violations[0]->getMessage());
97+
$this->assertEquals('Este valor não deveria ser vazio.', $violations->get(0)->getMessage());
10498
}
10599
}

0 commit comments

Comments
 (0)