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
178 lines (155 loc) · 4.36 KB
/
FormalExpression.php
File metadata and controls
178 lines (155 loc) · 4.36 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
<?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;
const defaultLanguage = 'FEEL';
const templateEngine = 'Mustache';
/**
* Languages supported for expressions
*/
const languages = [
'FEEL' => ['feelExpression', 'feelEncode'],
];
/**
* FEEL expression object to be used to evaluate
* @var \Symfony\Component\ExpressionLanguage\ExpressionLanguage $expressionLanguage
*/
private $feelExpression;
/**
* Initialize the expression language evaluator
*/
private function initFormalExpression()
{
$this->feelExpression = new ExpressionLanguage();
}
/**
* Prepare the data for the FEEL evaluator
*
* @param array $data
*
* @return array
*/
private function feelEncode(array $data)
{
return (array) json_decode(json_encode($data));
}
private function getTemplateEngine()
{
if (self::templateEngine == 'Mustache') {
return new MustacheExpressionEvaluator();
}
else {
throw new \Exception("Template engine not supported");
}
}
/**
* 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($this->getTemplateEngine(), $this->getBody(),$data);
}
/**
* Evaluate an expression using an specific template engine
* @param \ProcessMaker\Contracts\TemplateExpressionInterface $templateEngine
* @param string $expression
* @param array $data
*
* @return bool
*
* @throws ExpressionFailedException
* @throws ScriptLanguageNotSupported
* @throws SyntaxErrorException
*/
private function evaluate($templateEngine, $expression, array $data)
{
$body = $templateEngine->render($expression, $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, $body);
} catch (Throwable $error) {
throw new ExpressionFailedException($error);
}
}
}