Skip to content

Commit

Permalink
Upgrade ValidatedDTO (#266)
Browse files Browse the repository at this point in the history
* Upgrade ValidatedDTO

* Fix pest error in PHP 8.0

---------

Co-authored-by: Deeka Wong <[email protected]>
  • Loading branch information
huangdijia and huangdijia authored Jul 6, 2023
1 parent 738c991 commit b1869fe
Show file tree
Hide file tree
Showing 6 changed files with 535 additions and 144 deletions.
23 changes: 23 additions & 0 deletions publish/dto.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);
/**
* This file is part of friendsofhyperf/components.
*
* @link https://github.com/friendsofhyperf/components
* @document https://github.com/friendsofhyperf/components/blob/3.0/README.md
* @contact [email protected]
*/
return [
/*
|--------------------------------------------------------------------------
| REQUIRE CASTING
|--------------------------------------------------------------------------
|
| If this is set to true, you must configure a cast type for all properties of your DTOs.
| If a property doesn't have a cast type configured it will throw a
| \FriendsOfHyperf\ValidatedDTO\Exceptions\MissingCastTypeException exception
|
*/
'require_casting' => false,
];
9 changes: 5 additions & 4 deletions src/Casting/DTOCast.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

use FriendsOfHyperf\ValidatedDTO\Exception\CastException;
use FriendsOfHyperf\ValidatedDTO\Exception\CastTargetException;
use FriendsOfHyperf\ValidatedDTO\ValidatedDTO;
use FriendsOfHyperf\ValidatedDTO\SimpleDTO;
use Hyperf\Validation\ValidationException;
use Throwable;

Expand All @@ -25,7 +25,7 @@ public function __construct(private string $dtoClass)
/**
* @throws CastException|CastTargetException|ValidationException
*/
public function cast(string $property, mixed $value): ValidatedDTO
public function cast(string $property, mixed $value): SimpleDTO
{
if (is_string($value)) {
$value = json_decode($value, true);
Expand All @@ -39,11 +39,12 @@ public function cast(string $property, mixed $value): ValidatedDTO
$dto = new $this->dtoClass($value);
} catch (ValidationException $exception) {
throw $exception;
} catch (Throwable) {
} catch (Throwable $exception) {
throw $exception;
throw new CastException($property);
}

if (! $dto instanceof ValidatedDTO) {
if (! $dto instanceof SimpleDTO) {
throw new CastTargetException($property);
}

Expand Down
38 changes: 38 additions & 0 deletions src/Concerns/Wireable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

declare(strict_types=1);
/**
* This file is part of friendsofhyperf/components.
*
* @link https://github.com/friendsofhyperf/components
* @document https://github.com/friendsofhyperf/components/blob/3.0/README.md
* @contact [email protected]
*/
namespace FriendsOfHyperf\ValidatedDTO\Concerns;

use stdClass;

trait Wireable
{
public static function fromLivewire($value)
{
if (is_array($value)) {
return new static($value);
}

if (is_object($value) && method_exists($value, 'toArray')) {
return new static($value->toArray());
}

if ($value instanceof stdClass) {
return new static((array) $value);
}

return new static([]);
}

public function toLivewire(): array
{
return $this->toArray();
}
}
9 changes: 9 additions & 0 deletions src/ConfigProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,15 @@ public function __invoke(): array
'commands' => [
Command\GeneratorCommand::class,
],

'publish' => [
[
'id' => 'config',
'description' => 'The config file of dto.',
'source' => __DIR__ . '/../publish/dto.php',
'destination' => BASE_PATH . '/config/autoload/dto.php',
],
],
];
}
}
Loading

0 comments on commit b1869fe

Please sign in to comment.