|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\Model\Entity; |
| 4 | + |
| 5 | +use Doctrine\ORM\Mapping as ORM; |
| 6 | +use App\Exceptions\InvalidArgumentException; |
| 7 | +use Nette\Utils\Validators; |
| 8 | +use JsonSerializable; |
| 9 | + |
| 10 | +/** |
| 11 | + * @ORM\Entity |
| 12 | + * @ORM\Table(uniqueConstraints={@ORM\UniqueConstraint(columns={"group_id", "service", "key", "value"})}, |
| 13 | + * indexes={@ORM\Index(name="keys_idx", columns={"service", "key"})}) |
| 14 | + * |
| 15 | + * Key-value attributes assigned to groups to connect them to 3rd party systems to simplify |
| 16 | + * external group management (creation, archiving) and student membership management. |
| 17 | + */ |
| 18 | +class GroupExternalAttribute implements JsonSerializable |
| 19 | +{ |
| 20 | + use CreateableEntity; |
| 21 | + |
| 22 | + /** |
| 23 | + * @ORM\Id |
| 24 | + * @ORM\Column(type="uuid", unique=true) |
| 25 | + * @ORM\GeneratedValue(strategy="CUSTOM") |
| 26 | + * @ORM\CustomIdGenerator(class=\Ramsey\Uuid\Doctrine\UuidGenerator::class) |
| 27 | + * @var \Ramsey\Uuid\UuidInterface |
| 28 | + */ |
| 29 | + protected $id; |
| 30 | + |
| 31 | + /** |
| 32 | + * @ORM\ManyToOne(targetEntity="Group", inversedBy="externalAttributes") |
| 33 | + */ |
| 34 | + protected $group; |
| 35 | + |
| 36 | + /** |
| 37 | + * @ORM\Column(type="string", length=32) |
| 38 | + * Identifies 3rd party service which assigned the attribute. This is basically a namespace for keys. |
| 39 | + */ |
| 40 | + protected $service; |
| 41 | + |
| 42 | + /** |
| 43 | + * @ORM\Column(type="string", length=32) |
| 44 | + * Key of the attribute under which it can be searched. |
| 45 | + */ |
| 46 | + protected $key; |
| 47 | + |
| 48 | + /** |
| 49 | + * @ORM\Column(type="string") |
| 50 | + */ |
| 51 | + protected $value; |
| 52 | + |
| 53 | + /** |
| 54 | + * Constructor initializes all fields. |
| 55 | + * @param Group $group |
| 56 | + * @param string $service |
| 57 | + * @param string $key |
| 58 | + * @param string $value |
| 59 | + */ |
| 60 | + public function __construct(Group $group, string $service, string $key, string $value) |
| 61 | + { |
| 62 | + $this->group = $group; |
| 63 | + $this->service = $service; |
| 64 | + $this->key = $key; |
| 65 | + $this->value = $value; |
| 66 | + $this->createdNow(); |
| 67 | + } |
| 68 | + |
| 69 | + public function jsonSerialize(): mixed |
| 70 | + { |
| 71 | + return [ |
| 72 | + "id" => $this->getId(), |
| 73 | + "createdAt" => $this->createdAt->getTimestamp(), |
| 74 | + "group" => $this->getGroup()->getId(), |
| 75 | + "service" => $this->getService(), |
| 76 | + "key" => $this->getKey(), |
| 77 | + "value" => $this->getValue(), |
| 78 | + ]; |
| 79 | + } |
| 80 | + |
| 81 | + /* |
| 82 | + * Accessors |
| 83 | + */ |
| 84 | + |
| 85 | + public function getId(): ?string |
| 86 | + { |
| 87 | + return $this->id === null ? null : (string)$this->id; |
| 88 | + } |
| 89 | + |
| 90 | + public function getGroup(): Group |
| 91 | + { |
| 92 | + return $this->group; |
| 93 | + } |
| 94 | + |
| 95 | + public function getService(): string |
| 96 | + { |
| 97 | + return $this->service; |
| 98 | + } |
| 99 | + |
| 100 | + public function getKey(): string |
| 101 | + { |
| 102 | + return $this->key; |
| 103 | + } |
| 104 | + |
| 105 | + public function getValue(): string |
| 106 | + { |
| 107 | + return $this->value; |
| 108 | + } |
| 109 | +} |
0 commit comments