1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace TheCodingMachine\Discovery; |
5
|
|
|
|
6
|
|
|
use TheCodingMachine\Discovery\Utils\JsonException; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Represents an operation on an asset. |
10
|
|
|
* Either "add" or "remove". |
11
|
|
|
*/ |
12
|
|
|
class AssetOperation |
13
|
|
|
{ |
14
|
|
|
const ADD = 'add'; |
15
|
|
|
const REMOVE = 'remove'; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var string |
19
|
|
|
*/ |
20
|
|
|
private $operation; |
21
|
|
|
/** |
22
|
|
|
* @var Asset |
23
|
|
|
*/ |
24
|
|
|
private $asset; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @param string $operation Either "add" or "remove" |
28
|
|
|
* @param Asset $asset |
29
|
|
|
* @throws InvalidArgumentException |
30
|
|
|
*/ |
31
|
|
|
public function __construct(string $operation, Asset $asset) |
32
|
|
|
{ |
33
|
|
|
if ($operation !== 'add' && $operation !== 'remove') { |
34
|
|
|
throw new InvalidArgumentException('The action can only be add or remove.'); |
35
|
|
|
} |
36
|
|
|
$this->operation = $operation; |
37
|
|
|
$this->asset = $asset; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @return string |
42
|
|
|
*/ |
43
|
|
|
public function getOperation(): string |
44
|
|
|
{ |
45
|
|
|
return $this->operation; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @return Asset |
50
|
|
|
*/ |
51
|
|
|
public function getAsset(): Asset |
52
|
|
|
{ |
53
|
|
|
return $this->asset; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Builds the AssetOperation from a simple string. |
58
|
|
|
* |
59
|
|
|
* @param string $value |
60
|
|
|
* @param string $package |
61
|
|
|
* @return AssetOperation |
62
|
|
|
*/ |
63
|
|
|
public static function buildFromString(string $value, string $package, string $packageDir) : AssetOperation |
64
|
|
|
{ |
65
|
|
|
return new self(self::ADD, new Asset($value, $package, $packageDir, 0, [])); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Builds the AssetOperation from an array. |
70
|
|
|
* |
71
|
|
|
* @param array $array |
72
|
|
|
* @param string $package |
73
|
|
|
* @return AssetOperation |
74
|
|
|
* @throws JsonException |
75
|
|
|
*/ |
76
|
|
|
public static function buildFromArray(array $array, string $package, string $packageDir) : AssetOperation |
77
|
|
|
{ |
78
|
|
|
if (!isset($array['value'])) { |
79
|
|
|
throw new JsonException(sprintf('Missing "value" key in discovery.json from package %s', $package)); |
80
|
|
|
} |
81
|
|
|
$value = $array['value']; |
82
|
|
|
unset($array['value']); |
83
|
|
|
|
84
|
|
|
if (isset($array['priority'])) { |
85
|
|
|
$priority = $array['priority']; |
86
|
|
|
unset($array['priority']); |
87
|
|
|
} else { |
88
|
|
|
$priority = 0.0; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
if (isset($array['metadata'])) { |
92
|
|
|
$metadata = $array['metadata']; |
93
|
|
|
unset($array['metadata']); |
94
|
|
|
} else { |
95
|
|
|
$metadata = []; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
if (!empty($array)) { |
99
|
|
|
throw new JsonException(sprintf('Unexpected key(s) in discovery.json from package %s: "%s"', $package, implode(', ', array_keys($array)))); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
return new self(self::ADD, new Asset($value, $package, $packageDir, $priority, $metadata)); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|