This commit is contained in:
2022-02-05 07:43:27 +00:00
committed by Gitea
parent b26b8bdb98
commit 877d77f28b
34 changed files with 1936 additions and 8 deletions
@@ -0,0 +1,18 @@
<?php
namespace Spatie\DataTransferObject\Exceptions;
use Exception;
use Spatie\DataTransferObject\Caster;
class InvalidCasterClass extends Exception
{
public function __construct(string $className)
{
$expected = Caster::class;
parent::__construct(
"Class `{$className}` doesn't implement {$expected} and can't be used as a caster"
);
}
}
@@ -0,0 +1,15 @@
<?php
namespace Spatie\DataTransferObject\Exceptions;
use Exception;
class UnknownProperties extends Exception
{
public static function new(string $dtoClass, array $fields): self
{
$properties = json_encode($fields);
return new self("Unknown properties provided to `{$dtoClass}`: {$properties}");
}
}
@@ -0,0 +1,27 @@
<?php
namespace Spatie\DataTransferObject\Exceptions;
use Exception;
use Spatie\DataTransferObject\DataTransferObject;
class ValidationException extends Exception
{
public function __construct(
public DataTransferObject $dataTransferObject,
public array $validationErrors,
) {
$className = $dataTransferObject::class;
$messages = [];
foreach ($validationErrors as $fieldName => $errorsForField) {
/** @var \Spatie\DataTransferObject\Validation\ValidationResult $errorForField */
foreach ($errorsForField as $errorForField) {
$messages[] = "\t - `{$className}->{$fieldName}`: {$errorForField->message}";
}
}
parent::__construct("Validation errors:" . PHP_EOL . implode(PHP_EOL, $messages));
}
}