|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
|
|
4
|
|
|
namespace TheCodingMachine\Discovery; |
|
5
|
|
|
|
|
6
|
|
|
/** |
|
7
|
|
|
* Represent an asset. |
|
8
|
|
|
* Returned by the "getAssets" method. |
|
9
|
|
|
*/ |
|
10
|
|
|
class Asset implements \JsonSerializable |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* @var string |
|
14
|
|
|
*/ |
|
15
|
|
|
private $value; |
|
16
|
|
|
/** |
|
17
|
|
|
* @var string |
|
18
|
|
|
*/ |
|
19
|
|
|
private $package; |
|
20
|
|
|
/** |
|
21
|
|
|
* @var string |
|
22
|
|
|
*/ |
|
23
|
|
|
private $packageDir; |
|
24
|
|
|
/** |
|
25
|
|
|
* @var float |
|
26
|
|
|
*/ |
|
27
|
|
|
private $priority; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var mixed |
|
31
|
|
|
*/ |
|
32
|
|
|
private $metadata; |
|
33
|
|
|
|
|
34
|
|
|
public function __construct(string $value, string $package, string $packageDir, float $priority, $metadata) |
|
35
|
|
|
{ |
|
36
|
|
|
$this->value = $value; |
|
37
|
|
|
$this->package = $package; |
|
38
|
|
|
$this->packageDir = $packageDir; |
|
39
|
|
|
$this->priority = $priority; |
|
40
|
|
|
$this->metadata = $metadata; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @return string |
|
45
|
|
|
*/ |
|
46
|
|
|
public function getValue(): string |
|
47
|
|
|
{ |
|
48
|
|
|
return $this->value; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Returns the name of the Composer package. |
|
53
|
|
|
* |
|
54
|
|
|
* @return string |
|
55
|
|
|
*/ |
|
56
|
|
|
public function getPackage(): string |
|
57
|
|
|
{ |
|
58
|
|
|
return $this->package; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Returns the package directory, relative to root directory. |
|
63
|
|
|
* |
|
64
|
|
|
* @return string |
|
65
|
|
|
*/ |
|
66
|
|
|
public function getPackageDir(): string |
|
67
|
|
|
{ |
|
68
|
|
|
return $this->packageDir; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* The higher the priority the earlier the asset in the array. |
|
73
|
|
|
* |
|
74
|
|
|
* @return float |
|
75
|
|
|
*/ |
|
76
|
|
|
public function getPriority(): float |
|
77
|
|
|
{ |
|
78
|
|
|
return $this->priority; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* @return mixed |
|
83
|
|
|
*/ |
|
84
|
|
|
public function getMetadata() |
|
85
|
|
|
{ |
|
86
|
|
|
return $this->metadata; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* Specify data which should be serialized to JSON |
|
91
|
|
|
* @link http://php.net/manual/en/jsonserializable.jsonserialize.php |
|
92
|
|
|
* @return mixed data which can be serialized by <b>json_encode</b>, |
|
93
|
|
|
* which is a value of any type other than a resource. |
|
94
|
|
|
* @since 5.4.0 |
|
95
|
|
|
*/ |
|
96
|
|
|
public function jsonSerialize() |
|
97
|
|
|
{ |
|
98
|
|
|
return [ |
|
99
|
|
|
'value' => $this->getValue(), |
|
100
|
|
|
'package' => $this->getPackage(), |
|
101
|
|
|
'packageDir' => $this->getPackageDir(), |
|
102
|
|
|
'priority' => $this->getPriority(), |
|
103
|
|
|
'metadata' => $this->getMetadata() |
|
104
|
|
|
]; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
public static function fromArray(array $array) : Asset |
|
108
|
|
|
{ |
|
109
|
|
|
return new self($array['value'], $array['package'], $array['packageDir'], $array['priority'], $array['metadata']); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* Returns a simplified array representing the object. |
|
114
|
|
|
* |
|
115
|
|
|
* @return array|string |
|
116
|
|
|
*/ |
|
117
|
|
|
public function toSimpleArray() |
|
118
|
|
|
{ |
|
119
|
|
|
$item = [ |
|
120
|
|
|
'value' => $this->getValue(), |
|
121
|
|
|
]; |
|
122
|
|
|
|
|
123
|
|
|
if ($this->getPriority() !== 0.0) { |
|
124
|
|
|
$item['priority'] = $this->getPriority(); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
if (!empty($this->getMetadata())) { |
|
128
|
|
|
$item['metadata'] = $this->getMetadata(); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
if (count($item) === 1) { |
|
132
|
|
|
return $item['value']; |
|
133
|
|
|
} else { |
|
134
|
|
|
return $item; |
|
135
|
|
|
} |
|
136
|
|
|
} |
|
137
|
|
|
} |
|
138
|
|
|
|