src/Entity/EntityLog.php line 15

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use App\Enum\EntityLog\Type;
  4. use App\Library\Utils\Dev\ReflectionUtils\ReflectionUtils;
  5. use App\Repository\EntityLogRepository;
  6. use App\Service\EntityLog\EntityLogService;
  7. use App\Service\EntityService;
  8. use Doctrine\ORM\Mapping as ORM;
  9. /**
  10.  * @ORM\Entity(repositoryClass=EntityLogRepository::class)
  11.  */
  12. class EntityLog
  13. {
  14.     /**
  15.      * @ORM\Id
  16.      * @ORM\GeneratedValue
  17.      * @ORM\Column(type="integer")
  18.      */
  19.     private $id;
  20.     /**
  21.      * @ORM\Column(type="string", length=255)
  22.      */
  23.     private $entityClass;
  24.     /**
  25.      * @ORM\Column(type="enum", options={"values"="entity_log_enum", "default"="update"})
  26.      */
  27.     private $type Type::TYPE_UPDATE;
  28.     /**
  29.      * @ORM\Column(type="datetime")
  30.      */
  31.     private $createdAt;
  32.     /**
  33.      * @ORM\Column(type="string", length=255, nullable=true)
  34.      */
  35.     private $fieldName;
  36.     /**
  37.      * @ORM\Column(type="text", nullable=true)
  38.      */
  39.     private $oldValue;
  40.     /**
  41.      * @ORM\Column(type="text", nullable=true)
  42.      */
  43.     private $newValue;
  44.     /**
  45.      * @ORM\Column(type="integer", nullable=true)
  46.      */
  47.     private $entityId;
  48.     /**
  49.      * @ORM\ManyToOne(targetEntity=User::class)
  50.      * @ORM\JoinColumn(nullable=true)
  51.      */
  52.     private $editedBy;
  53.     /**
  54.      * @ORM\ManyToOne(targetEntity=Student::class)
  55.      * @ORM\JoinColumn(nullable=true)
  56.      */
  57.     private $student;
  58.     /**
  59.      * @ORM\ManyToOne(targetEntity=Person::class)
  60.      * @ORM\JoinColumn(nullable=true)
  61.      */
  62.     private $person;
  63.     /**
  64.      * @ORM\ManyToOne(targetEntity=UserSettings::class)
  65.      * @ORM\JoinColumn(nullable=true)
  66.      */
  67.     private $userSettings;
  68.     /**
  69.      * @ORM\ManyToOne(targetEntity=Candidate::class)
  70.      * @ORM\JoinColumn(nullable=true)
  71.      */
  72.     private $candidate;
  73.     /**
  74.      * @ORM\ManyToOne(targetEntity=Payment::class)
  75.      * @ORM\JoinColumn(nullable=true)
  76.      */
  77.     private $payment;
  78.     /**
  79.      * @ORM\Column(type="text", nullable=true)
  80.      */
  81.     private $data;
  82.     /**
  83.      * @ORM\Column(type="text", nullable=true)
  84.      */
  85.     private $oldData;
  86.     /**
  87.      * @ORM\Column(type="string", length=255, nullable=true)
  88.      */
  89.     private $entityText;
  90.     /**
  91.      * @ORM\Column(type="text", nullable=true)
  92.      */
  93.     private $fields;
  94.     /**
  95.      * @ORM\Column(type="datetime", nullable=true)
  96.      */
  97.     private $updatedAt;
  98.     public function __construct()
  99.     {
  100.         $this->createdAt = new \DateTime();
  101.         $this->updatedAt = new \DateTime();
  102.     }
  103.     public function getId(): ?int
  104.     {
  105.         return $this->id;
  106.     }
  107.     public function getEntityClass($full false): ?string
  108.     {
  109.         return ($full "App\\Entity\\" "") . $this->entityClass;
  110.     }
  111.     public function getEntityServiceClass(): ?string
  112.     {
  113.         $result "App\\Service\\" $this->getEntityClass() . "Service";
  114.         if (!class_exists($result)) {
  115.             $result "App\\Service\\" $this->getEntityClass()
  116.                 . "\\" $this->getEntityClass() . "Service";
  117.             if (!class_exists($result)) {
  118.                 $result null;
  119.             }
  120.         }
  121.         return $result;
  122.     }
  123.     public function setEntityClass(string $entityClass): self
  124.     {
  125.         $this->entityClass $entityClass;
  126.         return $this;
  127.     }
  128.     public function getType(): ?string
  129.     {
  130.         return $this->type;
  131.     }
  132.     public function isCreateType(): bool
  133.     {
  134.         return $this->type === Type::TYPE_CREATE;
  135.     }
  136.     public function isUpdateType(): bool
  137.     {
  138.         return $this->type === Type::TYPE_UPDATE;
  139.     }
  140.     public function isDeleteType(): bool
  141.     {
  142.         return $this->type === Type::TYPE_DELETE;
  143.     }
  144.     public function isRecoverType(): bool
  145.     {
  146.         return $this->type === Type::TYPE_RECOVER;
  147.     }
  148.     public function setType(string $type): self
  149.     {
  150.         $this->type $type;
  151.         return $this;
  152.     }
  153.     public function getCreatedAt(): ?\DateTime
  154.     {
  155.         return $this->createdAt;
  156.     }
  157.     public function setCreatedAt(\DateTime $createdAt): self
  158.     {
  159.         $this->createdAt $createdAt;
  160.         return $this;
  161.     }
  162.     public function getFieldName(): ?string
  163.     {
  164.         return $this->fieldName;
  165.     }
  166.     public function setFieldName(?string $fieldName): self
  167.     {
  168.         $this->fieldName $fieldName;
  169.         return $this;
  170.     }
  171.     public function getOldValue(): ?string
  172.     {
  173.         return $this->oldValue;
  174.     }
  175.     public function setOldValue(?string $oldValue): self
  176.     {
  177.         $this->oldValue $oldValue;
  178.         return $this;
  179.     }
  180.     public function getNewValue(): ?string
  181.     {
  182.         return $this->newValue;
  183.     }
  184.     public function setNewValue(?string $newValue): self
  185.     {
  186.         $this->newValue $newValue;
  187.         return $this;
  188.     }
  189.     public function getEntityId(): ?int
  190.     {
  191.         return $this->entityId;
  192.     }
  193.     public function setEntityId(?int $entityId): self
  194.     {
  195.         $this->entityId $entityId;
  196.         return $this;
  197.     }
  198.     public function getEditedBy(): ?User
  199.     {
  200.         return $this->editedBy;
  201.     }
  202.     public function setEditedBy(?User $editedBy): self
  203.     {
  204.         $this->editedBy $editedBy;
  205.         return $this;
  206.     }
  207.     public function getStudent(): ?Student
  208.     {
  209.         return $this->student;
  210.     }
  211.     public function setStudent(?Student $student): self
  212.     {
  213.         $this->student $student;
  214.         return $this;
  215.     }
  216.     public function getPerson(): ?Person
  217.     {
  218.         return $this->person;
  219.     }
  220.     public function setPerson(?Person $person): self
  221.     {
  222.         $this->person $person;
  223.         return $this;
  224.     }
  225.     public function getUserSettings(): ?UserSettings
  226.     {
  227.         return $this->userSettings;
  228.     }
  229.     public function setUserSettings(?UserSettings $userSettings): self
  230.     {
  231.         $this->userSettings $userSettings;
  232.         return $this;
  233.     }
  234.     public function getCandidate(): ?Candidate
  235.     {
  236.         return $this->candidate;
  237.     }
  238.     public function setCandidate(?Candidate $candidate): self
  239.     {
  240.         $this->candidate $candidate;
  241.         return $this;
  242.     }
  243.     public function getPayment(): ?Payment
  244.     {
  245.         return $this->payment;
  246.     }
  247.     public function setPayment(?Payment $payment): self
  248.     {
  249.         $this->payment $payment;
  250.         return $this;
  251.     }
  252.     public function getTypeText(): string
  253.     {
  254.         return Type::getText($this->type);
  255.     }
  256.     public function getTypeCss(): string
  257.     {
  258.         return Type::getCssClass($this->type);
  259.     }
  260.     public function getEntityClassCss(): string
  261.     {
  262.         return EntityService::getEntityClassCssClass("App\\Entity\\{$this->entityClass}");
  263.     }
  264.     public function getEntityClassText(): string
  265.     {
  266.         return EntityService::getEntityClassText($this->entityClass);
  267.     }
  268.     public function getEntity()
  269.     {
  270.         return $this->getPerson() ?? $this->getStudent() ?? $this->getUserSettings() ?? $this->getCandidate() ?? $this->getPayment();
  271.     }
  272.     public function setEntity($entity): self
  273.     {
  274.         if ($entity instanceof Person) {
  275.             return $this->setPerson($entity);
  276.         } elseif ($entity instanceof Student) {
  277.             return $this->setStudent($entity);
  278.         } elseif ($entity instanceof UserSettings) {
  279.             return $this->setUserSettings($entity);
  280.         } elseif ($entity instanceof Candidate) {
  281.             return $this->setCandidate($entity);
  282.         } elseif ($entity instanceof Payment) {
  283.             return $this->setPayment($entity);
  284.         }
  285.         return $this;
  286.     }
  287.     public function getEntityFieldText(): ?string
  288.     {
  289.         $entityClass "App\\Entity\\{$this->entityClass}";
  290.         $text null;
  291.         $entity $this->getEntity();
  292.         if ($entity && method_exists($entityClass'getFieldText')) {
  293.             $text $entityClass::getFieldText($this->fieldName);
  294.         }
  295.         return $text ?: $this->fieldName;
  296.     }
  297.     public function getOldFieldValueText(): ?string
  298.     {
  299.         $oldValue $this->getOldValue();
  300.         return $this->getFieldValueText($oldValue);
  301.     }
  302.     public function getNewFieldValueText(): ?string
  303.     {
  304.         $newValue $this->getNewValue();
  305.         $result $this->getFieldValueText($newValue);
  306.         return $result;
  307.     }
  308.     private function getFieldValueText($val)
  309.     {
  310.         return EntityLogService::entityFieldValueText(
  311.             $this->getEntity(), $this->fieldName$val
  312.         );
  313.     }
  314.     public function getData(): ?array
  315.     {
  316.         return json_decode($this->datatrue);
  317.     }
  318.     public function setData(?array $data): self
  319.     {
  320.         $this->data json_encode($data,
  321.             JSON_UNESCAPED_UNICODE JSON_UNESCAPED_SLASHES);
  322.         return $this;
  323.     }
  324.     public function addData(array $data$addOnlyNew false): self
  325.     {
  326.         $currentData $this->getData() ?: [];
  327.         if ($addOnlyNew) {
  328.            foreach ($data as $key => $value) {
  329.                if (!array_key_exists($key$currentData)) {
  330.                    $currentData[$key] = $value;
  331.                }
  332.            }
  333.         } else {
  334.             $data $this->getDataWithKeyPostfix($datacount($currentData));
  335.             $newData array_merge($currentData$data);
  336.         }
  337.         return $this->setData($newData);
  338.     }
  339.     private function getDataWithKeyPostfix(array $dataint $currentDataCount): array
  340.     {
  341.         $dataWithKeyPostfix = [];
  342.         $postfix $currentDataCount;
  343.         foreach ($data as $key => $value) {
  344.             $dataWithKeyPostfix["{$key}_$postfix"] = $value;
  345.         }
  346.         return $dataWithKeyPostfix;
  347.     }
  348.     public function getOldData(): ?array
  349.     {
  350.         return json_decode($this->oldDatatrue);
  351.     }
  352.     public function setOldData(?array $oldData): self
  353.     {
  354.         $this->oldData json_encode($oldData,
  355.             JSON_UNESCAPED_UNICODE JSON_UNESCAPED_SLASHES);
  356.         return $this;
  357.     }
  358.     public function addOldData(array $oldData$addOnlyNew false): self
  359.     {
  360.         $currentOldData $this->getOldData() ?: [];
  361.         if ($addOnlyNew) {
  362.             foreach ($oldData as $key => $value) {
  363.                 if (!array_key_exists($key$currentOldData)) {
  364.                     $currentOldData[$key] = $value;
  365.                 }
  366.             }
  367.                 return $this->setOldData($currentOldData);
  368.         } else {
  369.             $oldData $this->getDataWithKeyPostfix($oldDatacount($currentOldData));
  370.             $newOldData array_merge($currentOldData$oldData);
  371.             return $this->setOldData($newOldData);
  372.         }
  373.     }
  374.     public function setEntityText(?string $entityText): self
  375.     {
  376.         $this->entityText $entityText;
  377.         return $this;
  378.     }
  379.     public function getEntityText(): ?string
  380.     {
  381.         return $this->entityText;
  382.     }
  383.     public function getFields(): ?string
  384.     {
  385.         return $this->fields;
  386.     }
  387.     public function setFields(?string $fields): self
  388.     {
  389.         $this->fields $fields;
  390.         return $this;
  391.     }
  392.     public function addFieldsIfNotExists(array $fields): self
  393.     {
  394.         $currentFields $this->getFields() ? explode(","$this->getFields()) : [];
  395.         $newFields array_unique(array_merge($currentFields$fields));
  396.         return $this->setFields(implode(","$newFields));
  397.     }
  398.     public function getUpdatedAt($real false): ?\DateTimeInterface
  399.     {
  400.         if (!$real && !$this->updatedAt) {
  401.             return $this->createdAt;
  402.         }
  403.         return $this->updatedAt;
  404.     }
  405.     public function setUpdatedAt(?\DateTimeInterface $updatedAt): self
  406.     {
  407.         $this->updatedAt $updatedAt;
  408.         return $this;
  409.     }
  410.     public function setDataByKey(string $key$value): self
  411.     {
  412.         $data $this->getData() ?: [];
  413.         $data[$key] = $value;
  414.         return $this->setData($data);
  415.     }
  416.     public function setOldDataByKey(string $key$value): self
  417.     {
  418.         $oldData $this->getOldData() ?: [];
  419.         $oldData[$key] = $value;
  420.         return $this->setOldData($oldData);
  421.     }
  422.     public function updateData(array $data): self
  423.     {
  424.         foreach ($data as $key => $value) {
  425.             $this->setDataByKey($key$value);
  426.         }
  427.         return $this;
  428.     }
  429.     public function updateOldData(array $oldData): self
  430.     {
  431.         foreach ($oldData as $key => $value) {
  432.             $this->setOldDataByKey($key$value);
  433.         }
  434.         return $this;
  435.     }
  436. }