src/Entity/Document/Block.php line 12

  1. <?php
  2. namespace App\Entity\Document;
  3. use App\Entity\User;
  4. use App\Repository\Document\BlockRepository;
  5. use Doctrine\DBAL\Types\Types;
  6. use Doctrine\ORM\Mapping as ORM;
  7. use App\Entity\Document as Document;
  8. #[ORM\Entity(repositoryClassBlockRepository::class)]
  9. class Block
  10. {
  11.     const TYPE_TEXT 0;
  12.     const TYPE_PAGEBREAK 1;
  13.     #[ORM\Id]
  14.     #[ORM\GeneratedValue]
  15.     #[ORM\Column]
  16.     private ?int $id null;
  17.     #[ORM\Column(typeTypes::TEXT)]
  18.     private ?string $content null;
  19.     #[ORM\Column(length255nullabletrue)]
  20.     private ?string $title null;
  21.     #[ORM\Column(length255nullabletrue)]
  22.     private ?string $description null;
  23.     #[ORM\Column(length255nullabletrue)]
  24.     private ?string $tags null;
  25.     #[ORM\ManyToOne(inversedBy'blocks')]
  26.     #[ORM\JoinColumn(nullabletrue)]
  27.     private ?Document $document null;
  28.     #[ORM\Column(nullabletrue)]
  29.     private ?int $blockorder null;
  30.     #[ORM\ManyToOne]
  31.     private ?User $owner null;
  32.     #[ORM\Column(nullabletrue)]
  33.     private ?int $idInDoc null// identifies current block within the document
  34.     #[ORM\ManyToOne(inversedBy'blocks')]
  35.     private ?BlockCategory $category null;
  36.     #[ORM\Column(options: ["default" => 0])]
  37.     private ?int $type self::TYPE_TEXT;
  38.     public function getId(): ?int
  39.     {
  40.         return $this->id;
  41.     }
  42.     public function getContent(): ?string
  43.     {
  44.         return $this->content;
  45.     }
  46.     /* works the same as getContent but replaces [variables] with associated values
  47.      * $data is an associative array ["variable" => "value", "variable2" => "value2" ...]
  48.     */
  49.     public function getHydratedContent($data): ?string
  50.     {
  51.         $search = [];
  52.         $replace = [];
  53.         foreach($data as $key => $value) {
  54.             $search[] = "[".$key."]";
  55.             $replace[] = $value;
  56.         }
  57.         $hydrated str_replace($search$replace$this->getContent());
  58.         return $hydrated;
  59.     }
  60.     public function setContent(string $content): self
  61.     {
  62.         $this->content $content;
  63.         return $this;
  64.     }
  65.     public function getTitle(): ?string
  66.     {
  67.         return $this->title;
  68.     }
  69.     public function setTitle(?string $title): self
  70.     {
  71.         $this->title $title;
  72.         return $this;
  73.     }
  74.     public function getDescription(): ?string
  75.     {
  76.         return $this->description;
  77.     }
  78.     public function setDescription(?string $description): self
  79.     {
  80.         $this->description $description;
  81.         return $this;
  82.     }
  83.     public function getTags(): ?string
  84.     {
  85.         return $this->tags;
  86.     }
  87.     public function setTags(?string $tags): self
  88.     {
  89.         $this->tags $tags;
  90.         return $this;
  91.     }
  92.     public function getDocument(): ?Document
  93.     {
  94.         return $this->document;
  95.     }
  96.     public function setDocument(?Document $document): self
  97.     {
  98.         $this->document $document;
  99.         return $this;
  100.     }
  101.     public function getBlockorder(): ?int
  102.     {
  103.         return $this->blockorder;
  104.     }
  105.     public function setBlockorder(int $blockorder): self
  106.     {
  107.         $this->blockorder $blockorder;
  108.         return $this;
  109.     }
  110.     public static function clone(Block $source)
  111.     {
  112.         $block = new Block();
  113.         $block->setTags($source->getTags());
  114.         $block->setDescription($source->getDescription());
  115.         $block->setTitle($source->getTitle());
  116.         $block->setContent($source->getContent());
  117.         $block->setBlockorder($source->getBlockorder());
  118.         $block->setOwner($source->getOwner());
  119.         $block->setIdInDoc($source->getIdInDoc());
  120.         return $block;
  121.     }
  122.     public function getOwner(): ?User
  123.     {
  124.         return $this->owner;
  125.     }
  126.     public function setOwner(?User $owner): self
  127.     {
  128.         $this->owner $owner;
  129.         return $this;
  130.     }
  131.     /**
  132.      * @return int|null
  133.      * documentId is a way to identify a block within a document, for comparison purposes between different versions.
  134.      */
  135.     public function getIdInDoc(): ?int
  136.     {
  137.         return $this->idInDoc;
  138.     }
  139.     public function setIdInDoc(?int $idInDoc): self
  140.     {
  141.         $this->idInDoc $idInDoc;
  142.         return $this;
  143.     }
  144.     public function toArray()
  145.     {
  146.         return  [
  147.             "id" => $this->getId(),
  148.             "tags" => $this->getTags(),
  149.             "description" => $this->getDescription(),
  150.             "title" => $this->getTitle(),
  151.             "content" => $this->getContent(),
  152.             "categoryTitle" => $this->getCategory() ? $this->getCategory()->getTitle() : "",
  153.             "categoryId" => $this->getCategory() ? $this->getCategory()->getId() : "0"
  154.         ];
  155.     }
  156.     public function getCategory(): ?BlockCategory
  157.     {
  158.         return $this->category;
  159.     }
  160.     public function setCategory(?BlockCategory $category): self
  161.     {
  162.         $this->category $category;
  163.         return $this;
  164.     }
  165.     public function getType(): ?int
  166.     {
  167.         return $this->type;
  168.     }
  169.     public function setType(int $type): static
  170.     {
  171.         $this->type $type;
  172.         return $this;
  173.     }
  174. }