1
|
|
|
<?php namespace Anomaly\Streams\Platform\Ui\Form\Component\Action; |
2
|
|
|
|
3
|
|
|
use Anomaly\Streams\Platform\Ui\Form\FormBuilder; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Class ActionNormalizer |
7
|
|
|
* |
8
|
|
|
* @link http://pyrocms.com/ |
9
|
|
|
* @author PyroCMS, Inc. <[email protected]> |
10
|
|
|
* @author Ryan Thompson <[email protected]> |
11
|
|
|
*/ |
12
|
|
|
class ActionNormalizer |
13
|
|
|
{ |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Normalize action input. |
17
|
|
|
* |
18
|
|
|
* @param FormBuilder $builder |
19
|
|
|
*/ |
20
|
|
|
public function normalize(FormBuilder $builder) |
21
|
|
|
{ |
22
|
|
|
$form = $builder->getForm(); |
23
|
|
|
$actions = $builder->getActions(); |
24
|
|
|
|
25
|
|
|
$prefix = $form->getOption('prefix'); |
26
|
|
|
|
27
|
|
|
foreach ($actions as $slug => &$action) { |
|
|
|
|
28
|
|
|
$action = $this->process($prefix, $slug, $action); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
$builder->setActions($actions); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Process the action. |
36
|
|
|
* |
37
|
|
|
* @param $prefix |
38
|
|
|
* @param $slug |
39
|
|
|
* @param $action |
40
|
|
|
* @return array |
41
|
|
|
*/ |
42
|
|
|
protected function process($prefix, $slug, $action) |
43
|
|
|
{ |
44
|
|
|
/* |
45
|
|
|
* If the slug is numeric and the action is |
46
|
|
|
* a string then treat the string as both the |
47
|
|
|
* action and the slug. This is OK as long as |
48
|
|
|
* there are not multiple instances of this |
49
|
|
|
* input using the same action which is not likely. |
50
|
|
|
*/ |
51
|
|
View Code Duplication |
if (is_numeric($slug) && is_string($action)) { |
|
|
|
|
52
|
|
|
$action = [ |
53
|
|
|
'slug' => $action, |
54
|
|
|
'action' => $action, |
55
|
|
|
]; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/* |
59
|
|
|
* If the slug is NOT numeric and the action is a |
60
|
|
|
* string then use the slug as the slug and the |
61
|
|
|
* action as the action. |
62
|
|
|
*/ |
63
|
|
View Code Duplication |
if (!is_numeric($slug) && is_string($action)) { |
|
|
|
|
64
|
|
|
$action = [ |
65
|
|
|
'slug' => $slug, |
66
|
|
|
'action' => $action, |
67
|
|
|
]; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/* |
71
|
|
|
* If the slug is not numeric and the action is an |
72
|
|
|
* array without a slug then use the slug for |
73
|
|
|
* the slug for the action. |
74
|
|
|
*/ |
75
|
|
View Code Duplication |
if (is_array($action) && !isset($action['slug']) && !is_numeric($slug)) { |
|
|
|
|
76
|
|
|
$action['slug'] = $slug; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/* |
80
|
|
|
* If the slug is not numeric and the action is an |
81
|
|
|
* array without a action then use the slug for |
82
|
|
|
* the action for the action. |
83
|
|
|
*/ |
84
|
|
View Code Duplication |
if (is_array($action) && !isset($action['action']) && !is_numeric($slug)) { |
|
|
|
|
85
|
|
|
$action['action'] = $slug; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Default to size "sm" |
90
|
|
|
*/ |
91
|
|
|
if (!isset($action['size'])) { |
92
|
|
|
$action['size'] = 'sm'; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/* |
96
|
|
|
* Make sure the attributes array is set. |
97
|
|
|
*/ |
98
|
|
|
$action['attributes'] = array_get($action, 'attributes', []); |
99
|
|
|
|
100
|
|
|
/* |
101
|
|
|
* Move all data-* keys |
102
|
|
|
* to attributes. |
103
|
|
|
*/ |
104
|
|
|
foreach ($action as $attribute => $value) { |
105
|
|
|
if (str_is('data-*', $attribute)) { |
106
|
|
|
array_set($action, 'attributes.' . $attribute, array_pull($action, $attribute)); |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/* |
111
|
|
|
* If the HREF is present outside of the attributes |
112
|
|
|
* then pull it and put it in the attributes array. |
113
|
|
|
*/ |
114
|
|
|
if (isset($action['url'])) { |
115
|
|
|
$action['attributes']['url'] = array_pull($action, 'url'); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/* |
119
|
|
|
* Make sure the HREF is absolute. |
120
|
|
|
*/ |
121
|
|
View Code Duplication |
if ( |
|
|
|
|
122
|
|
|
isset($action['redirect']) && |
123
|
|
|
is_string($action['redirect']) && |
124
|
|
|
!starts_with($action['redirect'], ['http', '{url.']) |
125
|
|
|
) { |
126
|
|
|
$action['redirect'] = url($action['redirect']); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
$action['attributes']['name'] = $prefix . 'action'; |
130
|
|
|
$action['attributes']['value'] = $action['slug']; |
131
|
|
|
|
132
|
|
View Code Duplication |
if (isset($action['dropdown'])) { |
|
|
|
|
133
|
|
|
foreach ($action['dropdown'] as $key => &$dropdown) { |
|
|
|
|
134
|
|
|
$dropdown = $this->process($prefix, $key, $dropdown); |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
return $action; |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.