Skip to content

Commit 52dda6b

Browse files
committed
php cs fixer
1 parent ed178a8 commit 52dda6b

File tree

8 files changed

+105
-95
lines changed

8 files changed

+105
-95
lines changed

src/AssociativeArrayValidator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public static function isRuleMatched($rule): bool
1313

1414
public function validate(mixed $value): array
1515
{
16-
if(!is_array($value)) {
16+
if (!is_array($value)) {
1717
return ['The input is not an array'];
1818
}
1919

src/FieldValidator.php

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,24 @@
44

55
namespace Escuelait\MagnificValidator;
66

7-
class FieldValidator implements Validator {
7+
class FieldValidator implements Validator
8+
{
9+
private $rules;
810

9-
private $rules;
11+
public function __construct($rules)
12+
{
13+
assert(count((new StringsArrayValidator())->validate($rules)) === 0, 'Not valid rules for FieldValidator');
1014

11-
public function __construct($rules)
12-
{
13-
assert(count((new StringsArrayValidator())->validate($rules)) === 0, 'Not valid rules for FieldValidator');
14-
15-
$this->rules = $rules;
16-
}
15+
$this->rules = $rules;
16+
}
1717

18-
public function validate($value): array {
19-
$validators = (new ValidatorsStrategyFactory())->createValidators($this->rules);
20-
$fieldErrors = [];
21-
foreach($validators as $validator) {
22-
$fieldErrors = array_merge($fieldErrors, $validator->validate($value));
18+
public function validate($value): array
19+
{
20+
$validators = (new ValidatorsStrategyFactory())->createValidators($this->rules);
21+
$fieldErrors = [];
22+
foreach ($validators as $validator) {
23+
$fieldErrors = array_merge($fieldErrors, $validator->validate($value));
24+
}
25+
return $fieldErrors;
2326
}
24-
return $fieldErrors;
25-
}
26-
}
27+
}

src/FormValidator.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ public function __construct(array $rules)
1717

1818
public function validate($values): array
1919
{
20-
if(count((new AssociativeArrayValidator())->validate($values)) > 0) {
20+
if (count((new AssociativeArrayValidator())->validate($values)) > 0) {
2121
throw(new InvalidArgumentException('Not valid form values'));
2222
}
23-
23+
2424
$formErrors = [];
2525
foreach ($this->rules as $fieldName => $ruleNames) {
2626
$fieldErrors = $this->validateValue($values[$fieldName], $ruleNames);

src/StringsArrayValidator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public static function isRuleMatched($rule): bool
1313

1414
public function validate(mixed $array): array
1515
{
16-
if(!is_array($array)) {
16+
if (!is_array($array)) {
1717
return ['Input is not an array'];
1818
}
1919

src/ValidatorsFactory.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,19 @@
88

99
class ValidatorsFactory
1010
{
11-
12-
public function create(mixed $rules) {
13-
if(!is_string($rules) && !$this->isAssociativeArray($rules) && !$this->isNumericArrayOfStrings($rules)) {
11+
public function create(mixed $rules)
12+
{
13+
if (!is_string($rules) && !$this->isAssociativeArray($rules) && !$this->isNumericArrayOfStrings($rules)) {
1414
throw(new InvalidArgumentException("Not valid input to create a validator"));
1515
}
16-
if(is_string($rules)) {
16+
if (is_string($rules)) {
1717
$validatorStrategyFactory = new ValidatorsStrategyFactory();
18-
return new($validatorStrategyFactory->matchedValidator($rules))($rules);
18+
return new ($validatorStrategyFactory->matchedValidator($rules))($rules);
1919
}
20-
if($this->isAssociativeArray($rules)) {
20+
if ($this->isAssociativeArray($rules)) {
2121
return new FormValidator($rules);
2222
}
23-
if($this->isNumericArrayOfStrings($rules)) {
23+
if ($this->isNumericArrayOfStrings($rules)) {
2424
return new FieldValidator($rules);
2525
}
2626
}

tests/FieldValidatorTest.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@
55
namespace Escuelait\Tests\MagnificValidator;
66

77
use AssertionError;
8-
use PHPUnit\Framework\TestCase;
9-
use PHPUnit\Framework\Attributes\Test;
10-
use PHPUnit\Framework\Attributes\DataProvider;
118
use Escuelait\MagnificValidator\FieldValidator;
9+
use PHPUnit\Framework\Attributes\DataProvider;
10+
use PHPUnit\Framework\Attributes\Test;
11+
use PHPUnit\Framework\TestCase;
1212

1313
class FieldValidatorTest extends TestCase
1414
{
15-
16-
public static function validValuesDataProvider() {
15+
public static function validValuesDataProvider()
16+
{
1717
return [
1818
[
1919
['required', 'email'],
@@ -35,7 +35,8 @@ public function itRecivesAnExceptionWhenValidatingWithIncorrectInput($rules, $va
3535
$this->assertEmpty($errors);
3636
}
3737

38-
public static function invalidValuesDataProvider() {
38+
public static function invalidValuesDataProvider()
39+
{
3940
return [
4041
[['something', 'foo']],
4142
[null],

tests/FormValidatorTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,8 @@ public function itRecivesAnExceptionWhenRuleIsNotReal()
219219
$validator->validate(['comment' => 'something']);
220220
}
221221

222-
public static function invalidFormValuesDataProvider() {
222+
public static function invalidFormValuesDataProvider()
223+
{
223224
return [
224225
[2],
225226
['something'],

tests/ValidatorsFactoryTest.php

Lines changed: 68 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -9,71 +9,78 @@
99
use PHPUnit\Framework\Attributes\Test;
1010
use PHPUnit\Framework\TestCase;
1111

12-
class ValidatorsFactoryTest extends TestCase {
12+
class ValidatorsFactoryTest extends TestCase
13+
{
14+
public static function notValidInputProvider()
15+
{
16+
return [
17+
[5],
18+
[true],
19+
[5.09],
20+
[null],
21+
];
22+
}
1323

14-
public static function notValidInputProvider() {
15-
return [
16-
[5],
17-
[true],
18-
[5.09],
19-
[null],
20-
];
21-
}
24+
#[Test]
25+
#[DataProvider('notValidInputProvider')]
26+
public function itThrowsAnErrorWhenTryToCreateValidatorWithInvalidData($input)
27+
{
28+
$validatorFactory = new ValidatorsFactory();
29+
$this->expectException(\InvalidArgumentException::class);
30+
$this->expectExceptionMessage("Not valid input to create a validator");
31+
$validatorFactory->create($input);
32+
}
2233

23-
#[Test]
24-
#[DataProvider('notValidInputProvider')]
25-
public function itThrowsAnErrorWhenTryToCreateValidatorWithInvalidData($input) {
26-
$validatorFactory = new ValidatorsFactory();
27-
$this->expectException(\InvalidArgumentException::class);
28-
$this->expectExceptionMessage("Not valid input to create a validator");
29-
$validatorFactory->create($input);
30-
}
34+
#[Test]
35+
public function itValidatesAForm()
36+
{
37+
$validatorFactory = new ValidatorsFactory();
38+
$validator = $validatorFactory->create([
39+
'email' => ['required', 'email'],
40+
'password' => ['required', 'max:16'],
41+
]);
42+
$errors = $validator->validate([
43+
'email' => 'user@example.com',
44+
'password' => 'secret123',
45+
]);
46+
$this->assertEmpty($errors);
47+
}
3148

32-
#[Test]
33-
public function itValidatesAForm() {
34-
$validatorFactory = new ValidatorsFactory();
35-
$validator = $validatorFactory->create([
36-
'email' => ['required', 'email'],
37-
'password' => ['required', 'max:16'],
38-
]);
39-
$errors = $validator->validate([
40-
'email' => 'user@example.com',
41-
'password' => 'secret123',
42-
]);
43-
$this->assertEmpty($errors);
44-
}
49+
#[Test]
50+
public function itValidatesAField()
51+
{
52+
$validatorFactory = new ValidatorsFactory();
53+
$validator = $validatorFactory->create(['required', 'email']);
54+
$errors = $validator->validate('user@example.com');
55+
$this->assertEmpty($errors);
56+
}
4557

46-
#[Test]
47-
public function itValidatesAField() {
48-
$validatorFactory = new ValidatorsFactory();
49-
$validator = $validatorFactory->create(['required', 'email']);
50-
$errors = $validator->validate('user@example.com');
51-
$this->assertEmpty($errors);
52-
}
58+
#[Test]
59+
public function itDontValidatesAField()
60+
{
61+
$validatorFactory = new ValidatorsFactory();
62+
$validator = $validatorFactory->create(['required', 'url']);
63+
$errors = $validator->validate('user@example.com');
64+
$this->assertNotEmpty($errors);
65+
$this->assertContains('The input should be an url', $errors);
66+
}
5367

54-
#[Test]
55-
public function itDontValidatesAField() {
56-
$validatorFactory = new ValidatorsFactory();
57-
$validator = $validatorFactory->create(['required', 'url']);
58-
$errors = $validator->validate('user@example.com');
59-
$this->assertNotEmpty($errors);
60-
$this->assertContains('The input should be an url', $errors);
61-
}
68+
#[Test]
69+
public function itValidatesAValueWithASingleRule()
70+
{
71+
$validatorFactory = new ValidatorsFactory();
72+
$validator = $validatorFactory->create('required');
73+
$errors = $validator->validate('something');
74+
$this->assertEmpty($errors);
75+
}
6276

63-
#[Test]
64-
public function itValidatesAValueWithASingleRule() {
65-
$validatorFactory = new ValidatorsFactory();
66-
$validator = $validatorFactory->create('required');
67-
$errors = $validator->validate('something');
68-
$this->assertEmpty($errors);
69-
}
70-
71-
#[Test]
72-
public function itValidatesAValueWithASingleParametrizedRule() {
73-
$validatorFactory = new ValidatorsFactory();
74-
$validator = $validatorFactory->create('max:30');
75-
$errors = $validator->validate('something');
76-
$this->assertEmpty($errors);
77-
}
77+
#[Test]
78+
public function itValidatesAValueWithASingleParametrizedRule()
79+
{
80+
$validatorFactory = new ValidatorsFactory();
81+
$validator = $validatorFactory->create('max:30');
82+
$errors = $validator->validate('something');
83+
$this->assertEmpty($errors);
84+
}
7885

79-
}
86+
}

0 commit comments

Comments
 (0)