|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* This file was originally part of the league/commonmark package. |
|
7
|
|
|
* |
|
8
|
|
|
* (c) Colin O'Dell <[email protected]> |
|
9
|
|
|
* |
|
10
|
|
|
* Original code based on the CommonMark JS reference parser (https://bitly.com/commonmark-js) |
|
11
|
|
|
* - (c) John MacFarlane |
|
12
|
|
|
* |
|
13
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
14
|
|
|
* file that was distributed with this source code. |
|
15
|
|
|
*/ |
|
16
|
|
|
|
|
17
|
|
|
namespace League\Emoji\Node; |
|
18
|
|
|
|
|
19
|
|
|
use Dflydev\DotAccessData\Data; |
|
20
|
|
|
|
|
21
|
|
|
abstract class Node extends Data implements \Stringable |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* @var Document|null |
|
25
|
|
|
* |
|
26
|
|
|
* @psalm-readonly-allow-private-mutation |
|
27
|
|
|
*/ |
|
28
|
|
|
protected $document; |
|
29
|
|
|
|
|
30
|
|
|
/** @var string */ |
|
31
|
|
|
protected $content = ''; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @param array<string, mixed> $data |
|
35
|
|
|
*/ |
|
36
|
273 |
|
public function __construct(string $content = '', array $data = []) |
|
37
|
|
|
{ |
|
38
|
273 |
|
parent::__construct(['attributes' => new Data()]); |
|
39
|
273 |
|
$this->content = $content; |
|
40
|
|
|
|
|
41
|
273 |
|
$this->import($data); |
|
42
|
273 |
|
} |
|
43
|
|
|
|
|
44
|
3 |
|
public function __clone() |
|
45
|
|
|
{ |
|
46
|
3 |
|
$this->document = null; |
|
47
|
3 |
|
} |
|
48
|
|
|
|
|
49
|
231 |
|
public function __toString(): string |
|
50
|
|
|
{ |
|
51
|
231 |
|
return $this->getContent(); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
21 |
|
public function addClass(string ...$classes): void |
|
55
|
|
|
{ |
|
56
|
21 |
|
$class = (string) $this->getAttribute('class', ''); |
|
57
|
|
|
|
|
58
|
21 |
|
foreach ($classes as $value) { |
|
59
|
21 |
|
if ($class !== '') { |
|
60
|
21 |
|
$class .= ' '; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
21 |
|
$class .= $value; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
21 |
|
if ($class) { |
|
67
|
21 |
|
$this->setAttribute('class', $class); |
|
68
|
|
|
} |
|
69
|
21 |
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @param mixed $default |
|
73
|
|
|
* |
|
74
|
|
|
* @return mixed |
|
75
|
|
|
*/ |
|
76
|
30 |
|
public function getAttribute(string $name, $default = null) |
|
77
|
|
|
{ |
|
78
|
30 |
|
return $this->getAttributes()->get($name, $default); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
33 |
|
public function getAttributes(): Data |
|
82
|
|
|
{ |
|
83
|
|
|
/** @var Data $attributes */ |
|
84
|
33 |
|
$attributes = $this->get('attributes'); |
|
85
|
|
|
|
|
86
|
33 |
|
return $attributes; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
231 |
|
public function getContent(): string |
|
90
|
|
|
{ |
|
91
|
231 |
|
return $this->content; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
6 |
|
public function getDocument(): ?Document |
|
95
|
|
|
{ |
|
96
|
6 |
|
return $this->document; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
3 |
|
public function hasAttribute(string $name): bool |
|
100
|
|
|
{ |
|
101
|
3 |
|
return $this->getAttributes()->has($name); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* @param mixed $value |
|
106
|
|
|
*/ |
|
107
|
33 |
|
public function setAttribute(string $name, $value): void |
|
108
|
|
|
{ |
|
109
|
33 |
|
$this->getAttributes()->set($name, $value); |
|
110
|
33 |
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* @param array<string, mixed> $attributes |
|
114
|
|
|
*/ |
|
115
|
3 |
|
public function setAttributes(array $attributes = []): void |
|
116
|
|
|
{ |
|
117
|
3 |
|
$this->set('attributes', new Data($attributes)); |
|
118
|
3 |
|
} |
|
119
|
|
|
|
|
120
|
99 |
|
public function setContent(string $contents): void |
|
121
|
|
|
{ |
|
122
|
99 |
|
$this->content = $contents; |
|
123
|
99 |
|
} |
|
124
|
|
|
|
|
125
|
246 |
|
public function setDocument(?Document $document = null): void |
|
126
|
|
|
{ |
|
127
|
246 |
|
$this->document = $document; |
|
128
|
246 |
|
} |
|
129
|
|
|
|
|
130
|
21 |
|
public function replaceWith(Node $replacement): void |
|
131
|
|
|
{ |
|
132
|
21 |
|
if ($this->document) { |
|
133
|
21 |
|
$this->document->replaceNode($this, $replacement); |
|
134
|
|
|
} |
|
135
|
21 |
|
} |
|
136
|
|
|
} |
|
137
|
|
|
|