Skip to content

Commit fe3eda3

Browse files
committed
feat: Support array of types
1 parent 2be25cb commit fe3eda3

21 files changed

+581
-82
lines changed

composer.lock

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
use Exception;
3131
use Respect\Validation\Exceptions\NestedValidationException as RespectNestedValidationException;
3232

33-
class ErrorInfo
33+
class ErrorHolder
3434
{
3535
private Context $context;
3636

@@ -62,18 +62,19 @@ public function hasErrors(): bool
6262
public function addError(Exception|string $error): void
6363
{
6464
$propertyPath = $this->context->getOptional('internal.currentProperty', []);
65-
$errors = &$this->errors;
66-
foreach ($propertyPath as $property) {
67-
if (! isset($errors[$property])) {
68-
$errors[$property] = [];
69-
}
70-
71-
$errors = &$errors[$property];
72-
}
65+
$fullPropertyPath = implode('.', $propertyPath);
7366
if ($error instanceof RespectNestedValidationException) {
74-
$errors += array_values($error->getMessages());
67+
foreach (array_values($error->getMessages()) as $message) {
68+
$this->errors[] = [
69+
'field' => $fullPropertyPath,
70+
'reason' => $message,
71+
];
72+
}
7573
} else {
76-
$errors[] = is_string($error) ? $error : $error->getMessage();
74+
$this->errors[] = [
75+
'field' => $fullPropertyPath,
76+
'reason' => is_string($error) ? $error : $error->getMessage(),
77+
];
7778
}
7879

7980
if ($this->context->get('option.stopFirstError')) {

src/Exceptions/BaseException.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,22 @@
44

55
namespace Attributes\Validation\Exceptions;
66

7-
use Attributes\Validation\ErrorInfo;
7+
use Attributes\Validation\ErrorHolder;
88
use Exception;
99
use Throwable;
1010

1111
abstract class BaseException extends Exception
1212
{
13-
private ?ErrorInfo $result;
13+
private ?ErrorHolder $errorHolder;
1414

15-
public function __construct(string $message, ?ErrorInfo $result = null, ?Throwable $previous = null)
15+
public function __construct(string $message, ?ErrorHolder $errorHolder = null, ?Throwable $previous = null)
1616
{
17-
$this->result = $result;
17+
$this->errorHolder = $errorHolder;
1818
parent::__construct($message, 0, $previous);
1919
}
2020

21-
public function getInfo(): ?ErrorInfo
21+
public function getErrors(): array
2222
{
23-
return $this->result;
23+
return $this->errorHolder ? $this->errorHolder->getErrors() : [];
2424
}
2525
}

src/Property.php

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,10 @@ class Property
1212

1313
private mixed $value;
1414

15-
private string $modelClass;
16-
17-
public function __construct(ReflectionProperty $property, mixed $value, string $modelClass)
15+
public function __construct(ReflectionProperty $property, mixed $value)
1816
{
1917
$this->property = $property;
2018
$this->value = $value;
21-
$this->modelClass = $modelClass;
2219
}
2320

2421
public function getName(): string
@@ -40,9 +37,4 @@ public function getReflection(): ReflectionProperty
4037
{
4138
return $this->property;
4239
}
43-
44-
public function getModelClass(): string
45-
{
46-
return $this->modelClass;
47-
}
4840
}

src/Types/ArrArr.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Attributes\Validation\Types;
6+
7+
class ArrArr extends ArrayOf
8+
{
9+
private array $type;
10+
}

src/Types/ArrayOf.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Attributes\Validation\Types;
6+
7+
use ArrayObject;
8+
use Attributes\Validation\Exceptions\ValidationException;
9+
10+
abstract class ArrayOf extends ArrayObject
11+
{
12+
public function __construct(array $array = [])
13+
{
14+
if (! property_exists($this, 'type')) {
15+
throw new ValidationException('Missing property \'type\' in '.self::class);
16+
}
17+
parent::__construct($array);
18+
}
19+
}

src/Types/BoolArr.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Attributes\Validation\Types;
6+
7+
class BoolArr extends ArrayOf
8+
{
9+
private bool $type;
10+
}

src/Types/FloatArr.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Attributes\Validation\Types;
6+
7+
class FloatArr extends ArrayOf
8+
{
9+
private float $type;
10+
}

src/Types/IntArr.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Attributes\Validation\Types;
6+
7+
class IntArr extends ArrayOf
8+
{
9+
private int $type;
10+
}

src/Types/ObjArr.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Attributes\Validation\Types;
6+
7+
class ObjArr extends ArrayOf
8+
{
9+
private object $type;
10+
}

0 commit comments

Comments
 (0)