<?php
namespace App\Entity\Dossier;
use App\Entity\EntityId;
use Doctrine\ORM\Mapping as ORM;
use Webmozart\Assert\Assert;
#[ORM\Entity]
#[ORM\Table(name: 'dossier')]
class Dossier
{
public const int MAX_LENGTH_BEZEICHNUNG = 128;
public const int MAX_LENGTH_URLSLUG = 255;
#[ORM\Column(name: 'bezeichnung', type: 'string', length: 128, nullable: true)]
private ?string $bezeichnung = null;
#[ORM\Column(name: 'sort', type: 'integer', nullable: true)]
private int $sort = 0;
#[ORM\Column(name: 'url_slug', type: 'string', length: 255, unique: true, nullable: true)]
private ?string $urlSlug = null;
public function __construct(
#[ORM\Id]
#[ORM\Column(name: "id", type: "entityId")]
#[ORM\GeneratedValue(strategy: "NONE")]
private EntityId $id,
string $bezeichnung
)
{
$this->setBezeichnung($bezeichnung);
$this->setUrlSlug($bezeichnung); // erste position
}
public function change($bezeichnung): void
{
$this->setBezeichnung($bezeichnung);
$this->setUrlSlug($bezeichnung);
}
public function id(): EntityId
{
return $this->id;
}
public function bezeichnung(): ?string
{
return $this->bezeichnung;
}
public function sort(): int
{
return $this->sort;
}
public function urlSlug(): ?string
{
return $this->urlSlug;
}
private function setBezeichnung(string $bezeichnung): void
{
Assert::minLength($bezeichnung, 1);
Assert::maxLength($bezeichnung, self::MAX_LENGTH_BEZEICHNUNG);
$this->bezeichnung = $bezeichnung;
}
private function setUrlSlug($string): void
{
// all chars lowercase
$urlSlug = mb_strtolower((string) $string, 'UTF-8');
// äöü... ersetzen
$replacement = ['ä' => 'ae', 'ö' => 'oe', 'ü' => 'ue', 'è' => 'e', 'é' => 'e', 'à' => 'a'];
foreach ($replacement as $i => $u) {
$urlSlug = mb_ereg_replace($i, $u, $urlSlug);
}
// remove all special characters
$urlSlug = preg_replace('/[^a-zA-Z0-9]/', ' ', $urlSlug);
$urlSlug = str_replace('.', ' ', (string) $urlSlug);
// remove double or more space repeats between words chunk
$urlSlug = preg_replace('/\s+/', ' ', $urlSlug);
// remove white space and dot characters from both side
$urlSlug = trim((string) $urlSlug, ' ');
// fill spaces with hyphens
$urlSlug = preg_replace('/\s+/', '-', $urlSlug);
Assert::notEmpty($urlSlug);
Assert::maxLength($urlSlug, self::MAX_LENGTH_URLSLUG);
$this->urlSlug = $urlSlug;
}
}