78 lines
1.6 KiB
PHP
78 lines
1.6 KiB
PHP
<?php
|
|
|
|
namespace App\Model;
|
|
|
|
use Doctrine\Common\Collections\ArrayCollection;
|
|
use Doctrine\Common\Collections\Collection;
|
|
use Symfony\Component\Uid\Uuid;
|
|
use Symfony\Component\Validator\Constraints as Assert;
|
|
|
|
class UploadSessionDto
|
|
{
|
|
private string $uid;
|
|
|
|
#[Assert\Count(min: 1, minMessage: 'Bitte mindestens eine Datei hochladen')]
|
|
private Collection $uploads;
|
|
|
|
public function __construct(?string $uid = null)
|
|
{
|
|
$this->uid = $uid ?? Uuid::v4();
|
|
$this->uploads = new ArrayCollection();
|
|
}
|
|
|
|
public function getUid(): string
|
|
{
|
|
return $this->uid;
|
|
}
|
|
|
|
public function addUpload(UploadDto $upload): static
|
|
{
|
|
if (false === $this->uploads->contains($upload)) {
|
|
$this->uploads[] = $upload;
|
|
}
|
|
|
|
return $this;
|
|
}
|
|
|
|
public function removeUpload(string $uuid): static
|
|
{
|
|
$uploadToRemove = $this->uploads->filter(function (UploadDto $upload) use ($uuid) {
|
|
return $uuid === $upload->getUuid();
|
|
})->first();
|
|
|
|
if (null !== $uploadToRemove) {
|
|
$this->uploads->removeElement($uploadToRemove);
|
|
}
|
|
|
|
return $this;
|
|
}
|
|
|
|
public function getUploads(): Collection
|
|
{
|
|
return $this->uploads;
|
|
}
|
|
|
|
public function flushUploads(): static
|
|
{
|
|
$this->uploads->clear();
|
|
|
|
return $this;
|
|
}
|
|
|
|
public function getSize(): int
|
|
{
|
|
$size = 0;
|
|
|
|
foreach ($this->uploads as $upload) {
|
|
$size += $upload->getSize();
|
|
}
|
|
|
|
return $size;
|
|
}
|
|
|
|
public function getCount(): int
|
|
{
|
|
return $this->uploads->count();
|
|
}
|
|
}
|