1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* HiPanel core package |
4
|
|
|
* |
5
|
|
|
* @link https://hipanel.com/ |
6
|
|
|
* @package hipanel-core |
7
|
|
|
* @license BSD-3-Clause |
8
|
|
|
* @copyright Copyright (c) 2014-2019, HiQDev (http://hiqdev.com/) |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace hipanel\actions; |
12
|
|
|
|
13
|
|
|
use hiqdev\hiart\Collection; |
14
|
|
|
use yii\base\InvalidConfigException; |
15
|
|
|
use yii\helpers\ArrayHelper; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Class SwitchAction. |
19
|
|
|
* |
20
|
|
|
* @property Collection|mixed $collection |
21
|
|
|
*/ |
22
|
|
|
class SwitchAction extends Action implements \ArrayAccess, \IteratorAggregate, \yii\base\Arrayable |
23
|
|
|
{ |
24
|
|
|
use \hiqdev\yii2\collection\ManagerTrait; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var string|callable the success message or a callback, that returns string. |
28
|
|
|
* Gets arguments |
29
|
|
|
*/ |
30
|
|
|
public $success; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var string the error message |
34
|
|
|
*/ |
35
|
|
|
public $error; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var SwitchRule instance of the current running rule |
39
|
|
|
*/ |
40
|
|
|
public $rule; |
41
|
|
|
|
42
|
|
|
/** {@inheritdoc} */ |
43
|
|
|
public function init() |
44
|
|
|
{ |
45
|
|
|
parent::init(); |
46
|
|
|
|
47
|
|
|
$this->addItems($this->getDefaultRules()); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @return array the default rules for the action. |
52
|
|
|
* You can override this method in child classes to set own default rules. |
53
|
|
|
*/ |
54
|
|
|
protected function getDefaultRules() |
55
|
|
|
{ |
56
|
|
|
return []; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function getItemConfig($name = null, array $config = []) |
60
|
|
|
{ |
61
|
|
|
return [ |
62
|
|
|
'class' => SwitchRule::class, |
63
|
|
|
'name' => $name, |
64
|
|
|
'switch' => $this, |
65
|
|
|
'save' => ArrayHelper::remove($config, 'save'), |
66
|
|
|
'success' => ArrayHelper::remove($config, 'success', $config), |
67
|
|
|
'error' => ArrayHelper::remove($config, 'error'), |
68
|
|
|
'flash' => ArrayHelper::remove($config, 'flash', true), |
69
|
|
|
]; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function run() |
73
|
|
|
{ |
74
|
|
|
foreach ($this->keys() as $k) { |
75
|
|
|
$rule = $this->getItem($k); |
76
|
|
|
if ($rule instanceof SwitchRule && $rule->isApplicable()) { |
77
|
|
|
$oldRule = $this->rule; |
78
|
|
|
$this->rule = $rule; |
79
|
|
|
$error = $this->perform(); |
80
|
|
|
$type = $error ? 'error' : 'success'; |
81
|
|
|
if ($rule->save && $rule->flash) { |
82
|
|
|
$this->addFlash($type, $error); |
|
|
|
|
83
|
|
|
} |
84
|
|
|
$result = $rule->run($type); |
85
|
|
|
$this->rule = $oldRule; |
86
|
|
|
|
87
|
|
|
return $result; |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
throw new InvalidConfigException('Broken SwitchAction, no applicable rule found'); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Does perform only if rule has 'save' enabled. |
96
|
|
|
*/ |
97
|
|
|
public function perform() |
98
|
|
|
{ |
99
|
|
|
if (!$this->rule->save) { |
100
|
|
|
return false; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
return parent::perform(); |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.