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
12 changes: 6 additions & 6 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 12 additions & 11 deletions src/ErrorInfo.php → src/ErrorHolder.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
use Exception;
use Respect\Validation\Exceptions\NestedValidationException as RespectNestedValidationException;

class ErrorInfo
class ErrorHolder
{
private Context $context;

Expand Down Expand Up @@ -62,18 +62,19 @@ public function hasErrors(): bool
public function addError(Exception|string $error): void
{
$propertyPath = $this->context->getOptional('internal.currentProperty', []);
$errors = &$this->errors;
foreach ($propertyPath as $property) {
if (! isset($errors[$property])) {
$errors[$property] = [];
}

$errors = &$errors[$property];
}
$fullPropertyPath = implode('.', $propertyPath);
if ($error instanceof RespectNestedValidationException) {
$errors += array_values($error->getMessages());
foreach (array_values($error->getMessages()) as $message) {
$this->errors[] = [
'field' => $fullPropertyPath,
'reason' => $message,
];
}
} else {
$errors[] = is_string($error) ? $error : $error->getMessage();
$this->errors[] = [
'field' => $fullPropertyPath,
'reason' => is_string($error) ? $error : $error->getMessage(),
];
}

if ($this->context->get('option.stopFirstError')) {
Expand Down
12 changes: 6 additions & 6 deletions src/Exceptions/BaseException.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,22 @@

namespace Attributes\Validation\Exceptions;

use Attributes\Validation\ErrorInfo;
use Attributes\Validation\ErrorHolder;
use Exception;
use Throwable;

abstract class BaseException extends Exception
{
private ?ErrorInfo $result;
private ?ErrorHolder $errorHolder;

public function __construct(string $message, ?ErrorInfo $result = null, ?Throwable $previous = null)
public function __construct(string $message, ?ErrorHolder $errorHolder = null, ?Throwable $previous = null)
{
$this->result = $result;
$this->errorHolder = $errorHolder;
parent::__construct($message, 0, $previous);
}

public function getInfo(): ?ErrorInfo
public function getErrors(): array
{
return $this->result;
return $this->errorHolder ? $this->errorHolder->getErrors() : [];
}
}
10 changes: 1 addition & 9 deletions src/Property.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,10 @@ class Property

private mixed $value;

private string $modelClass;

public function __construct(ReflectionProperty $property, mixed $value, string $modelClass)
public function __construct(ReflectionProperty $property, mixed $value)
{
$this->property = $property;
$this->value = $value;
$this->modelClass = $modelClass;
}

public function getName(): string
Expand All @@ -40,9 +37,4 @@ public function getReflection(): ReflectionProperty
{
return $this->property;
}

public function getModelClass(): string
{
return $this->modelClass;
}
}
10 changes: 10 additions & 0 deletions src/Types/ArrArr.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

namespace Attributes\Validation\Types;

class ArrArr extends ArrayOf
{
private array $type;
}
19 changes: 19 additions & 0 deletions src/Types/ArrayOf.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

declare(strict_types=1);

namespace Attributes\Validation\Types;

use ArrayObject;
use Attributes\Validation\Exceptions\ValidationException;

abstract class ArrayOf extends ArrayObject
{
public function __construct(array $array = [])
{
if (! property_exists($this, 'type')) {
throw new ValidationException('Missing property \'type\' in '.self::class);
}
parent::__construct($array);
}
}
10 changes: 10 additions & 0 deletions src/Types/BoolArr.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

namespace Attributes\Validation\Types;

class BoolArr extends ArrayOf
{
private bool $type;
}
10 changes: 10 additions & 0 deletions src/Types/FloatArr.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

namespace Attributes\Validation\Types;

class FloatArr extends ArrayOf
{
private float $type;
}
10 changes: 10 additions & 0 deletions src/Types/IntArr.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

namespace Attributes\Validation\Types;

class IntArr extends ArrayOf
{
private int $type;
}
10 changes: 10 additions & 0 deletions src/Types/ObjArr.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

namespace Attributes\Validation\Types;

class ObjArr extends ArrayOf
{
private object $type;
}
10 changes: 10 additions & 0 deletions src/Types/StrArr.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

namespace Attributes\Validation\Types;

class StrArr extends ArrayOf
{
private string $type;
}
6 changes: 3 additions & 3 deletions src/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ public function validate(array $data, string|object $model): object

$validModel = is_string($model) ? new $model : $model;
$reflectionClass = new ReflectionClass($validModel);
$errorInfo = $this->context->getOptional(ErrorInfo::class) ?: new ErrorInfo($this->context);
$this->context->set(ErrorInfo::class, $errorInfo, override: true);
$errorInfo = $this->context->getOptional(ErrorHolder::class) ?: new ErrorHolder($this->context);
$this->context->set(ErrorHolder::class, $errorInfo, override: true);
$defaultAliasGenerator = $this->getDefaultAliasGenerator($reflectionClass);
foreach ($reflectionClass->getProperties() as $reflectionProperty) {
$propertyName = $reflectionProperty->getName();
Expand All @@ -89,7 +89,7 @@ public function validate(array $data, string|object $model): object
}

$propertyValue = $data[$aliasName];
$property = new Property($reflectionProperty, $propertyValue, $validModel::class);
$property = new Property($reflectionProperty, $propertyValue);
$this->context->set(Property::class, $property, override: true);

try {
Expand Down
4 changes: 2 additions & 2 deletions src/Validators/AttributesValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace Attributes\Validation\Validators;

use Attributes\Validation\Context;
use Attributes\Validation\ErrorInfo;
use Attributes\Validation\ErrorHolder;
use Attributes\Validation\Exceptions\ContextPropertyException;
use Attributes\Validation\Property;
use ReflectionAttribute;
Expand Down Expand Up @@ -33,7 +33,7 @@ public function validate(Property $property, Context $context): void
return;
}

$errorInfo = $context->get(ErrorInfo::class);
$errorInfo = $context->get(ErrorHolder::class);
foreach ($allAttributes as $attribute) {
$className = $attribute->getName();
if ($className == Rules\DateTime::class) {
Expand Down
4 changes: 2 additions & 2 deletions src/Validators/ChainValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace Attributes\Validation\Validators;

use Attributes\Validation\Context;
use Attributes\Validation\ErrorInfo;
use Attributes\Validation\ErrorHolder;
use Attributes\Validation\Exceptions\ContinueValidationException;
use Attributes\Validation\Exceptions\ValidationException;
use Attributes\Validation\Property;
Expand All @@ -25,7 +25,7 @@ class ChainValidator implements PropertyValidator
*/
public function validate(Property $property, Context $context): void
{
$errorInfo = $context->get(ErrorInfo::class);
$errorInfo = $context->get(ErrorHolder::class);
foreach ($this->allValidators as $validator) {
try {
$validator->validate($property, $context);
Expand Down
7 changes: 6 additions & 1 deletion src/Validators/TypeHintValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Attributes\Validation\Validators;

use ArrayObject;
use Attributes\Validation\Context;
use Attributes\Validation\Exceptions\ContextPropertyException;
use Attributes\Validation\Exceptions\ValidationException;
Expand Down Expand Up @@ -138,8 +139,8 @@ private function getDefaultRules(): array
'enum' => new TypeValidators\RawEnum,
'null' => new TypeValidators\RawNull,
DateTime::class => new TypeValidators\DateTime,
DateTimeInterface::class => new TypeValidators\DateTime,
'interface' => new TypeValidators\StrictType,
ArrayObject::class => new TypeValidators\ArrayObject,
'default' => new TypeValidators\AnyClass,
];
}
Expand All @@ -156,6 +157,10 @@ public function getTypeValidator(ReflectionNamedType|ReflectionType $propertyTyp
$typeHintName = $propertyType->getName();
$typeName = isset($this->typeHintRules[$typeHintName]) ? $typeHintName : 'default';
if ($typeName == 'default') {
if (is_subclass_of($typeHintName, ArrayObject::class)) {
return $this->typeHintRules[ArrayObject::class];
}

if (enum_exists($typeHintName)) {
return $this->typeHintRules['enum'];
}
Expand Down
Loading