Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
91.30% |
21 / 23 |
|
88.89% |
8 / 9 |
CRAP | |
0.00% |
0 / 1 |
| Dom | |
91.30% |
21 / 23 |
|
88.89% |
8 / 9 |
11.08 | |
0.00% |
0 / 1 |
| setContent | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| setSelector | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| setAction | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| setDelay | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| serialize | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| jsonSerialize | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| unserialize | |
77.78% |
7 / 9 |
|
0.00% |
0 / 1 |
3.10 | |||
| __toString | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| toArray | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * Jingga |
| 4 | * |
| 5 | * PHP Version 8.1 |
| 6 | * |
| 7 | * @package phpOMS\Model\Message |
| 8 | * @copyright Dennis Eichhorn |
| 9 | * @license OMS License 2.0 |
| 10 | * @version 1.0.0 |
| 11 | * @link https://jingga.app |
| 12 | */ |
| 13 | declare(strict_types=1); |
| 14 | |
| 15 | namespace phpOMS\Model\Message; |
| 16 | |
| 17 | use phpOMS\Contract\SerializableInterface; |
| 18 | |
| 19 | /** |
| 20 | * Dom class. |
| 21 | * |
| 22 | * @package phpOMS\Model\Message |
| 23 | * @license OMS License 2.0 |
| 24 | * @link https://jingga.app |
| 25 | * @since 1.0.0 |
| 26 | */ |
| 27 | final class Dom implements SerializableInterface |
| 28 | { |
| 29 | /** |
| 30 | * Message type. |
| 31 | * |
| 32 | * @var string |
| 33 | * @since 1.0.0 |
| 34 | */ |
| 35 | public const TYPE = 'dom'; |
| 36 | |
| 37 | /** |
| 38 | * Selector string. |
| 39 | * |
| 40 | * @var string |
| 41 | * @since 1.0.0 |
| 42 | */ |
| 43 | private string $selector = ''; |
| 44 | |
| 45 | /** |
| 46 | * Dom content. |
| 47 | * |
| 48 | * @var string |
| 49 | * @since 1.0.0 |
| 50 | */ |
| 51 | private string $content = ''; |
| 52 | |
| 53 | /** |
| 54 | * Dom action. |
| 55 | * |
| 56 | * @var int |
| 57 | * @since 1.0.0 |
| 58 | */ |
| 59 | private int $action = DomAction::MODIFY; |
| 60 | |
| 61 | /** |
| 62 | * Delay in ms. |
| 63 | * |
| 64 | * @var int |
| 65 | * @since 1.0.0 |
| 66 | */ |
| 67 | private int $delay = 0; |
| 68 | |
| 69 | /** |
| 70 | * Set DOM content |
| 71 | * |
| 72 | * @param string $content DOM Content |
| 73 | * |
| 74 | * @return void |
| 75 | * |
| 76 | * @since 1.0.0 |
| 77 | */ |
| 78 | public function setContent(string $content) : void |
| 79 | { |
| 80 | $this->content = $content; |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Set selector. |
| 85 | * |
| 86 | * @param string $selector Selector |
| 87 | * |
| 88 | * @return void |
| 89 | * |
| 90 | * @since 1.0.0 |
| 91 | */ |
| 92 | public function setSelector(string $selector) : void |
| 93 | { |
| 94 | $this->selector = $selector; |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Set action. |
| 99 | * |
| 100 | * @param int $action action |
| 101 | * |
| 102 | * @return void |
| 103 | * |
| 104 | * @since 1.0.0 |
| 105 | */ |
| 106 | public function setAction(int $action) : void |
| 107 | { |
| 108 | $this->action = $action; |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Set delay. |
| 113 | * |
| 114 | * @param int $delay Delay in ms |
| 115 | * |
| 116 | * @return void |
| 117 | * |
| 118 | * @since 1.0.0 |
| 119 | */ |
| 120 | public function setDelay(int $delay) : void |
| 121 | { |
| 122 | $this->delay = $delay; |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * Render message. |
| 127 | * |
| 128 | * @return string |
| 129 | * |
| 130 | * @since 1.0.0 |
| 131 | */ |
| 132 | public function serialize() : string |
| 133 | { |
| 134 | return $this->__toString(); |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * {@inheritdoc} |
| 139 | */ |
| 140 | public function jsonSerialize() : mixed |
| 141 | { |
| 142 | return $this->toArray(); |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * {@inheritdoc} |
| 147 | */ |
| 148 | public function unserialize(mixed $raw) : void |
| 149 | { |
| 150 | if (!\is_string($raw)) { |
| 151 | return; |
| 152 | } |
| 153 | |
| 154 | $unserialized = \json_decode($raw, true); |
| 155 | if (!\is_array($unserialized)) { |
| 156 | return; |
| 157 | } |
| 158 | |
| 159 | $this->delay = $unserialized['time'] ?? 0; |
| 160 | $this->selector = $unserialized['selector'] ?? ''; |
| 161 | $this->action = $unserialized['action'] ?? ''; |
| 162 | $this->content = $unserialized['content'] ?? DomAction::MODIFY; |
| 163 | } |
| 164 | |
| 165 | /** |
| 166 | * Stringify. |
| 167 | * |
| 168 | * @return string |
| 169 | * |
| 170 | * @since 1.0.0 |
| 171 | */ |
| 172 | public function __toString() |
| 173 | { |
| 174 | return (string) \json_encode($this->toArray()); |
| 175 | } |
| 176 | |
| 177 | /** |
| 178 | * Generate message array. |
| 179 | * |
| 180 | * @return array<string, mixed> |
| 181 | * |
| 182 | * @since 1.0.0 |
| 183 | */ |
| 184 | public function toArray() : array |
| 185 | { |
| 186 | return [ |
| 187 | 'type' => self::TYPE, |
| 188 | 'time' => $this->delay, |
| 189 | 'selector' => $this->selector, |
| 190 | 'action' => $this->action, |
| 191 | 'content' => $this->content, |
| 192 | ]; |
| 193 | } |
| 194 | } |