Skip to content
Snippets Groups Projects
NewsListingElementDtoFactory.php 1.24 KiB
Newer Older
<?php

namespace App\News\DtoFactory;

use App\News\Dto\NewsListingElementDto;
use App\Shared\Collection\ListingCollection;
use App\Shared\DtoFactory\FileDtoFactory;
use App\Shared\Entity\News;
use Ramsey\Collection\Collection;

class NewsListingElementDtoFactory
{
    public function __construct(
        private readonly FileDtoFactory $fileFactory,

    public function create(News $news): NewsListingElementDto
    {
        return new NewsListingElementDto(
            id: $news->getId(),
            name: $news->getName(),
            createAt: $news->getCreatedAt()?->format('d.m.Y'),
            detailLink: "api/v1/news/" . $news->getId(),
            description: $news->getPreviewText(),
            image: $news->getPreviewImage() !== null
                ? $this->fileFactory->create($news->getPreviewImage())
        );
    }

    /**
     * @param Collection<News> $news
     * @return ListingCollection<NewsListingElementDto>
    public function createCollection(Collection $news): ListingCollection
        return new ListingCollection(
            $news->map(function (News $oneNews) {
                return $this->create($oneNews);
            })
                ->toArray()