1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Workflow library. |
5
|
|
|
* |
6
|
|
|
* @package workflow |
7
|
|
|
* @author David Molineus <[email protected]> |
8
|
|
|
* @copyright 2014-2017 netzmacht David Molineus |
9
|
|
|
* @license LGPL 3.0 https://github.com/netzmacht/workflow |
10
|
|
|
* @filesource |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
declare(strict_types=1); |
14
|
|
|
|
15
|
|
|
namespace Netzmacht\Workflow\Flow\Condition\Transition; |
16
|
|
|
|
17
|
|
|
use Netzmacht\Workflow\Flow\Context; |
18
|
|
|
use Netzmacht\Workflow\Flow\Item; |
19
|
|
|
use Netzmacht\Workflow\Flow\Transition; |
20
|
|
|
use Netzmacht\Workflow\Util\Comparison; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Class PayloadPropertyCondition |
24
|
|
|
*/ |
25
|
|
|
class PayloadPropertyCondition implements Condition |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* Payload property name. |
29
|
|
|
* |
30
|
|
|
* @var string |
31
|
|
|
*/ |
32
|
|
|
private $property; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Expected value. |
36
|
|
|
* |
37
|
|
|
* @var mixed |
38
|
|
|
*/ |
39
|
|
|
private $value; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Comparison operator. |
43
|
|
|
* |
44
|
|
|
* @var string |
45
|
|
|
*/ |
46
|
|
|
private $operator; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* PayloadPropertyCondition constructor. |
50
|
|
|
* |
51
|
|
|
* @param string $property Payload property name. |
52
|
|
|
* @param mixed $value Expected value. |
53
|
|
|
* @param string $operator Comparison operator. |
54
|
|
|
*/ |
55
|
|
|
public function __construct(string $property, $value, string $operator = Comparison::EQUALS) |
56
|
|
|
{ |
57
|
|
|
$this->property = $property; |
58
|
|
|
$this->value = $value; |
59
|
|
|
$this->operator = $operator; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* {@inheritdoc} |
64
|
|
|
*/ |
65
|
|
|
public function match(Transition $transition, Item $item, Context $context): bool |
66
|
|
|
{ |
67
|
|
|
$payloadValue = $context->getPayload()->get($this->property); |
68
|
|
|
|
69
|
|
|
if (Comparison::compare($payloadValue, $this->value, $this->operator)) { |
70
|
|
|
return true; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
$context->addError( |
74
|
|
|
'transition.condition.payload_property.failed', |
75
|
|
|
[ |
76
|
|
|
'property' => $this->property, |
77
|
|
|
'expected' => $this->value, |
78
|
|
|
'actual' => $payloadValue, |
79
|
|
|
'operator' => $this->operator, |
80
|
|
|
] |
81
|
|
|
); |
82
|
|
|
|
83
|
|
|
return false; |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|