|
1
|
|
|
<?php |
|
2
|
|
|
namespace Robo\State; |
|
3
|
|
|
|
|
4
|
|
|
/** |
|
5
|
|
|
* A State\Data object contains a "message" (the primary result) and a |
|
6
|
|
|
* data array (the persistent state). The message is transient, and does |
|
7
|
|
|
* not move into the persistent state unless explicitly copied there. |
|
8
|
|
|
*/ |
|
9
|
|
|
class Data extends \ArrayObject |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* @var string |
|
13
|
|
|
*/ |
|
14
|
|
|
protected $message; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @param string $message |
|
18
|
|
|
* @param array $data |
|
19
|
|
|
*/ |
|
20
|
|
|
public function __construct($message = '', $data = []) |
|
21
|
|
|
{ |
|
22
|
|
|
$this->message = $message; |
|
23
|
|
|
parent::__construct($data); |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @return array |
|
28
|
|
|
*/ |
|
29
|
|
|
public function getData() |
|
30
|
|
|
{ |
|
31
|
|
|
return $this->getArrayCopy(); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @return string |
|
36
|
|
|
*/ |
|
37
|
|
|
public function getMessage() |
|
38
|
|
|
{ |
|
39
|
|
|
return $this->message; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Merge another result into this result. Data already |
|
44
|
|
|
* existing in this result takes precedence over the |
|
45
|
|
|
* data in the Result being merged. |
|
46
|
|
|
* |
|
47
|
|
|
* @param \Robo\ResultData $result |
|
48
|
|
|
* |
|
49
|
|
|
* @return $this |
|
50
|
|
|
*/ |
|
51
|
|
|
public function merge(Data $result) |
|
52
|
|
|
{ |
|
53
|
|
|
$mergedData = $this->getArrayCopy() + $result->getArrayCopy(); |
|
54
|
|
|
$this->exchangeArray($mergedData); |
|
55
|
|
|
return $this; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Update the current data with the data provided in the parameter. |
|
60
|
|
|
* Provided data takes precedence. |
|
61
|
|
|
* |
|
62
|
|
|
* @param \ArrayObject $update |
|
63
|
|
|
* |
|
64
|
|
|
* @return $this |
|
65
|
|
|
*/ |
|
66
|
|
|
public function update(\ArrayObject $update) |
|
67
|
|
|
{ |
|
68
|
|
|
$iterator = $update->getIterator(); |
|
69
|
|
|
|
|
70
|
|
|
while ($iterator->valid()) { |
|
71
|
|
|
$this[$iterator->key()] = $iterator->current(); |
|
72
|
|
|
$iterator->next(); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
return $this; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Merge another result into this result. Data already |
|
80
|
|
|
* existing in this result takes precedence over the |
|
81
|
|
|
* data in the Result being merged. |
|
82
|
|
|
* |
|
83
|
|
|
* $data['message'] is handled specially, and is appended |
|
84
|
|
|
* to $this->message if set. |
|
85
|
|
|
* |
|
86
|
|
|
* @param array $data |
|
87
|
|
|
* |
|
88
|
|
|
* @return array |
|
89
|
|
|
*/ |
|
90
|
|
|
public function mergeData(array $data) |
|
91
|
|
|
{ |
|
92
|
|
|
$mergedData = $this->getArrayCopy() + $data; |
|
93
|
|
|
$this->exchangeArray($mergedData); |
|
94
|
|
|
return $mergedData; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* @return bool |
|
99
|
|
|
*/ |
|
100
|
|
|
public function hasExecutionTime() |
|
101
|
|
|
{ |
|
102
|
|
|
return isset($this['time']); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* @return null|float |
|
107
|
|
|
*/ |
|
108
|
|
|
public function getExecutionTime() |
|
109
|
|
|
{ |
|
110
|
|
|
if (!$this->hasExecutionTime()) { |
|
111
|
|
|
return null; |
|
112
|
|
|
} |
|
113
|
|
|
return $this['time']; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* Accumulate execution time |
|
118
|
|
|
*/ |
|
119
|
|
|
public function accumulateExecutionTime($duration) |
|
120
|
|
|
{ |
|
121
|
|
|
// Convert data arrays to scalar |
|
122
|
|
|
if (is_array($duration)) { |
|
123
|
|
|
$duration = isset($duration['time']) ? $duration['time'] : 0; |
|
124
|
|
|
} |
|
125
|
|
|
$this['time'] = $this->getExecutionTime() + $duration; |
|
126
|
|
|
return $this->getExecutionTime(); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* Accumulate the message. |
|
131
|
|
|
*/ |
|
132
|
|
|
public function accumulateMessage($message) |
|
133
|
|
|
{ |
|
134
|
|
|
if (!empty($this->message)) { |
|
135
|
|
|
$this->message .= "\n"; |
|
136
|
|
|
} |
|
137
|
|
|
$this->message .= $message; |
|
138
|
|
|
return $this->getMessage(); |
|
139
|
|
|
} |
|
140
|
|
|
} |
|
141
|
|
|
|