vendor/nellapp/sdk-bundle/src/Entity/AbstractCustomView.php line 13

Open in your IDE?
  1. <?php
  2. namespace Nellapp\Bundle\SDKBundle\Entity;
  3. use Doctrine\ORM\Mapping as ORM;
  4. use Doctrine\ORM\Mapping\MappedSuperclass;
  5. use Nellapp\Bundle\SDKBundle\CustomizableView\AbstractCustomizableViewManager;
  6. use Nellapp\Bundle\SDKBundle\CustomizableView\Model\AbstractColumnDefinition;
  7. use Nellapp\Bundle\SDKBundle\CustomizableView\Model\Column;
  8. use Symfony\Component\Validator\Constraints as Assert;
  9. #[MappedSuperclass]
  10. abstract class AbstractCustomView
  11. {
  12. const DEFAULT_VIEW_NAME = '__DEFAULT__';
  13. #[ORM\Column(type: 'string', nullable: false)]
  14. #[Assert\NotBlank()]
  15. private ?string $name = null;
  16. /** @var Column[] $columns */
  17. #[ORM\Column(type: "json", nullable: true)]
  18. private array $columns = [];
  19. /** @var ?array $filters */
  20. #[ORM\Column(type: 'json', nullable: true)]
  21. private ?array $filters = null;
  22. #[ORM\Column(type: 'boolean', nullable: false)]
  23. private ?bool $active = false;
  24. #[ORM\Column(type: 'string', nullable: true)]
  25. private ?string $sortKey = null;
  26. #[ORM\Column(type: 'string', nullable: true)]
  27. #[Assert\Choice(choices: ['asc', 'desc'])]
  28. private ?string $sortDirection = null;
  29. /**
  30. * @param bool $active
  31. * @param AbstractColumnDefinition[] $columnsDef
  32. */
  33. public function __construct(
  34. array $columnsDef,
  35. bool $active = true,
  36. )
  37. {
  38. $count = 0;
  39. foreach ($columnsDef as $def) {
  40. $this->columns[$def->getKey()] = new Column($def, $count, $active);
  41. $count++;
  42. }
  43. $this->orderColumns();
  44. }
  45. abstract function getId();
  46. /**
  47. * @return Column[]
  48. */
  49. public function getActiveColumns(): array
  50. {
  51. if ($this->hasColumnDeserialized()) {
  52. $activeColumns = [];
  53. foreach ($this->getColumns() as $column) {
  54. if ($column->isActive()) {
  55. $activeColumns[$column->getColumnDefinition()->getKey()] = $column;
  56. }
  57. }
  58. return $activeColumns;
  59. }
  60. throw new \LogicException(
  61. 'You must deserialize Columns before accessing it. Use ' .
  62. AbstractCustomizableViewManager::class .
  63. ' for deserialization.'
  64. );
  65. }
  66. /**
  67. * @return AbstractColumnDefinition[]
  68. */
  69. public function getActiveColumnsDefs(): array
  70. {
  71. if ($this->hasColumnDeserialized()) {
  72. $activeColumnsDefs = [];
  73. foreach ($this->getColumns() as $column) {
  74. if ($column->isActive()) {
  75. $activeColumnsDefs[] = $column->getColumnDefinition();
  76. }
  77. }
  78. return $activeColumnsDefs;
  79. }
  80. throw new \LogicException(
  81. 'You must deserialize Columns before accessing it. Use ' .
  82. AbstractCustomizableViewManager::class .
  83. ' for deserialization.'
  84. );
  85. }
  86. public function getColumnDefByKey(string $key): ?AbstractColumnDefinition
  87. {
  88. if ($this->hasColumnDeserialized()) {
  89. foreach ($this->columns as $column) {
  90. if ($column->getColumnDefinition()->getKey() === $key) {
  91. return $column->getColumnDefinition();
  92. }
  93. }
  94. return null;
  95. }
  96. throw new \LogicException(
  97. 'You must deserialize Columns before accessing it. Use ' .
  98. AbstractCustomizableViewManager::class .
  99. ' for deserialization.'
  100. );
  101. }
  102. private function orderColumns(): void
  103. {
  104. if ($this->hasColumnDeserialized()) {
  105. uasort($this->columns, function (Column $columnA, Column $columnB): int {
  106. if ($columnA->getOrder() > $columnB->getOrder()) {
  107. return 1;
  108. } else if ($columnA->getOrder() < $columnB->getOrder()) {
  109. return -1;
  110. }
  111. return 0;
  112. });
  113. return;
  114. }
  115. throw new \LogicException(
  116. 'You must deserialize Columns before accessing it. Use ' .
  117. AbstractCustomizableViewManager::class .
  118. ' for deserialization.'
  119. );
  120. }
  121. public function isNewView(): bool
  122. {
  123. if ($this->name === self::DEFAULT_VIEW_NAME) {
  124. return true;
  125. }
  126. return false;
  127. }
  128. public function setNewView(): self
  129. {
  130. $this->name = self::DEFAULT_VIEW_NAME;
  131. return $this;
  132. }
  133. /**
  134. * @return array
  135. */
  136. public function getColumns(): array
  137. {
  138. return $this->columns;
  139. }
  140. public function hasColumnDeserialized(): bool
  141. {
  142. if (!empty($this->columns) && !$this->columns[array_key_first($this->columns)] instanceof Column) {
  143. return false;
  144. }
  145. return true;
  146. }
  147. /**
  148. * @param Column[] $columns
  149. * @return $this
  150. */
  151. public function setColumns(array $columns): self
  152. {
  153. $this->columns = $columns;
  154. return $this;
  155. }
  156. public function getName(): ?string
  157. {
  158. return $this->name;
  159. }
  160. public function setName(?string $name): AbstractCustomView
  161. {
  162. $this->name = $name;
  163. return $this;
  164. }
  165. public function getActive(): ?bool
  166. {
  167. return $this->active;
  168. }
  169. public function setActive(?bool $active): AbstractCustomView
  170. {
  171. $this->active = $active;
  172. return $this;
  173. }
  174. public function getFilters(): ?array
  175. {
  176. return $this->filters;
  177. }
  178. public function setFilters(?array $filters): AbstractCustomView
  179. {
  180. $this->filters = $filters;
  181. return $this;
  182. }
  183. public function getSortKey(): ?string
  184. {
  185. return $this->sortKey;
  186. }
  187. public function setSortKey(?string $sortKey): AbstractCustomView
  188. {
  189. $this->sortKey = $sortKey;
  190. return $this;
  191. }
  192. public function getSortDirection(): ?string
  193. {
  194. return $this->sortDirection;
  195. }
  196. public function setSortDirection(?string $sortDirection): AbstractCustomView
  197. {
  198. $this->sortDirection = $sortDirection;
  199. return $this;
  200. }
  201. }