Skip to content

Commit

Permalink
Added Arrayable support for SimpleDTO (#693)
Browse files Browse the repository at this point in the history
* Added Arrayable support for SimpleDTO

* feat: Refactor SimpleDTO's isArrayable method

The code changes in `SimpleDTO.php` refactor the `isArrayable` method to improve readability and maintainability. The changes reorder the conditions to prioritize commonly used types and remove unnecessary checks. This ensures that the method accurately determines if a value is arrayable.

---------

Co-authored-by: Deeka Wong <8337659+huangdijia@users.noreply.github.com>
  • Loading branch information
huangdijia and huangdijia authored Jul 24, 2024
1 parent 4feeaf8 commit e79eb23
Showing 1 changed file with 3 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/SimpleDTO.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
use FriendsOfHyperf\ValidatedDTO\Exception\MissingCastTypeException;
use Hyperf\Collection\Collection;
use Hyperf\Context\ApplicationContext;
use Hyperf\Contract\Arrayable;
use Hyperf\Contract\CastsAttributes;
use Hyperf\Contract\ConfigInterface;
use Hyperf\Contract\ValidatorInterface;
Expand Down Expand Up @@ -450,6 +451,7 @@ private function isArrayable(mixed $value): bool
|| $value instanceof Collection
|| $value instanceof ValidatedDTO
|| $value instanceof Model
|| $value instanceof Arrayable
|| (is_object($value) && ! ($value instanceof UploadedFile));
}

Expand All @@ -463,6 +465,7 @@ private function formatArrayableValue(mixed $value): array|int|string
$value instanceof Collection => $this->transformCollectionToArray($value),
$value instanceof Model => $this->transformModelToArray($value),
$value instanceof SimpleDTO => $this->transformDTOToArray($value),
$value instanceof Arrayable => $value->toArray(),
is_object($value) => (array) $value,
default => [],
};
Expand Down

0 comments on commit e79eb23

Please sign in to comment.