benycode / dto
data transfer object representation based on laravel validation
v1.0.3
2021-03-17 10:14 UTC
Requires
- php: ^7.4
- ext-json: *
- illuminate/contracts: >=5.5
- illuminate/support: >=5.5
Requires (Dev)
- orchestra/testbench: >=3.5
- phpunit/phpunit: ^8.5
- sempro/phpunit-pretty-print: ^1.2
This package is not auto-updated.
Last update: 2024-11-21 04:15:36 UTC
README
Installation
You can install the package via composer:
composer require benycode/dto
Introduction
Working with arrays is painfully. This package represents simple trait that can makes your life easier. It gives you an ability to convert arrays to Data Transfer Objects and validates it using laravel validator facade.
Here are a couple of examples:
class UserDTO implements Arrayable, JsonSerializable { use DTOTrait; /** * @var int */ private $id; /** * @var string */ private $name; /** * @var string */ private $avatarUrl; /** * UserDTO constructor. * @param int $id * @param string $name * @param string $avatarUrl */ public function __construct(int $id, string $name, string $avatarUrl) { $this->id = $id; $this->name = $name; $this->avatarUrl = $avatarUrl; } /** * @return int */ public function getId(): int { return $this->id; } /** * @return string */ public function getName(): string { return $this->name; } /** * @return string */ public function getAvatarUrl(): string { return $this->avatarUrl; } /** * @inheritDoc */ protected static function createFromArray(array $data): self { return new self( Arr::get($data, 'id'), Arr::get($data, 'name'), Arr::get($data, 'avatar_url') ); } /** * @return array */ protected static function validationRules(): array { return [ 'id' => 'required|integer', 'name' => 'required|string|max:255', 'avatar_url' => 'required|string|url', ]; } /** * @return array */ public function toArray(): array { return [ 'id' => $this->id, 'name' => $this->name, 'avatar_url' => $this->avatarUrl ]; } /** * @inheritDoc */ public function jsonSerialize(): array { return $this->toArray(); } }
Then you can easily do:
try { $dto = UserDTO::fromArray([ 'id' => 1, 'name' => 'Alex', 'avatar_url' => 'http://google.com' ]); dd($dto->toArray(), json_encode($dto)); } catch (ValidationException $exception) { dd($exception->errors()); }
Samples
Samples can be found in /tests/Unit/Samples