1
|
|
|
<?php namespace Anomaly\Streams\Platform\Ui\ControlPanel\Component\Section; |
2
|
|
|
|
3
|
|
|
use Anomaly\Streams\Platform\Addon\Module\ModuleCollection; |
4
|
|
|
use Anomaly\Streams\Platform\Ui\ControlPanel\ControlPanelBuilder; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* Class SectionNormalizer |
8
|
|
|
* |
9
|
|
|
* @link http://pyrocms.com/ |
10
|
|
|
* @author PyroCMS, Inc. <[email protected]> |
11
|
|
|
* @author Ryan Thompson <[email protected]> |
12
|
|
|
*/ |
13
|
|
|
class SectionNormalizer |
14
|
|
|
{ |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* The module collection. |
18
|
|
|
* |
19
|
|
|
* @var ModuleCollection |
20
|
|
|
*/ |
21
|
|
|
protected $modules; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Create a new SectionNormalizer instance. |
25
|
|
|
* |
26
|
|
|
* @param ModuleCollection $modules |
27
|
|
|
*/ |
28
|
|
|
public function __construct(ModuleCollection $modules) |
29
|
|
|
{ |
30
|
|
|
$this->modules = $modules; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Normalize the section input. |
35
|
|
|
* |
36
|
|
|
* @param ControlPanelBuilder $builder |
37
|
|
|
*/ |
38
|
|
|
public function normalize(ControlPanelBuilder $builder) |
39
|
|
|
{ |
40
|
|
|
$sections = $builder->getSections(); |
41
|
|
|
|
42
|
|
|
/* |
43
|
|
|
* Move child sections into main array. |
44
|
|
|
*/ |
45
|
|
|
foreach ($sections as $slug => &$section) { |
46
|
|
|
if (isset($section['sections'])) { |
47
|
|
|
foreach ($section['sections'] as $key => &$child) { |
48
|
|
|
$child['parent'] = array_get($section, 'slug', $slug); |
49
|
|
|
$child['slug'] = array_get($child, 'slug', $key); |
50
|
|
|
|
51
|
|
|
$sections[$key] = $child; |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/* |
57
|
|
|
* Loop over each section and make sense of the input |
58
|
|
|
* provided for the given module. |
59
|
|
|
*/ |
60
|
|
|
foreach ($sections as $slug => &$section) { |
61
|
|
|
|
62
|
|
|
/* |
63
|
|
|
* If the slug is not valid and the section |
64
|
|
|
* is a string then use the section as the slug. |
65
|
|
|
*/ |
66
|
|
|
if (is_numeric($slug) && is_string($section)) { |
67
|
|
|
$section = [ |
68
|
|
|
'slug' => $section, |
69
|
|
|
]; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/* |
73
|
|
|
* If the slug is a string and the title is not |
74
|
|
|
* set then use the slug as the slug. |
75
|
|
|
*/ |
76
|
|
|
if (is_string($slug) && !isset($section['slug'])) { |
77
|
|
|
$section['slug'] = $slug; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/* |
81
|
|
|
* Make sure we have attributes. |
82
|
|
|
*/ |
83
|
|
|
$section['attributes'] = array_get($section, 'attributes', []); |
84
|
|
|
|
85
|
|
|
/* |
86
|
|
|
* Move the HREF into attributes. |
87
|
|
|
*/ |
88
|
|
|
if (isset($section['href'])) { |
89
|
|
|
$section['attributes']['href'] = array_pull($section, 'href'); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/* |
93
|
|
|
* Move all data-* keys |
94
|
|
|
* to attributes. |
95
|
|
|
*/ |
96
|
|
|
foreach ($section as $attribute => $value) { |
97
|
|
|
if (str_is('data-*', $attribute)) { |
98
|
|
|
array_set($section, 'attributes.' . $attribute, array_pull($section, $attribute)); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/* |
103
|
|
|
* Move the data-href into the permalink. |
104
|
|
|
* |
105
|
|
|
* @deprecated as of v3.2 |
106
|
|
|
*/ |
107
|
|
|
if (!isset($section['permalink']) && isset($section['attributes']['data-href'])) { |
108
|
|
|
$section['permalink'] = array_pull($section, 'attributes.data-href'); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/* |
112
|
|
|
* Make sure the HREF and permalink are absolute. |
113
|
|
|
*/ |
114
|
|
|
if ( |
115
|
|
|
isset($section['attributes']['href']) && |
116
|
|
|
is_string($section['attributes']['href']) && |
117
|
|
|
!starts_with($section['attributes']['href'], 'http') |
118
|
|
|
) { |
119
|
|
|
$section['attributes']['href'] = url($section['attributes']['href']); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
View Code Duplication |
if ( |
|
|
|
|
123
|
|
|
isset($section['permalink']) && |
124
|
|
|
is_string($section['permalink']) && |
125
|
|
|
!starts_with($section['permalink'], 'http') |
126
|
|
|
) { |
127
|
|
|
$section['permalink'] = url($section['permalink']); |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
$builder->setSections(array_values($sections)); |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.