forked from ProcessMaker/processmaker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFormalExpression.php
More file actions
170 lines (152 loc) · 3.98 KB
/
FormalExpression.php
File metadata and controls
170 lines (152 loc) · 3.98 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
<?php
namespace ProcessMaker\Models;
use Mustache_Engine;
use ProcessMaker\Exception\ExpressionFailedException;
use ProcessMaker\Exception\ScriptLanguageNotSupported;
use ProcessMaker\Exception\SyntaxErrorException;
use ProcessMaker\Nayra\Bpmn\BaseTrait;
use ProcessMaker\Nayra\Contracts\Bpmn\FormalExpressionInterface;
use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
use Symfony\Component\ExpressionLanguage\SyntaxError;
use Throwable;
/**
* FormalExpression
*
* @package ProcessMaker\Model
*/
class FormalExpression implements FormalExpressionInterface
{
use BaseTrait;
/**
* Languages supported for expressions
*/
const languages = [
'FEEL' => ['feelExpression', 'feelEncode'],
];
const defaultLanguage = 'FEEL';
/**
* @var \Symfony\Component\ExpressionLanguage\ExpressionLanguage $expressionLanguage
*/
private $feelExpression;
/**
* Initialize the expression language evaluator
*/
private function initFormalExpression()
{
$this->feelExpression = new ExpressionLanguage();
}
/**
* Parse mustache syntax
*
* @param string $expression
* @param array $data
*
* @return mixed
*/
private function mustacheExpression($expression, $data)
{
$mustache = new Mustache_Engine();
return $mustache->render($expression, $data);
}
/**
* Evaluate the format expression.
*
* @param array $data
*
* @return string
*/
private function evaluate(array $data)
{
$body = $this->mustacheExpression($this->getBody(), $data);
if (!trim($body)) {
return true;
}
$language = $this->getLanguage() ?: self::defaultLanguage;
if (!isset(self::languages[$language])) {
throw new ScriptLanguageNotSupported($language);
}
$evaluator = self::languages[$language][0];
$encoder = isset(self::languages[$language][1]) ? self::languages[$language][1] : null;
try {
$values = $encoder ? $this->$encoder($data) : $data;
return $this->$evaluator->evaluate($body, $values);
} catch (SyntaxError $syntaxError) {
throw new SyntaxErrorException($syntaxError);
} catch (Throwable $error) {
throw new ExpressionFailedException($error);
}
}
/**
* Prepare the data for the FEEL evaluator
*
* @param array $data
*
* @return array
*/
private function feelEncode(array $data)
{
return (array) json_decode(json_encode($data));
}
/**
* Get the body of the Expression.
*
* @return string
*/
public function getBody()
{
return $this->getProperty(FormalExpressionInterface::BPMN_PROPERTY_BODY);
}
/**
* Get the body of the Expression.
*
* @param string $body
*
* @return $this
*/
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);
}
/**
* Get the expression language.
*
* @param string $language
*
* @return $this
*/
public function setLanguage($language)
{
$this->setProperty(FormalExpressionInterface::BPMN_PROPERTY_LANGUAGE, $language);
return $this;
}
/**
* Invoke the format expression.
*
* @param mixed $data
*
* @return string
*/
public function __invoke($data)
{
return $this->evaluate($data);
}
}