1 | <?php |
||
2 | |||
3 | /* this file is part of pipelines */ |
||
4 | |||
5 | namespace Ktomk\Pipelines\File; |
||
6 | |||
7 | use Ktomk\Pipelines\Value\Properties; |
||
8 | |||
9 | /** |
||
10 | * Class Image |
||
11 | * |
||
12 | * @package Ktomk\Pipelines\File\File |
||
13 | */ |
||
14 | class Image |
||
15 | { |
||
16 | /** |
||
17 | * @var ImageName |
||
18 | */ |
||
19 | private $name; |
||
20 | |||
21 | /** |
||
22 | * @var Properties |
||
23 | */ |
||
24 | private $properties; |
||
25 | |||
26 | /** |
||
27 | * if an 'image' entry is set, validate it is a string or a section. |
||
28 | * |
||
29 | * @param array $array |
||
30 | * |
||
31 | * @throws ParseException if the image name is invalid |
||
32 | * |
||
33 | * @return void |
||
34 | */ |
||
35 | 9 | public static function validate(array $array) |
|
36 | { |
||
37 | 9 | if (!array_key_exists('image', $array)) { |
|
38 | 1 | return; |
|
39 | } |
||
40 | |||
41 | 8 | $image = $array['image']; |
|
42 | |||
43 | 8 | if (is_array($image) && isset($image['name'])) { |
|
44 | 4 | if (!ImageName::validate($image['name'])) { |
|
45 | 2 | throw new ParseException(sprintf( |
|
46 | "'image' invalid Docker image name: '%s'", |
||
47 | 2 | $image['name'] |
|
48 | )); |
||
49 | } |
||
50 | |||
51 | 2 | return; |
|
52 | } |
||
53 | |||
54 | 4 | if (!is_string($image)) { |
|
55 | 2 | throw new ParseException("'image' requires a Docker image name"); |
|
56 | } |
||
57 | 2 | if (!ImageName::validate($image)) { |
|
58 | 1 | throw new ParseException( |
|
59 | 1 | sprintf("'image' invalid Docker image name: '%s'", $image) |
|
60 | ); |
||
61 | } |
||
62 | } |
||
63 | |||
64 | /** |
||
65 | * Image constructor. |
||
66 | * |
||
67 | * @param array|string $image |
||
68 | * |
||
69 | * @throws ParseException |
||
70 | */ |
||
71 | 10 | public function __construct($image) |
|
72 | { |
||
73 | 10 | $this->properties = new Properties(); |
|
74 | 10 | $this->parse($image); |
|
75 | } |
||
76 | |||
77 | 1 | public function __toString() |
|
78 | { |
||
79 | 1 | return (string)$this->name; |
|
80 | } |
||
81 | |||
82 | /** |
||
83 | * @return ImageName image name |
||
84 | */ |
||
85 | 2 | public function getName() |
|
86 | { |
||
87 | 2 | return $this->name; |
|
88 | } |
||
89 | |||
90 | /** |
||
91 | * @return Properties properties additional to name |
||
92 | */ |
||
93 | 1 | public function getProperties() |
|
94 | { |
||
95 | 1 | return $this->properties; |
|
96 | } |
||
97 | |||
98 | /** |
||
99 | * Specify data which should be serialized to JSON |
||
100 | * |
||
101 | * @return array |
||
102 | */ |
||
103 | 1 | public function jsonSerialize() |
|
104 | { |
||
105 | return array( |
||
106 | 1 | 'name' => (string)$this->getName(), |
|
107 | ); |
||
108 | } |
||
109 | |||
110 | /** |
||
111 | * @param array|string $image |
||
112 | * |
||
113 | * @throws ParseException |
||
114 | * |
||
115 | * @return void |
||
116 | */ |
||
117 | 10 | private function parse($image) |
|
118 | { |
||
119 | 10 | if (!(is_string($image) || is_array($image))) { |
|
0 ignored issues
–
show
introduced
by
![]() |
|||
120 | 1 | throw new ParseException( |
|
121 | "'image' expects either 'a string' or 'a section'" |
||
122 | ); |
||
123 | } |
||
124 | |||
125 | 9 | if (is_string($image)) { |
|
126 | 3 | $this->parseString($image); |
|
127 | |||
128 | 2 | return; |
|
129 | } |
||
130 | |||
131 | 6 | $this->parseArray($image); |
|
132 | } |
||
133 | |||
134 | /** |
||
135 | * @param string $name |
||
136 | * |
||
137 | * @throws ParseException |
||
138 | * |
||
139 | * @return void |
||
140 | */ |
||
141 | 8 | private function parseString($name) |
|
142 | { |
||
143 | 8 | if (!ImageName::validate($name)) { |
|
144 | 1 | throw new ParseException( |
|
145 | 1 | sprintf("'image' invalid Docker image name: '%s'", $name) |
|
146 | ); |
||
147 | } |
||
148 | |||
149 | 7 | $this->name = new ImageName($name); |
|
150 | } |
||
151 | |||
152 | /** |
||
153 | * @param array $image |
||
154 | * |
||
155 | * @throws ParseException |
||
156 | * |
||
157 | * @return void |
||
158 | */ |
||
159 | 6 | private function parseArray(array $image) |
|
160 | { |
||
161 | 6 | if (!isset($image['name'])) { |
|
162 | 1 | throw new ParseException("'image' needs a name"); |
|
163 | } |
||
164 | 5 | $this->parseString($image['name']); |
|
165 | 5 | unset($image['name']); |
|
166 | |||
167 | 5 | $entries = array('run-as-user', 'username', 'password', 'email', 'aws'); |
|
168 | 5 | $image = $this->properties->import($image, $entries); |
|
169 | |||
170 | 5 | if (!empty($image)) { |
|
171 | 1 | throw new ParseException(sprintf( |
|
172 | "unknown 'image' property '%s', expects either a string or a section", |
||
173 | 1 | key($image) |
|
174 | )); |
||
175 | } |
||
176 | } |
||
177 | } |
||
178 |