1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* this file is part of pipelines */ |
4
|
|
|
|
5
|
|
|
namespace Ktomk\Pipelines\File; |
6
|
|
|
|
7
|
|
|
class Pipeline |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* @var File |
11
|
|
|
*/ |
12
|
|
|
private $file; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @var array|Step[] |
16
|
|
|
*/ |
17
|
|
|
private $steps; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Pipeline constructor. |
21
|
|
|
* @param File $file |
22
|
|
|
* @param array $definition |
23
|
|
|
* @throws \Ktomk\Pipelines\File\ParseException |
24
|
|
|
*/ |
25
|
5 |
|
public function __construct(File $file, array $definition) |
26
|
|
|
{ |
27
|
|
|
// quick validation |
28
|
5 |
|
if (!isset($definition[0])) { |
29
|
1 |
|
ParseException::__('Pipeline requires a list of steps'); |
30
|
|
|
} |
31
|
|
|
|
32
|
5 |
|
$this->file = $file; |
33
|
5 |
|
$this->steps = $this->parseSteps($definition); |
34
|
4 |
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @return File |
38
|
|
|
*/ |
39
|
2 |
|
public function getFile() |
40
|
|
|
{ |
41
|
2 |
|
return $this->file; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* get id of pipeline within the corresponding file object |
46
|
|
|
* |
47
|
|
|
* @return null|string id, can be null in fake/test conditions |
48
|
|
|
*/ |
49
|
1 |
|
public function getId() |
50
|
|
|
{ |
51
|
1 |
|
return $this->file->getIdFrom($this); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @return array|Step[] |
56
|
|
|
*/ |
57
|
1 |
|
public function getSteps() |
58
|
|
|
{ |
59
|
1 |
|
return $this->steps; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Specify data which should be serialized to JSON |
64
|
|
|
* |
65
|
|
|
* @return array |
66
|
|
|
* @since 5.4.0 |
67
|
|
|
*/ |
68
|
1 |
|
public function jsonSerialize() |
69
|
|
|
{ |
70
|
1 |
|
$steps = array(); |
71
|
1 |
|
foreach ($this->steps as $step) { |
72
|
1 |
|
$steps[] = $step->jsonSerialize(); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
return array( |
76
|
1 |
|
'steps' => $steps, |
77
|
|
|
); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @param array $definition |
82
|
|
|
* @throws \Ktomk\Pipelines\File\ParseException |
83
|
|
|
* @return array |
84
|
|
|
*/ |
85
|
5 |
|
private function parseSteps(array $definition) { |
86
|
5 |
|
$steps = array(); |
87
|
5 |
|
foreach ($definition as $index => $step) { |
88
|
5 |
|
if (!is_array($step)) { |
89
|
1 |
|
ParseException::__('Pipeline requires a list of steps'); |
90
|
|
|
} |
91
|
5 |
|
$steps[] = $this->step($index, $step); |
92
|
|
|
} |
93
|
|
|
|
94
|
4 |
|
return $steps; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @param int $index of step, from the zero based index in the list of steps |
99
|
|
|
* @param array $step |
100
|
|
|
* @return Step |
101
|
|
|
*/ |
102
|
5 |
|
private function step($index, array $step) |
103
|
|
|
{ |
104
|
5 |
|
if (!isset($step['step'])) { |
105
|
1 |
|
ParseException::__( |
106
|
1 |
|
"Missing required property 'step'" |
107
|
|
|
); |
108
|
|
|
} |
109
|
|
|
|
110
|
4 |
|
return new Step($this, $index, $step['step']); |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|