bundle/w3des/SmsBundle/src/Entity/SmsCity.php line 22

Open in your IDE?
  1. <?php
  2. namespace w3des\SmsBundle\Entity;
  3. use Doctrine\Common\Collections\ArrayCollection;
  4. use Doctrine\Common\Collections\Collection;
  5. use Doctrine\ORM\Mapping as ORM;
  6. use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
  7. use w3des\SmsBundle\Text;
  8. /**
  9. * Słownik miejscowości dla formularza SMS i awarii.
  10. *
  11. * @ORM\Entity()
  12. * @ORM\Table(
  13. * name="sms_cities",
  14. * uniqueConstraints={
  15. * @ORM\UniqueConstraint(name="sms_city_name_uniq", columns={"name"})
  16. * }
  17. * )
  18. * @UniqueEntity(fields={"name"}, message="Ta miejscowość już istnieje.")
  19. */
  20. class SmsCity
  21. {
  22. /**
  23. * @ORM\Id
  24. * @ORM\GeneratedValue
  25. * @ORM\Column(type="integer")
  26. */
  27. protected $id;
  28. /**
  29. * @ORM\Column(type="string")
  30. */
  31. protected $name;
  32. /**
  33. * @ORM\OneToMany(targetEntity="SmsStreet", mappedBy="city", cascade={"persist"}, orphanRemoval=true)
  34. */
  35. protected $streets;
  36. public function __construct()
  37. {
  38. $this->streets = new ArrayCollection();
  39. }
  40. public function getId()
  41. {
  42. return $this->id;
  43. }
  44. public function getName()
  45. {
  46. return $this->name;
  47. }
  48. public function setName($name)
  49. {
  50. $this->name = $name !== null ? Text::clean($name) : null;
  51. return $this;
  52. }
  53. public function getStreets(): Collection
  54. {
  55. return $this->streets;
  56. }
  57. /**
  58. * @return string[] nazwy ulic posortowane alfabetycznie
  59. */
  60. public function getStreetNames(): array
  61. {
  62. $names = [];
  63. foreach ($this->streets as $street) {
  64. $names[] = $street->getName();
  65. }
  66. return Text::sort($names);
  67. }
  68. public function getStreetCount(): int
  69. {
  70. return $this->streets->count();
  71. }
  72. /**
  73. * Skrót listy ulic do tabeli w panelu admina.
  74. */
  75. public function getStreetsSummary(): string
  76. {
  77. $summary = \implode(', ', $this->getStreetNames());
  78. if (\mb_strlen($summary) > 120) {
  79. $summary = \mb_substr($summary, 0, 120) . '…';
  80. }
  81. return $summary;
  82. }
  83. /**
  84. * Ustawia listę ulic: dodaje brakujące, usuwa nieobecne na liście.
  85. * Porównanie bez względu na wielkość liter.
  86. *
  87. * @param string[] $names
  88. */
  89. public function syncStreets(array $names): void
  90. {
  91. $wanted = [];
  92. foreach ($names as $name) {
  93. $name = Text::clean($name);
  94. if ($name !== '' && !isset($wanted[\mb_strtolower($name)])) {
  95. $wanted[\mb_strtolower($name)] = $name;
  96. }
  97. }
  98. foreach ($this->streets->toArray() as $street) {
  99. $key = \mb_strtolower($street->getName());
  100. if (isset($wanted[$key])) {
  101. $street->setName($wanted[$key]);
  102. unset($wanted[$key]);
  103. } else {
  104. $this->streets->removeElement($street);
  105. }
  106. }
  107. foreach ($wanted as $name) {
  108. $this->streets->add((new SmsStreet())->setCity($this)->setName($name));
  109. }
  110. }
  111. public function __toString()
  112. {
  113. return (string) $this->name;
  114. }
  115. }