forked from ProcessMaker/processmaker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTimerExpression.php
More file actions
195 lines (172 loc) · 4.76 KB
/
TimerExpression.php
File metadata and controls
195 lines (172 loc) · 4.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
<?php
namespace ProcessMaker\Models;
use DateInterval;
use DateTime;
use Exception;
use Mustache_Engine;
use ProcessMaker\Nayra\Bpmn\BaseTrait;
use ProcessMaker\Nayra\Bpmn\Models\DatePeriod;
use ProcessMaker\Nayra\Contracts\Bpmn\FormalExpressionInterface;
/**
* FormalExpression to handle time events
*/
class TimerExpression implements FormalExpressionInterface
{
use BaseTrait;
/**
* Languages supported for expressions
*/
const languages = [
'Timer' => [],
];
const defaultLanguage = 'Timer';
/**
* Initialize the expression language evaluator
*/
private function initFormalExpression()
{
}
/**
* Get the body of the Expression.
*
* @return string
*/
public function getBody()
{
return $this->getProperty(FormalExpressionInterface::BPMN_PROPERTY_BODY);
}
/**
* Set the body of the Expression.
*
* @param string $body
*
* @return TimerExpression
*/
public function setBody($body)
{
$this->setProperty(FormalExpressionInterface::BPMN_PROPERTY_BODY, $body);
return $this;
}
/**
* Get the type that this Expression returns when evaluated.
*
* @return string
*/
public function getEvaluatesToType()
{
return $this->getProperty(FormalExpressionInterface::BPMN_PROPERTY_EVALUATES_TO_TYPE_REF);
}
/**
* Get the expression language.
*
* @return string
*/
public function getLanguage()
{
return $this->getProperty(FormalExpressionInterface::BPMN_PROPERTY_LANGUAGE);
}
/**
* Invoke the format expression.
*
* @param mixed $data
*
* @return string
*/
public function __invoke($data)
{
$expression = $this->getProperty(FormalExpressionInterface::BPMN_PROPERTY_BODY);
$expression = $this->mustacheTimerExpression($expression, $data);
return $this->getDateExpression($expression)
?: $this->getCycleExpression($expression)
?: $this->getDurationExpression($expression)
?: $this->getMultipleCycleExpression($expression);
}
/**
* Parse mustache syntax in timer expressions
*
* @param string $expression
* @param array $data
*
* @return mixed
*/
private function mustacheTimerExpression($expression, $data)
{
$mustache = new Mustache_Engine();
return $mustache->render($expression, $data);
}
/**
* Get a DateTime if the expression is a date.
*
* @return \DateTime
*/
protected function getDateExpression($expression)
{
try {
$date = new DateTime($expression);
} catch (Exception $e) {
$date = false;
}
return $date;
}
/**
* Get a DatePeriod if the expression is a cycle.
*
* Ex. R4/2018-05-01T00:00:00Z/PT1M
* R/2018-05-01T00:00:00Z/PT1M/2025-10-02T00:00:00Z
*
* @return \DatePeriod
*/
protected function getCycleExpression($expression)
{
try {
//Improve Repeating intervals (R/start/interval/end) configuration
if (preg_match('/^R\/([^\/]+)\/([^\/]+)\/([^\/]+)$/', $expression, $repeating)) {
$cycle = new DatePeriod(new DateTime($repeating[1]), new DateInterval($repeating[2]), new DateTime($repeating[3]));
} elseif (preg_match('/^R\/([^\/]+)\/([^\/]+)$/', $expression, $repeating)) {
$cycle = new DatePeriod(new DateTime($repeating[1]), new DateInterval($repeating[2]), -1);
} else {
$cycle = new DatePeriod($expression);
}
} catch (Exception $e) {
$cycle = false;
}
return $cycle;
}
/**
* Get a multiple DatePeriod if the expression is a multiple cycle.
*
* Ex. 2019-08-01T00:00:00Z|R4/2019-08-01T00:00:00Z/PT1W|R4/2019-08-02T00:00:00Z/PT1W
*
* throws every week thursday and friday
*
* @return array
*/
protected function getMultipleCycleExpression($expression)
{
try {
$parts = explode('|', $expression);
$firstDate = new DateTime(array_shift($parts));
$cycles = [];
foreach ($parts as $part) {
$cycles[] = $this->getCycleExpression($part);
}
} catch (Exception $e) {
$cycles = false;
}
return $cycles;
}
/**
* Get a DateInterval if the expression is a duration.
*
* @return \DateInterval
*/
protected function getDurationExpression($expression)
{
try {
$duration = new DateInterval($expression);
} catch (Exception $e) {
$duration = false;
}
return $duration;
}
}