src/Entity/CalendarEventKind.php line 12

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use App\Repository\CalendarEventKindRepository;
  4. use App\Service\EntityService;
  5. use Doctrine\ORM\Mapping as ORM;
  6. /**
  7.  * @ORM\Entity(repositoryClass=CalendarEventKindRepository::class)
  8.  */
  9. class CalendarEventKind
  10. {
  11.     /**
  12.      * @ORM\Id
  13.      * @ORM\GeneratedValue
  14.      * @ORM\Column(type="integer")
  15.      */
  16.     private $id;
  17.     /**
  18.      * @ORM\Column(type="string", length=255)
  19.      */
  20.     private $name;
  21.     /**
  22.      * @ORM\Column(type="datetime")
  23.      */
  24.     private $createdAt;
  25.     /**
  26.      * @ORM\Column(type="string", length=255, nullable=true)
  27.      */
  28.     private $nameRegex;
  29.     /**
  30.      * @ORM\Column(type="string", length=20, unique=true)
  31.      */
  32.     private $kindId;
  33.     /**
  34.      * @ORM\ManyToOne(targetEntity=FinanceCategory::class)
  35.      * @ORM\JoinColumn(nullable=true)
  36.      */
  37.     private $financeCategory;
  38.     public function __construct()
  39.     {
  40.         $this->createdAt = new \DateTime();
  41.     }
  42.     public function getId(): ?int
  43.     {
  44.         return $this->id;
  45.     }
  46.     public function getName(): ?string
  47.     {
  48.         return $this->name;
  49.     }
  50.     public function setName(string $name): self
  51.     {
  52.         $this->name $name;
  53.         return $this;
  54.     }
  55.     public function getCreatedAt(): ?\DateTimeInterface
  56.     {
  57.         return $this->createdAt;
  58.     }
  59.     public function setCreatedAt(\DateTimeInterface $createdAt): self
  60.     {
  61.         $this->createdAt $createdAt;
  62.         return $this;
  63.     }
  64.     public function getNameRegex(): ?string
  65.     {
  66.         return $this->nameRegex;
  67.     }
  68.     public function getNameRegexWords(): array
  69.     {
  70.         if (!$this->getNameRegex()) {
  71.             return [];
  72.         }
  73.         $wordsStr $this->getNameRegex();
  74.         return preg_split("/, */"$wordsStr, -1PREG_SPLIT_NO_EMPTY);
  75.     }
  76.     public function getNameFullRegex(): ?string
  77.     {
  78.         static $nonLetterRegex "[^а-яА-ЯёЁ\s\-]";
  79.         if (!$this->getNameRegex()) {
  80.             return null;
  81.         }
  82.         $words $this->getNameRegexWords();
  83. //        $words = array_map(function ($word) use ($nonLetterRegex) {
  84. //            $word = preg_quote($word, "/");
  85. //            $word .= "($nonLetterRegex|$)";
  86. //            return $word;
  87. //        }, $words);
  88.         return "/" implode("|"$words) . "/u";
  89.     }
  90.     public function setNameRegex(?string $nameRegex): self
  91.     {
  92.         $this->nameRegex $nameRegex;
  93.         return $this;
  94.     }
  95.     public function getKindId(): ?string
  96.     {
  97.         return $this->kindId;
  98.     }
  99.     public function setKindId(string $kindId): self
  100.     {
  101.         $this->kindId $kindId;
  102.         return $this;
  103.     }
  104.     public function getFinanceCategory(): ?FinanceCategory
  105.     {
  106.         return $this->financeCategory;
  107.     }
  108.     public function setFinanceCategory(?FinanceCategory $financeCategory): self
  109.     {
  110.         if ($this->getFinanceCategory()
  111.             && !EntityService::isSameEntity($this->getFinanceCategory(), $financeCategory)) {
  112.             //todo другое решение
  113.             //временное решение - создать другой вид мероприятия и использовать его
  114.             throw new \Exception("Нельзя изменить статью финансов вида мероприятия, т.к. "
  115.             "она может использоваться в связанных финансовых транзакциях. Обратитесь к администратору."
  116.             " Возможное решение: создайте новый вид мероприятия с нужной статьей финансов и используйте его.");
  117.         }
  118.         $this->financeCategory $financeCategory;
  119.         return $this;
  120.     }
  121. }