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
|
|
|
* @param string message |
44
|
|
|
*/ |
45
|
|
|
public function setMessage($message) |
46
|
|
|
{ |
47
|
|
|
$this->message = $message; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Merge another result into this result. Data already |
52
|
|
|
* existing in this result takes precedence over the |
53
|
|
|
* data in the Result being merged. |
54
|
|
|
* |
55
|
|
|
* @param \Robo\ResultData $result |
56
|
|
|
* |
57
|
|
|
* @return $this |
58
|
|
|
*/ |
59
|
|
|
public function merge(Data $result) |
60
|
|
|
{ |
61
|
|
|
$mergedData = $this->getArrayCopy() + $result->getArrayCopy(); |
62
|
|
|
$this->exchangeArray($mergedData); |
63
|
|
|
return $this; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Update the current data with the data provided in the parameter. |
68
|
|
|
* Provided data takes precedence. |
69
|
|
|
* |
70
|
|
|
* @param \ArrayObject $update |
71
|
|
|
* |
72
|
|
|
* @return $this |
73
|
|
|
*/ |
74
|
|
|
public function update(\ArrayObject $update) |
75
|
|
|
{ |
76
|
|
|
$iterator = $update->getIterator(); |
77
|
|
|
|
78
|
|
|
while ($iterator->valid()) { |
79
|
|
|
$this[$iterator->key()] = $iterator->current(); |
80
|
|
|
$iterator->next(); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
return $this; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Merge another result into this result. Data already |
88
|
|
|
* existing in this result takes precedence over the |
89
|
|
|
* data in the Result being merged. |
90
|
|
|
* |
91
|
|
|
* $data['message'] is handled specially, and is appended |
92
|
|
|
* to $this->message if set. |
93
|
|
|
* |
94
|
|
|
* @param array $data |
95
|
|
|
* |
96
|
|
|
* @return array |
97
|
|
|
*/ |
98
|
|
|
public function mergeData(array $data) |
99
|
|
|
{ |
100
|
|
|
$mergedData = $this->getArrayCopy() + $data; |
101
|
|
|
$this->exchangeArray($mergedData); |
102
|
|
|
return $mergedData; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @return bool |
107
|
|
|
*/ |
108
|
|
|
public function hasExecutionTime() |
109
|
|
|
{ |
110
|
|
|
return isset($this['time']); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @return null|float |
115
|
|
|
*/ |
116
|
|
|
public function getExecutionTime() |
117
|
|
|
{ |
118
|
|
|
if (!$this->hasExecutionTime()) { |
119
|
|
|
return null; |
120
|
|
|
} |
121
|
|
|
return $this['time']; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Accumulate execution time |
126
|
|
|
*/ |
127
|
|
|
public function accumulateExecutionTime($duration) |
128
|
|
|
{ |
129
|
|
|
// Convert data arrays to scalar |
130
|
|
|
if (is_array($duration)) { |
131
|
|
|
$duration = isset($duration['time']) ? $duration['time'] : 0; |
132
|
|
|
} |
133
|
|
|
$this['time'] = $this->getExecutionTime() + $duration; |
134
|
|
|
return $this->getExecutionTime(); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Accumulate the message. |
139
|
|
|
*/ |
140
|
|
|
public function accumulateMessage($message) |
141
|
|
|
{ |
142
|
|
|
if (!empty($this->message)) { |
143
|
|
|
$this->message .= "\n"; |
144
|
|
|
} |
145
|
|
|
$this->message .= $message; |
146
|
|
|
return $this->getMessage(); |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
|