src/Entity/Dossier/Dossier.php line 11

Open in your IDE?
  1. <?php
  2. namespace App\Entity\Dossier;
  3. use App\Entity\EntityId;
  4. use Doctrine\ORM\Mapping as ORM;
  5. use Webmozart\Assert\Assert;
  6. #[ORM\Entity]
  7. #[ORM\Table(name: 'dossier')]
  8. class Dossier
  9. {
  10. public const int MAX_LENGTH_BEZEICHNUNG = 128;
  11. public const int MAX_LENGTH_URLSLUG = 255;
  12. #[ORM\Column(name: 'bezeichnung', type: 'string', length: 128, nullable: true)]
  13. private ?string $bezeichnung = null;
  14. #[ORM\Column(name: 'sort', type: 'integer', nullable: true)]
  15. private int $sort = 0;
  16. #[ORM\Column(name: 'url_slug', type: 'string', length: 255, unique: true, nullable: true)]
  17. private ?string $urlSlug = null;
  18. public function __construct(
  19. #[ORM\Id]
  20. #[ORM\Column(name: "id", type: "entityId")]
  21. #[ORM\GeneratedValue(strategy: "NONE")]
  22. private EntityId $id,
  23. string $bezeichnung
  24. )
  25. {
  26. $this->setBezeichnung($bezeichnung);
  27. $this->setUrlSlug($bezeichnung); // erste position
  28. }
  29. public function change($bezeichnung): void
  30. {
  31. $this->setBezeichnung($bezeichnung);
  32. $this->setUrlSlug($bezeichnung);
  33. }
  34. public function id(): EntityId
  35. {
  36. return $this->id;
  37. }
  38. public function bezeichnung(): ?string
  39. {
  40. return $this->bezeichnung;
  41. }
  42. public function sort(): int
  43. {
  44. return $this->sort;
  45. }
  46. public function urlSlug(): ?string
  47. {
  48. return $this->urlSlug;
  49. }
  50. private function setBezeichnung(string $bezeichnung): void
  51. {
  52. Assert::minLength($bezeichnung, 1);
  53. Assert::maxLength($bezeichnung, self::MAX_LENGTH_BEZEICHNUNG);
  54. $this->bezeichnung = $bezeichnung;
  55. }
  56. private function setUrlSlug($string): void
  57. {
  58. // all chars lowercase
  59. $urlSlug = mb_strtolower((string) $string, 'UTF-8');
  60. // äöü... ersetzen
  61. $replacement = ['ä' => 'ae', 'ö' => 'oe', 'ü' => 'ue', 'è' => 'e', 'é' => 'e', 'à' => 'a'];
  62. foreach ($replacement as $i => $u) {
  63. $urlSlug = mb_ereg_replace($i, $u, $urlSlug);
  64. }
  65. // remove all special characters
  66. $urlSlug = preg_replace('/[^a-zA-Z0-9]/', ' ', $urlSlug);
  67. $urlSlug = str_replace('.', ' ', (string) $urlSlug);
  68. // remove double or more space repeats between words chunk
  69. $urlSlug = preg_replace('/\s+/', ' ', $urlSlug);
  70. // remove white space and dot characters from both side
  71. $urlSlug = trim((string) $urlSlug, ' ');
  72. // fill spaces with hyphens
  73. $urlSlug = preg_replace('/\s+/', '-', $urlSlug);
  74. Assert::notEmpty($urlSlug);
  75. Assert::maxLength($urlSlug, self::MAX_LENGTH_URLSLUG);
  76. $this->urlSlug = $urlSlug;
  77. }
  78. }