1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Dominikb\ComposerLicenseChecker; |
6
|
|
|
|
7
|
|
|
class Dependency |
8
|
|
|
{ |
9
|
|
|
/** @var string */ |
10
|
|
|
private $name; |
11
|
|
|
|
12
|
|
|
/** @var string */ |
13
|
|
|
private $version; |
14
|
|
|
|
15
|
|
|
/** @var string[] */ |
16
|
|
|
private $licenses; |
17
|
|
|
|
18
|
|
|
// This is a constant that is used to represent the absence of a license. |
19
|
|
|
// Matches the value of the 'none' license in the composer.json file. |
20
|
|
|
const NO_LICENSES = ['none']; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Dependency constructor. |
24
|
|
|
* |
25
|
|
|
* @param string $name |
26
|
|
|
* @param string $version |
27
|
|
|
* @param string[] $licenses |
28
|
|
|
*/ |
29
|
13 |
|
public function __construct(string $name = '', string $version = '', array $licenses = []) |
30
|
|
|
{ |
31
|
13 |
|
$this->name = $name; |
32
|
13 |
|
$this->version = $version; |
33
|
13 |
|
$this->licenses = $licenses; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @return string |
38
|
|
|
*/ |
39
|
5 |
|
public function getName(): string |
40
|
|
|
{ |
41
|
5 |
|
return $this->name; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param string $name |
46
|
|
|
*/ |
47
|
5 |
|
public function setName(string $name): self |
48
|
|
|
{ |
49
|
5 |
|
$this->name = $name; |
50
|
|
|
|
51
|
5 |
|
return $this; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @return string |
56
|
|
|
*/ |
57
|
5 |
|
public function getVersion(): string |
58
|
|
|
{ |
59
|
5 |
|
return $this->version; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param string $version |
64
|
|
|
*/ |
65
|
5 |
|
public function setVersion(string $version): self |
66
|
|
|
{ |
67
|
5 |
|
$this->version = $version; |
68
|
|
|
|
69
|
5 |
|
return $this; |
70
|
|
|
} |
71
|
|
|
|
72
|
10 |
|
public function hasAnyLicense(): bool |
73
|
|
|
{ |
74
|
10 |
|
return ! empty($this->licenses); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @return string[] |
79
|
|
|
*/ |
80
|
10 |
|
public function getLicenses(): array |
81
|
|
|
{ |
82
|
10 |
|
if ($this->hasAnyLicense()) { |
83
|
9 |
|
return $this->licenses; |
84
|
|
|
} else { |
85
|
1 |
|
return self::NO_LICENSES; |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @param string[] $licenses |
91
|
|
|
*/ |
92
|
9 |
|
public function setLicenses(array $licenses): self |
93
|
|
|
{ |
94
|
9 |
|
$this->licenses = $licenses; |
95
|
|
|
|
96
|
9 |
|
return $this; |
97
|
|
|
} |
98
|
|
|
|
99
|
2 |
|
public function getAuthorName(): string |
100
|
|
|
{ |
101
|
2 |
|
return explode('/', $this->name)[0]; |
102
|
|
|
} |
103
|
|
|
|
104
|
2 |
|
public function getPackageName(): string |
105
|
|
|
{ |
106
|
2 |
|
$parts = explode('/', $this->name); |
107
|
|
|
|
108
|
2 |
|
if (count($parts) != 2) { |
109
|
1 |
|
return ''; |
110
|
|
|
} |
111
|
|
|
|
112
|
1 |
|
return $parts[1]; |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|