1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* (c) Steve Nebes <[email protected]> |
4
|
|
|
* |
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
6
|
|
|
* file that was distributed with this source code. |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
declare(strict_types=1); |
10
|
|
|
|
11
|
|
|
namespace SN\RangeDifferencer\Tag; |
12
|
|
|
|
13
|
|
|
use InvalidArgumentException; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* An atom that represents a closing or opening tag. |
17
|
|
|
*/ |
18
|
|
|
class TagAtom implements AtomInterface |
19
|
|
|
{ |
20
|
|
|
/** @var string */ |
21
|
|
|
private $identifier = ''; |
22
|
|
|
|
23
|
|
|
/** @var string */ |
24
|
|
|
private $internalIdentifiers = ''; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @param string $str |
28
|
|
|
* |
29
|
|
|
* @throws InvalidArgumentException |
30
|
|
|
*/ |
31
|
|
|
public function __construct(string $str) |
32
|
|
|
{ |
33
|
|
|
if (!$this->isValidAtom($str)) { |
34
|
|
|
throw new InvalidArgumentException('The given string is not a valid tag.'); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
// Remove the < and >. |
38
|
|
|
$str = \mb_substr($str, 1, -1); |
39
|
|
|
|
40
|
|
|
if (false !== $pos = \mb_strpos($str, ' ')) { |
41
|
|
|
$this->identifier = \mb_substr($str, 0, $pos); |
42
|
|
|
$this->internalIdentifiers = mb_substr($str, $pos + 1); |
43
|
|
|
} else { |
44
|
|
|
$this->identifier = $str; |
45
|
|
|
} |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** {@inheritdoc} */ |
49
|
|
|
public function getIdentifier(): string |
50
|
|
|
{ |
51
|
|
|
return $this->identifier; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** {@inheritdoc} */ |
55
|
|
|
public function getInternalIdentifiers(): string |
56
|
|
|
{ |
57
|
|
|
return $this->internalIdentifiers; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** {@inheritdoc} */ |
61
|
|
|
public function hasInternalIdentifiers(): bool |
62
|
|
|
{ |
63
|
|
|
return \mb_strlen($this->internalIdentifiers) > 0; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @param string $str |
68
|
|
|
* @return bool |
69
|
|
|
*/ |
70
|
|
|
public static function isValidTag(string $str): bool |
71
|
|
|
{ |
72
|
|
|
return |
73
|
|
|
0 === \mb_strrpos($str, '<') && |
74
|
|
|
\mb_strpos($str, '>') === \mb_strlen($str) - 1 && |
75
|
|
|
\mb_strlen($str) >= 3; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** {@inheritdoc} */ |
79
|
|
|
public function getFullText(): string |
80
|
|
|
{ |
81
|
|
|
$s = sprintf('<%s', $this->identifier); |
82
|
|
|
|
83
|
|
|
if ($this->hasInternalIdentifiers()) { |
84
|
|
|
$s .= sprintf(' %s', $this->internalIdentifiers); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
return sprintf('%s>', $s); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** {@inheritdoc} */ |
91
|
|
|
public function isValidAtom(string $str): bool |
92
|
|
|
{ |
93
|
|
|
return self::isValidTag($str); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @return string |
98
|
|
|
*/ |
99
|
|
|
public function __toString(): string |
100
|
|
|
{ |
101
|
|
|
return sprintf('TagAtom: %s', $this->getFullText()); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** {@inheritdoc} */ |
105
|
|
|
public function equalsIdentifier(AtomInterface $other): bool |
106
|
|
|
{ |
107
|
|
|
return $other->getIdentifier() === $this->identifier; |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|