1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is 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\CommonMark\Extension\Mention; |
18
|
|
|
|
19
|
|
|
use League\CommonMark\Extension\CommonMark\Node\Inline\Link; |
20
|
|
|
use League\CommonMark\Node\Inline\Text; |
21
|
|
|
|
22
|
|
|
class Mention extends Link |
23
|
|
|
{ |
24
|
|
|
/** @var string */ |
25
|
|
|
private $name; |
26
|
|
|
|
27
|
|
|
/** @var string */ |
28
|
|
|
private $prefix; |
29
|
|
|
|
30
|
63 |
|
/** @var string */ |
31
|
|
|
private $identifier; |
32
|
63 |
|
|
33
|
63 |
|
public function __construct(string $name, string $prefix, string $identifier, ?string $label = null) |
34
|
|
|
{ |
35
|
63 |
|
$this->name = $name; |
36
|
63 |
|
$this->prefix = $prefix; |
37
|
|
|
$this->identifier = $identifier; |
38
|
18 |
|
|
39
|
|
|
parent::__construct('', $label ?? \sprintf('%s%s', $prefix, $identifier)); |
40
|
18 |
|
} |
41
|
3 |
|
|
42
|
|
|
public function getLabel(): ?string |
43
|
|
|
{ |
44
|
18 |
|
if (($labelNode = $this->findLabelNode()) === null) { |
45
|
|
|
return null; |
46
|
|
|
} |
47
|
33 |
|
|
48
|
|
|
return $labelNode->getLiteral(); |
49
|
33 |
|
} |
50
|
|
|
|
51
|
|
|
public function getIdentifier(): string |
52
|
12 |
|
{ |
53
|
|
|
return $this->identifier; |
54
|
12 |
|
} |
55
|
|
|
|
56
|
|
|
public function getName(): ?string |
57
|
15 |
|
{ |
58
|
|
|
return $this->name; |
59
|
15 |
|
} |
60
|
|
|
|
61
|
|
|
public function getPrefix(): string |
62
|
|
|
{ |
63
|
|
|
return $this->prefix; |
64
|
|
|
} |
65
|
12 |
|
|
66
|
|
|
public function hasUrl(): bool |
67
|
12 |
|
{ |
68
|
3 |
|
return $this->url !== ''; |
69
|
3 |
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
12 |
|
* @return $this |
73
|
|
|
*/ |
74
|
12 |
|
public function setLabel(string $label): self |
75
|
|
|
{ |
76
|
|
|
if (($labelNode = $this->findLabelNode()) === null) { |
77
|
18 |
|
$labelNode = new Text(); |
78
|
|
|
$this->prependChild($labelNode); |
79
|
18 |
|
} |
80
|
18 |
|
|
81
|
18 |
|
$labelNode->setLiteral($label); |
82
|
|
|
|
83
|
|
|
return $this; |
84
|
|
|
} |
85
|
3 |
|
|
86
|
|
|
private function findLabelNode(): ?Text |
87
|
|
|
{ |
88
|
|
|
foreach ($this->children() as $child) { |
89
|
|
|
if ($child instanceof Text) { |
90
|
|
|
return $child; |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
return null; |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|