LauLamanApps /
apple-passbook
| 1 | <?php |
||
| 2 | |||
| 3 | declare(strict_types=1); |
||
| 4 | |||
| 5 | namespace LauLamanApps\ApplePassbook\MetaData\Field; |
||
| 6 | |||
| 7 | use LauLamanApps\ApplePassbook\Exception\InvalidArgumentException; |
||
| 8 | use LauLamanApps\ApplePassbook\MetaData\SemanticTag; |
||
| 9 | use LauLamanApps\ApplePassbook\Style\DataDetector; |
||
| 10 | use LauLamanApps\ApplePassbook\Style\TextAlignment; |
||
| 11 | |||
| 12 | class Field |
||
| 13 | { |
||
| 14 | private string $key; |
||
| 15 | /** @var string|int|bool */ |
||
| 16 | private $value; |
||
| 17 | private string $label; |
||
| 18 | /** @var DataDetector[]|null */ |
||
| 19 | private ?array $dataDetectorTypes; |
||
| 20 | private string $changeMessage; |
||
| 21 | private TextAlignment $textAlignment; |
||
| 22 | private string $attributedValue; |
||
| 23 | |||
| 24 | /** @var SemanticTag[] */ |
||
| 25 | private array $semantics; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * @param string|int|bool $value |
||
| 29 | */ |
||
| 30 | public function __construct(?string $key = null, $value = null, ?string $label = null) |
||
| 31 | { |
||
| 32 | if ($key !== null) { |
||
| 33 | $this->setKey($key); |
||
| 34 | } |
||
| 35 | |||
| 36 | if ($value !== null) { |
||
| 37 | $this->setValue($value); |
||
| 38 | } |
||
| 39 | |||
| 40 | if ($label !== null) { |
||
| 41 | $this->setLabel($label); |
||
| 42 | } |
||
| 43 | } |
||
| 44 | |||
| 45 | public function setKey(string $key): void |
||
| 46 | { |
||
| 47 | $this->key = $key; |
||
| 48 | 43 | } |
|
| 49 | |||
| 50 | 43 | /** |
|
| 51 | 39 | * @param string|int|bool $value |
|
| 52 | */ |
||
| 53 | public function setValue($value): void |
||
| 54 | 43 | { |
|
| 55 | 11 | if (!is_scalar($value)) { |
|
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
| 56 | throw new InvalidArgumentException('Value should be a scalar type.'); |
||
| 57 | } |
||
| 58 | 43 | ||
| 59 | 1 | $this->value = $value; |
|
| 60 | } |
||
| 61 | 43 | ||
| 62 | public function setLabel(string $label): void |
||
| 63 | 40 | { |
|
| 64 | $this->label = $label; |
||
| 65 | 40 | } |
|
| 66 | 40 | ||
| 67 | public function addDataDetectorType(DataDetector $dataDetector): void |
||
| 68 | 37 | { |
|
| 69 | $this->dataDetectorTypes[$dataDetector->getValue()] = $dataDetector; |
||
| 70 | 37 | } |
|
| 71 | 3 | ||
| 72 | public function setChangeMessage(string $changeMessage): void |
||
| 73 | { |
||
| 74 | 34 | $this->changeMessage = $changeMessage; |
|
| 75 | 34 | } |
|
| 76 | |||
| 77 | 5 | public function setTextAlignment(TextAlignment $textAlignment): void |
|
| 78 | { |
||
| 79 | 5 | $this->textAlignment = $textAlignment; |
|
| 80 | 5 | } |
|
| 81 | |||
| 82 | 3 | public function setAttributedValue(string $attributedValue): void |
|
| 83 | { |
||
| 84 | 3 | $this->attributedValue = $attributedValue; |
|
| 85 | 3 | } |
|
| 86 | |||
| 87 | 4 | public function addSemanticTag(SemanticTag $semanticTag): void |
|
| 88 | { |
||
| 89 | 4 | $this->semantics[] = $semanticTag; |
|
| 90 | 4 | } |
|
| 91 | |||
| 92 | 3 | /** |
|
| 93 | * @return array<string, array<int|string, mixed>|bool|int|string> |
||
| 94 | 3 | */ |
|
| 95 | 3 | public function getMetadata(): array |
|
| 96 | { |
||
| 97 | 3 | $data = [ |
|
| 98 | 'key' => $this->key, |
||
| 99 | 3 | 'value'=> $this->value, |
|
| 100 | 3 | ]; |
|
| 101 | |||
| 102 | 32 | if (isset($this->label)) { |
|
| 103 | $data['label'] = $this->label; |
||
| 104 | } |
||
| 105 | 32 | ||
| 106 | 32 | if (isset($this->dataDetectorTypes)) { |
|
| 107 | foreach ($this->dataDetectorTypes as $dataDetector) { |
||
| 108 | $data['dataDetectorTypes'][] = (string) $dataDetector->getValue(); |
||
| 109 | 32 | } |
|
| 110 | 5 | } |
|
| 111 | |||
| 112 | if (isset($this->changeMessage)) { |
||
| 113 | 32 | $data['changeMessage'] = $this->changeMessage; |
|
| 114 | 3 | } |
|
| 115 | 3 | ||
| 116 | if (isset($this->textAlignment)) { |
||
| 117 | $data['textAlignment'] = (string) $this->textAlignment->getValue(); |
||
| 118 | } |
||
| 119 | 32 | ||
| 120 | 4 | if (isset($this->attributedValue)) { |
|
| 121 | $data['attributedValue'] = $this->attributedValue; |
||
| 122 | } |
||
| 123 | 32 | ||
| 124 | 3 | if (isset($this->semantics)) { |
|
| 125 | foreach ($this->semantics as $tag) { |
||
| 126 | $data['semantics'][$tag->getKey()] = $tag->getValue(); |
||
| 127 | 32 | } |
|
| 128 | 3 | } |
|
| 129 | |||
| 130 | return $data; |
||
| 131 | 32 | } |
|
| 132 | } |
||
| 133 |