Skip to content

Commit

Permalink
feat(user): add DTO/Mapper
Browse files Browse the repository at this point in the history
see: #30
  • Loading branch information
n3wborn committed Nov 1, 2023
1 parent 3c1fda5 commit a291de1
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 0 deletions.
49 changes: 49 additions & 0 deletions src/Service/User/UserDTO.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

namespace App\Service\User;

final class UserDTO
{
public function __construct(
private string $email = '',
private ?string $slug = null,
private array $projects = [],
) {
}

public function getSlug(): ?string
{
return $this->slug;
}

public function setSlug(string $slug): self
{
$this->slug = $slug;

return $this;
}

public function getEmail(): ?string
{
return $this->email;
}

public function setEmail(string $email): self
{
$this->email = $email;

return $this;
}

public function getProjects(): array
{
return $this->projects;
}

public function setProjects(array $projects): self
{
$this->projects = $projects;

return $this;
}
}
29 changes: 29 additions & 0 deletions src/Service/User/UserMapper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace App\Service\User;

use App\Entity\User;
use Symfony\Component\Serializer\Encoder\JsonEncoder;
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
use Symfony\Component\Serializer\Serializer;

final class UserMapper
{
/** @throws ExceptionInterface */
public static function fromEntityToJson(User $user): mixed
{
$dto = self::fromEntityToDTO($user);
$serializer = new Serializer([new ObjectNormalizer()]);

return $serializer->normalize($dto, JsonEncoder::FORMAT);
}

public static function fromEntityToDTO(User $user): UserDTO
{
return new UserDTO(
$user->getEmail(),
$user->getSlug(),
// UserHelper::getCategoriesArrayFromUser($user)
);
}
}

0 comments on commit a291de1

Please sign in to comment.