Skip to content

Commit 2c90a62

Browse files
committed
Added a class to hold tag information.
1 parent c534e03 commit 2c90a62

1 file changed

Lines changed: 158 additions & 0 deletions

File tree

src/VersionTag.php

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Mistralys\VersionParser;
6+
7+
use Mistralys\VersionParser\Parser\ShortTags;
8+
use Mistralys\VersionParser\Parser\TagWeights;
9+
10+
class VersionTag
11+
{
12+
private string $tagName;
13+
private int $number;
14+
private string $branch;
15+
private VersionParser $version;
16+
17+
public function __construct(VersionParser $version, string $tagName, int $number, string $branchName='')
18+
{
19+
$this->version = $version;
20+
$this->tagName = strtolower($tagName);
21+
$this->number = $number;
22+
$this->branch = $branchName;
23+
24+
if($this->number === 0 && !empty($this->tagName)) {
25+
$this->number = 1;
26+
}
27+
}
28+
29+
public function getWeight() : int
30+
{
31+
return TagWeights::getWeights()[$this->tagName] ?? 0;
32+
}
33+
34+
public function getBranchName() : string
35+
{
36+
return $this->branch;
37+
}
38+
39+
public function getNormalized() : string
40+
{
41+
$tagName = $this->getTagName();
42+
43+
if(empty($tagName)) {
44+
return $this->getBranchName();
45+
}
46+
47+
if($this->number > 1) {
48+
$name = $tagName.$this->number;
49+
} else {
50+
$name = $tagName;
51+
}
52+
53+
if(!empty($this->branch)) {
54+
$name = $this->branch.$this->version->getSeparatorChar().$name;
55+
}
56+
57+
return $name;
58+
}
59+
60+
/**
61+
* Retrieves the tag name used in the version.
62+
*
63+
* NOTE: This can be either the long or short variant
64+
* of the tag, if it has one.
65+
*
66+
* @return string Lowercase by default, or uppercase if enabled - see {@see VersionParser::setTagUppercase()}.
67+
*/
68+
public function getTagName() : string
69+
{
70+
if($this->version->isTagUppercase()) {
71+
return strtoupper($this->tagName);
72+
}
73+
74+
return $this->tagName;
75+
}
76+
77+
/**
78+
* Retrieves the tag type, which excludes the short tag
79+
* variants. For example, if the tag is <code>a</code>,
80+
* this will return <code>alpha</code>.
81+
*
82+
* @return string The lowercase tag type.
83+
*/
84+
public function getTagType() : string
85+
{
86+
if(empty($this->tagName)) {
87+
return VersionParser::TAG_TYPE_NONE;
88+
}
89+
90+
$short = ShortTags::getTagNames();
91+
$tagName = array_search($this->tagName, $short);
92+
if($tagName !== false) {
93+
return $tagName;
94+
}
95+
96+
return $this->tagName;
97+
}
98+
99+
public function getNumber() : int
100+
{
101+
return $this->number;
102+
}
103+
104+
public function isReleaseCandidate() : bool
105+
{
106+
return $this->isTagType(VersionParser::TAG_TYPE_RELEASE_CANDIDATE);
107+
}
108+
109+
public function isSnapshot() : bool
110+
{
111+
return $this->isTagType(VersionParser::TAG_TYPE_SNAPSHOT);
112+
}
113+
114+
public function isBeta() : bool
115+
{
116+
return $this->isTagType(VersionParser::TAG_TYPE_BETA);
117+
}
118+
119+
public function isAlpha() : bool
120+
{
121+
return $this->isTagType(VersionParser::TAG_TYPE_ALPHA);
122+
}
123+
124+
public function isPatch() : bool
125+
{
126+
return $this->isTagType(VersionParser::TAG_TYPE_PATCH);
127+
}
128+
129+
public function isDev() : bool
130+
{
131+
return $this->isTagType(VersionParser::TAG_TYPE_DEV);
132+
}
133+
134+
public function isStable() : bool
135+
{
136+
return
137+
$this->isTagType(VersionParser::TAG_TYPE_NONE)
138+
||
139+
$this->isTagType(VersionParser::TAG_TYPE_STABLE);
140+
}
141+
142+
public function isTagType(string $type) : bool
143+
{
144+
return $this->getTagType() === $type || $this->getTagName() === $type;
145+
}
146+
147+
public function toArray() : array
148+
{
149+
return array(
150+
'tagName' => $this->getTagName(),
151+
'tagType' => $this->getTagType(),
152+
'number' => $this->getTagName(),
153+
'branch' => $this->getBranchName(),
154+
'weight' => $this->getWeight(),
155+
'normalized' => $this->getNormalized()
156+
);
157+
}
158+
}

0 commit comments

Comments
 (0)