Magneds /
php-lokalise
| 1 | <?php // Copyright ⓒ 2018 Magneds IP B.V. - All Rights Reserved |
||
| 2 | namespace Magneds\Lokalise\TranslateString\Entity; |
||
| 3 | |||
| 4 | use Magneds\Lokalise\Language\Entity\Language; |
||
| 5 | |||
| 6 | class TranslateStringPartial |
||
| 7 | { |
||
| 8 | /** |
||
| 9 | * @var string |
||
| 10 | */ |
||
| 11 | protected $key; |
||
| 12 | |||
| 13 | /** |
||
| 14 | * @var int|null |
||
| 15 | */ |
||
| 16 | protected $platformMask; |
||
| 17 | |||
| 18 | /** |
||
| 19 | * @var string|null |
||
| 20 | */ |
||
| 21 | protected $description; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * @var string[] |
||
| 25 | */ |
||
| 26 | protected $tags = []; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * @var string[] |
||
| 30 | */ |
||
| 31 | protected $translations = []; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * TranslateStringPartial constructor. |
||
| 35 | * @param string $key |
||
| 36 | */ |
||
| 37 | public function __construct(string $key) |
||
| 38 | { |
||
| 39 | $this->key = $key; |
||
| 40 | } |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @return string |
||
| 44 | */ |
||
| 45 | public function getKey(): string |
||
| 46 | { |
||
| 47 | return $this->key; |
||
| 48 | } |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @return int|null |
||
| 52 | */ |
||
| 53 | public function getPlatformMask() |
||
| 54 | { |
||
| 55 | return $this->platformMask; |
||
| 56 | } |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @param int $platformMask |
||
| 60 | * @return $this |
||
| 61 | */ |
||
| 62 | public function setPlatformMask($platformMask) |
||
| 63 | { |
||
| 64 | $this->platformMask = (int)$platformMask; |
||
| 65 | return $this; |
||
| 66 | } |
||
| 67 | |||
| 68 | /** |
||
| 69 | * @return string |
||
| 70 | */ |
||
| 71 | public function getDescription(): string |
||
| 72 | { |
||
| 73 | return $this->description; |
||
|
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||
| 74 | } |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @param string $description |
||
| 78 | * @return $this |
||
| 79 | */ |
||
| 80 | public function setDescription($description) |
||
| 81 | { |
||
| 82 | $this->description = (string)$description; |
||
| 83 | return $this; |
||
| 84 | } |
||
| 85 | |||
| 86 | /** |
||
| 87 | * @return string[] |
||
| 88 | */ |
||
| 89 | public function getTags(): array |
||
| 90 | { |
||
| 91 | return $this->tags; |
||
| 92 | } |
||
| 93 | |||
| 94 | /** |
||
| 95 | * @param string[] $tags |
||
| 96 | * @return $this |
||
| 97 | */ |
||
| 98 | public function setTags(array $tags) |
||
| 99 | { |
||
| 100 | $this->tags = (array)$tags; |
||
| 101 | return $this; |
||
| 102 | } |
||
| 103 | |||
| 104 | /** |
||
| 105 | * @param $tag |
||
| 106 | * @return $this |
||
| 107 | */ |
||
| 108 | public function addTag(string $tag) |
||
| 109 | { |
||
| 110 | if (in_array($tag, $this->tags)) { |
||
| 111 | return $this; |
||
| 112 | } |
||
| 113 | $this->tags[] = $tag; |
||
| 114 | |||
| 115 | return $this; |
||
| 116 | } |
||
| 117 | |||
| 118 | /** |
||
| 119 | * @return string[] |
||
| 120 | */ |
||
| 121 | public function getTranslations(): array |
||
| 122 | { |
||
| 123 | return $this->translations; |
||
| 124 | } |
||
| 125 | |||
| 126 | public function addTranslation(Language $language, $translation) |
||
| 127 | { |
||
| 128 | $isoLang = $language->getIso(); |
||
| 129 | $this->translations[$isoLang] = $translation; |
||
| 130 | } |
||
| 131 | |||
| 132 | public function toArray() |
||
| 133 | { |
||
| 134 | $data = [ |
||
| 135 | 'key' => $this->key, |
||
| 136 | 'platform_mask' => $this->platformMask, |
||
| 137 | 'description' => $this->description, |
||
| 138 | 'tags' => $this->tags, |
||
| 139 | 'translations' => $this->translations |
||
| 140 | ]; |
||
| 141 | |||
| 142 | return array_filter($data); |
||
| 143 | } |
||
| 144 | |||
| 145 | public static function buildFromTranslateString(TranslateString $translateString) |
||
| 146 | { |
||
| 147 | $partial = new self($translateString->getKey()); |
||
| 148 | $partial->setPlatformMask($translateString->getPlatformMask()); |
||
| 149 | $partial->setTags($translateString->getTags()); |
||
| 150 | |||
| 151 | return $partial; |
||
| 152 | } |
||
| 153 | } |
||
| 154 |