| @@ 22-69 (lines=48) @@ | ||
| 19 | use Acme\App\Presentation\Web\Core\Port\Paginator\PaginatorFactoryInterface; |
|
| 20 | use Acme\App\Presentation\Web\Core\Port\Paginator\PaginatorInterface; |
|
| 21 | ||
| 22 | final class GetHtmlViewModel implements TemplateViewModelInterface |
|
| 23 | { |
|
| 24 | /** |
|
| 25 | * @var PaginatorInterface [string, string, string, DateTimeInterface, string, string[]] |
|
| 26 | */ |
|
| 27 | private $postList; |
|
| 28 | ||
| 29 | /** |
|
| 30 | * The view model constructor depends on the most raw elements possible. |
|
| 31 | * In this case the view model constructor is private, so that we force the usage of the named constructor. |
|
| 32 | * We want this because it is not possible to have the a constructor with the raw data, since it is a list of data. |
|
| 33 | */ |
|
| 34 | private function __construct(PaginatorInterface $postList) |
|
| 35 | { |
|
| 36 | $this->postList = $postList; |
|
| 37 | } |
|
| 38 | ||
| 39 | /** |
|
| 40 | * We create named constructors for the cases where we need to extract the raw data from complex data structures. |
|
| 41 | */ |
|
| 42 | public static function fromPostDtoList( |
|
| 43 | PaginatorFactoryInterface $paginatorFactory, |
|
| 44 | int $page, |
|
| 45 | LatestPostWithAuthorAndTagsDto ...$postDtoList |
|
| 46 | ): self { |
|
| 47 | $postDataList = []; |
|
| 48 | foreach ($postDtoList as $postDto) { |
|
| 49 | $postDataList[] = [ |
|
| 50 | 'title' => $postDto->getTitle(), |
|
| 51 | 'summary' => $postDto->getSummary(), |
|
| 52 | 'slug' => $postDto->getSlug(), |
|
| 53 | 'publishedAt' => $postDto->getPublishedAt(), |
|
| 54 | 'authorFullName' => $postDto->getAuthorFullName(), |
|
| 55 | 'tagList' => $postDto->getTagList(), |
|
| 56 | ]; |
|
| 57 | } |
|
| 58 | ||
| 59 | $paginator = $paginatorFactory->createPaginator($postDataList); |
|
| 60 | $paginator->setCurrentPage($page); |
|
| 61 | ||
| 62 | return new self($paginator); |
|
| 63 | } |
|
| 64 | ||
| 65 | public function getPostList(): PaginatorInterface |
|
| 66 | { |
|
| 67 | return $this->postList; |
|
| 68 | } |
|
| 69 | } |
|
| 70 | ||
| @@ 22-70 (lines=49) @@ | ||
| 19 | use Acme\App\Presentation\Web\Core\Port\Paginator\PaginatorFactoryInterface; |
|
| 20 | use Acme\App\Presentation\Web\Core\Port\Paginator\PaginatorInterface; |
|
| 21 | ||
| 22 | final class GetXmlViewModel implements TemplateViewModelInterface |
|
| 23 | { |
|
| 24 | /** |
|
| 25 | * @var PaginatorInterface [string, string, string, DateTimeInterface, string, string[]] |
|
| 26 | */ |
|
| 27 | private $postList; |
|
| 28 | ||
| 29 | /** |
|
| 30 | * The view model constructor depends on the most raw elements possible. |
|
| 31 | * In this case the view model constructor is private, so that we force the usage of the named constructor. |
|
| 32 | * We want this because it is not possible to have the a constructor with the raw data, since it is a list of data. |
|
| 33 | */ |
|
| 34 | private function __construct(PaginatorInterface $postList) |
|
| 35 | { |
|
| 36 | $this->postList = $postList; |
|
| 37 | } |
|
| 38 | ||
| 39 | /** |
|
| 40 | * We create named constructors for the cases where we need to extract the raw data from complex data structures. |
|
| 41 | */ |
|
| 42 | public static function fromPostDtoList( |
|
| 43 | PaginatorFactoryInterface $paginatorFactory, |
|
| 44 | int $page, |
|
| 45 | LatestPostWithAuthorAndTagsDto ...$postList |
|
| 46 | ): self { |
|
| 47 | $postDataList = []; |
|
| 48 | foreach ($postList as $post) { |
|
| 49 | $postDataList[] = [ |
|
| 50 | 'title' => $post->getTitle(), |
|
| 51 | 'summary' => $post->getSummary(), |
|
| 52 | 'slug' => $post->getSlug(), |
|
| 53 | 'publishedAt' => $post->getPublishedAt(), |
|
| 54 | 'authorFullName' => $post->getAuthorFullName(), |
|
| 55 | 'authorEmail' => $post->getAuthorEmail(), |
|
| 56 | 'tagList' => $post->getTagList(), |
|
| 57 | ]; |
|
| 58 | } |
|
| 59 | ||
| 60 | $paginator = $paginatorFactory->createPaginator($postDataList); |
|
| 61 | $paginator->setCurrentPage($page); |
|
| 62 | ||
| 63 | return new self($paginator); |
|
| 64 | } |
|
| 65 | ||
| 66 | public function getPostList(): PaginatorInterface |
|
| 67 | { |
|
| 68 | return $this->postList; |
|
| 69 | } |
|
| 70 | } |
|
| 71 | ||