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 |
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
|
|
|
* Image constructor. |
28
|
|
|
* |
29
|
|
|
* @param string|array $image |
30
|
|
|
*/ |
31
|
9 |
|
public function __construct($image) |
32
|
|
|
{ |
33
|
9 |
|
$this->properties = new Properties(); |
34
|
9 |
|
$this->parse($image); |
35
|
5 |
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param string|array $image |
39
|
|
|
*/ |
40
|
9 |
|
private function parse($image) |
41
|
|
|
{ |
42
|
9 |
|
if (is_string($image)) { |
43
|
3 |
|
$this->parseString($image); |
44
|
6 |
|
} elseif (is_array($image)) { |
|
|
|
|
45
|
5 |
|
$this->parseArray($image); |
46
|
|
|
} else { |
47
|
1 |
|
ParseException::__( |
48
|
1 |
|
"'image' expects either 'a string' or 'a section'" |
49
|
|
|
); |
50
|
|
|
} |
51
|
5 |
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @param string $name |
55
|
|
|
*/ |
56
|
7 |
|
private function parseString($name) |
57
|
|
|
{ |
58
|
7 |
|
if (!ImageName::validate($name)) { |
59
|
1 |
|
ParseException::__( |
60
|
1 |
|
sprintf("'image' invalid Docker image name: '%s'", $name) |
61
|
|
|
); |
62
|
|
|
} |
63
|
|
|
|
64
|
6 |
|
$this->name = new ImageName($name); |
65
|
6 |
|
} |
66
|
|
|
|
67
|
5 |
|
private function parseArray(array $image) |
68
|
|
|
{ |
69
|
5 |
|
if (!isset($image['name'])) { |
70
|
1 |
|
ParseException::__("'image' needs a name"); |
71
|
|
|
} |
72
|
4 |
|
$this->parseString($image['name']); |
73
|
4 |
|
unset($image['name']); |
74
|
|
|
|
75
|
4 |
|
$entries = array('username', 'password', 'email', 'aws'); |
76
|
4 |
|
$image = $this->properties->import($image, $entries); |
77
|
|
|
|
78
|
4 |
|
if (!empty($image)) { |
79
|
1 |
|
ParseException::__(sprintf( |
80
|
1 |
|
"unknown 'image' property '%s', expects either a string or a section", |
81
|
1 |
|
key($image) |
82
|
|
|
)); |
83
|
|
|
} |
84
|
3 |
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @return ImageName image name |
88
|
|
|
*/ |
89
|
1 |
|
public function getName() |
90
|
|
|
{ |
91
|
1 |
|
return $this->name; |
92
|
|
|
} |
93
|
|
|
|
94
|
1 |
|
public function __toString() |
95
|
|
|
{ |
96
|
1 |
|
return (string)$this->name; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @return Properties properties additional to name |
101
|
|
|
*/ |
102
|
1 |
|
public function getProperties() |
103
|
|
|
{ |
104
|
1 |
|
return $this->properties; |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|