DamienHarper /
adf-tools
| 1 | <?php |
||
| 2 | |||
| 3 | declare(strict_types=1); |
||
| 4 | |||
| 5 | namespace DH\Adf\Node\Inline; |
||
| 6 | |||
| 7 | use DH\Adf\Node\InlineNode; |
||
| 8 | use DH\Adf\Node\MarkNode; |
||
| 9 | use DH\Adf\Node\Node; |
||
| 10 | |||
| 11 | /** |
||
| 12 | * @see https://developer.atlassian.com/cloud/jira/platform/apis/document/nodes/text |
||
| 13 | */ |
||
| 14 | class Text extends InlineNode |
||
| 15 | { |
||
| 16 | protected string $type = 'text'; |
||
| 17 | private string $text; |
||
| 18 | private array $marks = []; |
||
| 19 | |||
| 20 | public function __construct(string $text, MarkNode ...$marks) |
||
| 21 | { |
||
| 22 | $this->text = $text; |
||
| 23 | |||
| 24 | foreach ($marks as $mark) { |
||
| 25 | $this->addMark($mark); |
||
| 26 | } |
||
| 27 | } |
||
| 28 | |||
| 29 | public function jsonSerialize(): array |
||
| 30 | { |
||
| 31 | $result = parent::jsonSerialize(); |
||
| 32 | if ($this->marks) { |
||
|
0 ignored issues
–
show
|
|||
| 33 | $result['marks'] = $this->marks; |
||
| 34 | } |
||
| 35 | $result['text'] = $this->text; |
||
| 36 | |||
| 37 | return $result; |
||
| 38 | } |
||
| 39 | |||
| 40 | public function getMarks(): array |
||
| 41 | { |
||
| 42 | return $this->marks; |
||
| 43 | } |
||
| 44 | |||
| 45 | public static function load(array $data): self |
||
| 46 | { |
||
| 47 | self::checkNodeData(static::class, $data, ['text']); |
||
| 48 | |||
| 49 | $args = []; |
||
| 50 | if (\array_key_exists('marks', $data)) { |
||
| 51 | foreach ($data['marks'] as $nodeData) { |
||
| 52 | $class = Node::NODE_MAPPING[$nodeData['type']]; |
||
| 53 | $node = $class::load($nodeData); |
||
| 54 | \assert($node instanceof MarkNode); |
||
| 55 | $args[] = $node; |
||
| 56 | } |
||
| 57 | } |
||
| 58 | |||
| 59 | return new self($data['text'], ...$args); |
||
| 60 | } |
||
| 61 | |||
| 62 | public function getText(): string |
||
| 63 | { |
||
| 64 | return $this->text; |
||
| 65 | } |
||
| 66 | |||
| 67 | protected function addMark(MarkNode $mark): void |
||
| 68 | { |
||
| 69 | $this->marks[] = $mark; |
||
| 70 | } |
||
| 71 | } |
||
| 72 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.