Skip to content
Snippets Groups Projects
NewsMapper.php 2.34 KiB
Newer Older
<?php

namespace App\Mapper;

use App\Entity\News;
use App\Entity\NewsCategory;
use App\Model\NewsDetailElement;
use App\Model\NewsFilterVariants;
use App\Model\NewsList;
use App\Model\NewsListingElement;
use App\Model\NewsCategory as NewsCategoryModel;
use App\Model\Pagination;
use Ramsey\Collection\Collection;

class NewsMapper
{
    public static function mapToNewsList(
        Collection $news,
        Collection $newsCategory,
        int $page,
        int $limit,
        int $count
    ): NewsList {
        return new NewsList(
            new Pagination($page, ceil($count / $limit), $limit),
            new Collection(NewsListingElement::class, array_map(
                function (News $newsOne) {
                    return self::mapToListingElement($newsOne);
                }, $news->toArray())
            ),
            new NewsFilterVariants(
                new Collection(NewsCategoryModel::class, array_map(
                    function (NewsCategory $newsCategoryOne) {
                        return self::mapToNewsCategory($newsCategoryOne);
                    }, $newsCategory->toArray())
                )
            )
        );
    }

    public static function mapToListingElement(News $newsOne): NewsListingElement
    {
        return new NewsListingElement(
            $newsOne->getId(),
            $newsOne->getPreviewText(),
            $newsOne->getDetailText(),
            FileMapper::mapToFile($newsOne->getFile()),
            $newsOne->getCreateAt()?->format('d.m.Y'),
            "/{$newsOne->getCode()}/code/"
        );
    }

    public static function mapToDetailElement(News $newsOne): NewsDetailElement
    {
        return new NewsDetailElement(
            $newsOne->getId(),
            $newsOne->getPreviewText(),
            $newsOne->getDetailText(),
            $newsOne->getDetailText(),
            FileMapper::mapToFile($newsOne->getFile()),
            $newsOne->getCreateAt()?->format('d.m.Y'),
            $newsOne->getSeo()?->getTitle(),
            $newsOne->getSeo()?->getDescription(),
            $newsOne->getSeo()?->getKeywords()
    public static function mapToNewsCategory(
        NewsCategory $newsCategory
    ): NewsCategoryModel {
        return new NewsCategoryModel(
            $newsCategory->getId(),
            $newsCategory->getName(),
            $newsCategory->getCode(),
        );
    }
}