Skip to content
Snippets Groups Projects
FileDtoFactory.php 915 B
Newer Older
<?php

namespace App\Shared\DtoFactory;

use App\Shared\Dto\DtoCollection;
use App\Shared\Dto\FileDto;
use App\Shared\Entity\File;
use Doctrine\Common\Collections\Collection;

class FileDtoFactory
{
    public function create(File $file): FileDto
    {
        return new FileDto(
            id: $file->getId(),
            name: $file->getName(),
            description: $file->getDescription(),
            size: $file->getSize(),
            type: $file->getType(),
            url: $file->getUrl(),
        );
    }

    /**
     * @param Collection<File> $files
     * @return DtoCollection<FileDto>
     */
    public function createCollection(Collection $files): DtoCollection
    {
        $filesDto = $files->map(function(File $file) {
            return $this->create($file);
        });

        return new DtoCollection(
            FileDto::class,
            $filesDto->toArray()
        );
    }
}