<?php
namespace App\Entity\Core;
use App\Entity\Core\Topic\Topic;
use DateTime;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
use JsonSerializable;
#[ORM\Table('category')]
#[ORM\Entity(repositoryClass: CategoryRepository::class)]
class Category implements JsonSerializable
{
/**
* @var int
*/
#[ORM\Column(name: 'id', type: 'integer')]
#[ORM\Id]
#[ORM\GeneratedValue(strategy: 'AUTO')]
private $id;
/**
* @var string
*/
#[ORM\Column(name: 'name', type: 'string', length: 255, unique: true)]
private $name;
/**
* @var string
*/
#[ORM\Column(name: 'display_name', type: 'string', length: 255, nullable: true)]
private $displayName;
/**
* Type of a category;
* 0 - normal
* 1 - screening
*
* @var int
*/
#[ORM\Column(name: 'type', type: 'integer', nullable: true, options: ['default' => 0])]
private $type = 0;
/**
* @var Topic[]
*/
#[ORM\OneToMany(targetEntity: Topic::class, mappedBy: 'category', cascade: ['persist'])]
#[ORM\OrderBy(['sortOrder' => 'ASC'])]
private $topics;
/**
* @var bool $enabled
*/
#[ORM\Column(name: 'enabled', type: 'boolean', unique: false, options: ['default' => true])]
private $enabled = true;
/**
* @var bool $show_in_self_study
*/
#[ORM\Column(name: 'show_in_self_study', type: 'boolean', unique: false, options: ['default' => true])]
private $showInSelfStudy = false;
/**
* @var bool $show_in_self_study
*/
#[ORM\Column(name: 'show_in_main_system', type: 'boolean', unique: false, options: ['default' => true])]
private $showInMainSystem = true;
/**
* @var bool $show_in_self_study
*/
#[ORM\Column(name: 'show_only_to_demo_user', type: 'boolean', unique: false, options: ['default' => false])]
private $showOnlyToDemoUser = false;
/**
* @var int
*/
#[ORM\Column(name: 'sort_order', type: 'integer', nullable: true, options: ['default' => 0])]
private $sortOrder;
/**
* @var ?DateTime
*/
#[ORM\Column(name: 'deleted_at', type: 'datetime', nullable: true)]
private ?DateTime $deletedAt;
public function __construct()
{
$this->topics = new ArrayCollection();
}
/**
* Get id.
*
* @return int
*/
public function getId()
{
return $this->id;
}
/**
* Set name.
*
* @param string $name
*
* @return Category
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name.
*
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* @return Topic[]
*/
public function getTopics()
{
return $this->topics;
}
/**
* @return Topic[]
*/
public function getEnabledTopics()
{
return $this->topics->filter(function (Topic $topic) {
return $topic->isEnabled();
});
}
/**
* @return Category
*/
public function addTopic(Topic $topic)
{
$topic->setCategory($this);
$this->topics->add($topic);
return $this;
}
/**
* @return bool
*/
public function removeTopic(Topic $topic)
{
$topic->setCategory(null);
return $this->topics->removeElement($topic);
}
/**
* @return int
*/
public function getType()
{
return $this->type;
}
/**
* @param int $type
*/
public function setType($type)
{
$this->type = $type;
}
public function isEnabled(): bool
{
return $this->enabled;
}
public function setEnabled(bool $enabled): void
{
$this->enabled = $enabled;
}
/**
* @return int
*/
public function getSortOrder()
{
return $this->sortOrder;
}
/**
* @param int $sortOrder
*/
public function setSortOrder($sortOrder)
{
$this->sortOrder = $sortOrder;
}
public function getTopicNames()
{
$topic_obj_array = $this->getTopics()->toArray();
$topics = array_map(fn($e) => $e->getName(), $topic_obj_array);
return implode(', ', $topics);
}
public function isShownInMainSystem(): bool
{
return $this->showInMainSystem;
}
public function setShowInMainSystem(bool $showInMainSystem): void
{
$this->showInMainSystem = $showInMainSystem;
}
public function getDisplayName(): string
{
if ($this->displayName)
{
return $this->displayName;
}
return $this->name;
}
public function setDisplayName(?string $displayName): void
{
$this->displayName = $displayName;
}
public function getDeletedAt(): ?DateTime
{
return $this->deletedAt;
}
public function setDeletedAt(?DateTime $deletedAt): void
{
$this->deletedAt = $deletedAt;
}
public function JsonSerialize(): mixed
{
return [
"id" => $this->getId(),
"name" => $this->getName(),
"displayName" => $this->getDisplayName(),
"type" => $this->getType(),
];
}
public function __toString(): string
{
return $this->getName();
}
}