<?php
namespace App\Entity;
use App\Enum\EntityLog\Type;
use App\Library\Utils\Dev\ReflectionUtils\ReflectionUtils;
use App\Repository\EntityLogRepository;
use App\Service\EntityLog\EntityLogService;
use App\Service\EntityService;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity(repositoryClass=EntityLogRepository::class)
*/
class EntityLog
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=255)
*/
private $entityClass;
/**
* @ORM\Column(type="enum", options={"values"="entity_log_enum", "default"="update"})
*/
private $type = Type::TYPE_UPDATE;
/**
* @ORM\Column(type="datetime")
*/
private $createdAt;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $fieldName;
/**
* @ORM\Column(type="text", nullable=true)
*/
private $oldValue;
/**
* @ORM\Column(type="text", nullable=true)
*/
private $newValue;
/**
* @ORM\Column(type="integer", nullable=true)
*/
private $entityId;
/**
* @ORM\ManyToOne(targetEntity=User::class)
* @ORM\JoinColumn(nullable=true)
*/
private $editedBy;
/**
* @ORM\ManyToOne(targetEntity=Student::class)
* @ORM\JoinColumn(nullable=true)
*/
private $student;
/**
* @ORM\ManyToOne(targetEntity=Person::class)
* @ORM\JoinColumn(nullable=true)
*/
private $person;
/**
* @ORM\ManyToOne(targetEntity=UserSettings::class)
* @ORM\JoinColumn(nullable=true)
*/
private $userSettings;
/**
* @ORM\ManyToOne(targetEntity=Candidate::class)
* @ORM\JoinColumn(nullable=true)
*/
private $candidate;
/**
* @ORM\ManyToOne(targetEntity=Payment::class)
* @ORM\JoinColumn(nullable=true)
*/
private $payment;
/**
* @ORM\Column(type="text", nullable=true)
*/
private $data;
/**
* @ORM\Column(type="text", nullable=true)
*/
private $oldData;
public function __construct()
{
$this->createdAt = new \DateTime();
}
public function getId(): ?int
{
return $this->id;
}
public function getEntityClass(): ?string
{
return $this->entityClass;
}
public function setEntityClass(string $entityClass): self
{
$this->entityClass = $entityClass;
return $this;
}
public function getType(): ?string
{
return $this->type;
}
public function setType(string $type): self
{
$this->type = $type;
return $this;
}
public function getCreatedAt(): ?\DateTime
{
return $this->createdAt;
}
public function setCreatedAt(\DateTime $createdAt): self
{
$this->createdAt = $createdAt;
return $this;
}
public function getFieldName(): ?string
{
return $this->fieldName;
}
public function setFieldName(?string $fieldName): self
{
$this->fieldName = $fieldName;
return $this;
}
public function getOldValue(): ?string
{
return $this->oldValue;
}
public function setOldValue(?string $oldValue): self
{
$this->oldValue = $oldValue;
return $this;
}
public function getNewValue(): ?string
{
return $this->newValue;
}
public function setNewValue(?string $newValue): self
{
$this->newValue = $newValue;
return $this;
}
public function getEntityId(): ?int
{
return $this->entityId;
}
public function setEntityId(?int $entityId): self
{
$this->entityId = $entityId;
return $this;
}
public function getEditedBy(): ?User
{
return $this->editedBy;
}
public function setEditedBy(?User $editedBy): self
{
$this->editedBy = $editedBy;
return $this;
}
public function getStudent(): ?Student
{
return $this->student;
}
public function setStudent(?Student $student): self
{
$this->student = $student;
return $this;
}
public function getPerson(): ?Person
{
return $this->person;
}
public function setPerson(?Person $person): self
{
$this->person = $person;
return $this;
}
public function getUserSettings(): ?UserSettings
{
return $this->userSettings;
}
public function setUserSettings(?UserSettings $userSettings): self
{
$this->userSettings = $userSettings;
return $this;
}
public function getCandidate(): ?Candidate
{
return $this->candidate;
}
public function setCandidate(?Candidate $candidate): self
{
$this->candidate = $candidate;
return $this;
}
public function getPayment(): ?Payment
{
return $this->payment;
}
public function setPayment(?Payment $payment): self
{
$this->payment = $payment;
return $this;
}
public function getTypeText(): string
{
return Type::getText($this->type);
}
public function getTypeCss(): string
{
return Type::getCssClass($this->type);
}
public function getEntityClassCss(): string
{
return EntityService::getEntityClassCssClass("App\\Entity\\{$this->entityClass}");
}
public function getEntityText(): string
{
return EntityService::getEntityClassText($this->entityClass);
}
public function getEntity()
{
return $this->getPerson() ?? $this->getStudent() ?? $this->getUserSettings() ?? $this->getCandidate() ?? $this->getPayment();
}
public function setEntity($entity): self
{
if ($entity instanceof Person) {
return $this->setPerson($entity);
} elseif ($entity instanceof Student) {
return $this->setStudent($entity);
} elseif ($entity instanceof UserSettings) {
return $this->setUserSettings($entity);
} elseif ($entity instanceof Candidate) {
return $this->setCandidate($entity);
} elseif ($entity instanceof Payment) {
return $this->setPayment($entity);
}
return $this;
}
public function getEntityFieldText(): ?string
{
$entityClass = "App\\Entity\\{$this->entityClass}";
$text = null;
$entity = $this->getEntity();
if ($entity && method_exists($entityClass, 'getFieldText')) {
$text = $entityClass::getFieldText($this->fieldName);
}
return $text ?: $this->fieldName;
}
public function getOldFieldValueText(): ?string
{
$oldValue = $this->getOldValue();
return $this->getFieldValueText($oldValue);
}
public function getNewFieldValueText(): ?string
{
$newValue = $this->getNewValue();
$result = $this->getFieldValueText($newValue);
return $result;
}
private function getFieldValueText($val)
{
return EntityLogService::entityFieldValueText(
$this->getEntity(), $this->fieldName, $val
);
}
public function getData(): ?array
{
return json_decode($this->data, true);
}
public function setData(?array $data): self
{
$this->data = json_encode($data,
JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
return $this;
}
public function getOldData(): ?array
{
return json_decode($this->oldData, true);
}
public function setOldData(?array $oldData): self
{
$this->oldData = json_encode($oldData,
JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
return $this;
}
}