1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SoliDry\Extension; |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
use SoliDry\Helpers\Json; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class FsmTrait |
10
|
|
|
* |
11
|
|
|
* @package SoliDry\Extension |
12
|
|
|
* |
13
|
|
|
* @property ApiController entity |
14
|
|
|
*/ |
15
|
|
|
trait FsmTrait |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @param array $jsonProps JSON input properties |
19
|
|
|
* @throws \SoliDry\Exceptions\AttributesException |
20
|
|
|
*/ |
21
|
|
|
protected function checkFsmCreate(array &$jsonProps) : void |
22
|
|
|
{ |
23
|
|
|
$stateMachine = new StateMachine($this->entity); |
|
|
|
|
24
|
|
|
$stateField = $stateMachine->getField(); |
25
|
|
|
if (empty($jsonProps[$stateField])) { |
26
|
|
|
$stateMachine->setInitial($stateField); |
27
|
|
|
$jsonProps[$stateField] = $stateMachine->getInitial(); |
28
|
|
|
} else { |
29
|
|
|
foreach ($jsonProps as $k => $v) { |
30
|
|
|
if ($stateMachine->isStatedField($k) === true) { |
31
|
|
|
$stateMachine->setStates($k); |
32
|
|
|
if ($stateMachine->isInitial($v) === false) { |
33
|
|
|
// the field is under state machine rules and it is not initial state |
34
|
|
|
Json::outputErrors( |
35
|
|
|
[ |
36
|
|
|
[ |
37
|
|
|
JSONApiInterface::ERROR_TITLE => 'This state is not an initial.', |
38
|
|
|
JSONApiInterface::ERROR_DETAIL => 'The state - \'' . $v . '\' is not an initial.', |
39
|
|
|
], |
40
|
|
|
] |
41
|
|
|
); |
42
|
|
|
} |
43
|
|
|
} |
44
|
|
|
} |
45
|
|
|
} |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* |
50
|
|
|
* @param array $jsonProps |
51
|
|
|
* @param $model |
52
|
|
|
* @throws \SoliDry\Exceptions\AttributesException |
53
|
|
|
*/ |
54
|
|
|
protected function checkFsmUpdate(array $jsonProps, $model) : void |
55
|
|
|
{ |
56
|
|
|
$stateMachine = new StateMachine($this->entity); |
|
|
|
|
57
|
|
|
$field = $stateMachine->getField(); |
58
|
|
|
$stateMachine->setStates($field); |
59
|
|
|
if (empty($jsonProps[$field]) === false |
60
|
|
|
&& $stateMachine->isStatedField($field) === true |
61
|
|
|
&& $stateMachine->isTransitive($model->$field, $jsonProps[$field]) === false |
62
|
|
|
) { |
63
|
|
|
// the field is under state machine rules and it is not transitive in this direction |
64
|
|
|
Json::outputErrors( |
65
|
|
|
[ |
66
|
|
|
[ |
67
|
|
|
JSONApiInterface::ERROR_TITLE => 'State can`t be changed through this way.', |
68
|
|
|
JSONApiInterface::ERROR_DETAIL => 'The state of a field/column - \'' . $field . '\' can`t be changed from: \'' . $model->$field . '\', to: \'' . $jsonProps[$field] . '\'', |
69
|
|
|
], |
70
|
|
|
] |
71
|
|
|
); |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
} |