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 $prefix; |
26
|
|
|
|
27
|
|
|
/** @var string */ |
28
|
|
|
private $identifier; |
29
|
|
|
|
30
|
63 |
|
public function __construct(string $prefix, string $identifier, ?string $label = null) |
31
|
|
|
{ |
32
|
63 |
|
$this->prefix = $prefix; |
33
|
63 |
|
$this->identifier = $identifier; |
34
|
|
|
|
35
|
63 |
|
parent::__construct('', $label ?? \sprintf('%s%s', $prefix, $identifier)); |
36
|
63 |
|
} |
37
|
|
|
|
38
|
18 |
|
public function getLabel(): ?string |
39
|
|
|
{ |
40
|
18 |
|
if (($labelNode = $this->findLabelNode()) === null) { |
41
|
3 |
|
return null; |
42
|
|
|
} |
43
|
|
|
|
44
|
18 |
|
return $labelNode->getLiteral(); |
45
|
|
|
} |
46
|
|
|
|
47
|
33 |
|
public function getIdentifier(): string |
48
|
|
|
{ |
49
|
33 |
|
return $this->identifier; |
50
|
|
|
} |
51
|
|
|
|
52
|
12 |
|
public function getPrefix(): string |
53
|
|
|
{ |
54
|
12 |
|
return $this->prefix; |
55
|
|
|
} |
56
|
|
|
|
57
|
15 |
|
public function hasUrl(): bool |
58
|
|
|
{ |
59
|
15 |
|
return $this->url !== ''; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @return $this |
64
|
|
|
*/ |
65
|
12 |
|
public function setLabel(string $label): self |
66
|
|
|
{ |
67
|
12 |
|
if (($labelNode = $this->findLabelNode()) === null) { |
68
|
3 |
|
$labelNode = new Text(); |
69
|
3 |
|
$this->prependChild($labelNode); |
70
|
|
|
} |
71
|
|
|
|
72
|
12 |
|
$labelNode->setLiteral($label); |
73
|
|
|
|
74
|
12 |
|
return $this; |
75
|
|
|
} |
76
|
|
|
|
77
|
18 |
|
private function findLabelNode(): ?Text |
78
|
|
|
{ |
79
|
18 |
|
foreach ($this->children() as $child) { |
80
|
18 |
|
if ($child instanceof Text) { |
81
|
18 |
|
return $child; |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
85
|
3 |
|
return null; |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|