ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
OptionImplementation.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
25
27{
28 protected ?int $option_id;
29
33 protected array $translations;
34
35 public function __construct(
36 protected int $position,
37 ?int $option_id = null,
39 ) {
40 $this->option_id = $option_id;
41 $this->translations = $translations;
42 }
43
44 public function optionID(): ?int
45 {
46 return $this->option_id;
47 }
48
49 public function isPersisted(): bool
50 {
51 return !is_null($this->option_id);
52 }
53
54 protected function getSubData(): \Generator
55 {
56 yield from $this->getTranslations();
57 }
58
59 public function getPosition(): int
60 {
61 return $this->position;
62 }
63
64 public function setPosition(int $position): void
65 {
66 if ($this->position === $position) {
67 return;
68 }
69 $this->position = $position;
70 $this->markAsChanged();
71 }
72
73 public function getTranslations(): \Generator
74 {
76 }
77
78 public function hasTranslationInLanguage(string $language): bool
79 {
80 foreach ($this->translations as $translation) {
81 if ($translation->language() === $language) {
82 return true;
83 }
84 }
85 return false;
86 }
87
88 public function getTranslationInLanguage(string $language): ?OptionTranslation
89 {
90 foreach ($this->translations as $translation) {
91 if ($translation->language() === $language) {
92 return $translation;
93 }
94 }
95 return null;
96 }
97
101 public function addTranslation(string $language): OptionTranslation
102 {
103 if ($this->hasTranslationInLanguage($language)) {
104 throw new Exception('Translation in language ' . $language . ' already exists.');
105 }
106
107 $translation = new OptionTranslationImplementation($language, '');
108 $this->translations[] = $translation;
109 return $translation;
110 }
111
112 public function removeTranslation(string $language): void
113 {
114 foreach ($this->translations as $key => $translation) {
115 if ($translation->language() !== $language) {
116 continue;
117 }
118 unset($this->translations[$key]);
119 $this->markAsChanged();
120 }
121 }
122}
__construct(protected int $position, ?int $option_id=null, OptionTranslation ... $translations)