Completed
Push — master ( f77ca5...2d9d37 )
by Ryan
07:29
created

SectionNormalizer::normalize()   B

Complexity

Conditions 5
Paths 7

Size

Total Lines 30
Code Lines 11

Duplication

Lines 5
Ratio 16.67 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 5
loc 30
rs 8.439
cc 5
eloc 11
nc 7
nop 1
1
<?php namespace Anomaly\Streams\Platform\Ui\Form\Component\Section;
2
3
use Anomaly\Streams\Platform\Ui\Form\FormBuilder;
4
5
/**
6
 * Class SectionNormalizer
7
 *
8
 * @link          http://pyrocms.com/
9
 * @author        PyroCMS, Inc. <[email protected]>
10
 * @author        Ryan Thompson <[email protected]>
11
 * @package       Anomaly\Streams\Platform\Ui\Form\Component\Section
12
 */
13
class SectionNormalizer
14
{
15
16
    /**
17
     * Normalize the sections.
18
     *
19
     * @param FormBuilder $builder
20
     */
21
    public function normalize(FormBuilder $builder)
22
    {
23
        $sections = $builder->getSections();
24
25
        foreach ($sections as $slug => &$section) {
26
27
            if (is_string($section)) {
28
                $section = [
29
                    'view' => $section
30
                ];
31
            }
32
33
            /**
34
             * Make sure some default parameters exist.
35
             */
36
            $section['attributes'] = array_get($section, 'attributes', []);
37
38
            /**
39
             * Move all data-* keys
40
             * to attributes.
41
             */
42 View Code Duplication
            foreach ($section as $attribute => $value) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
43
                if (str_is('data-*', $attribute)) {
44
                    array_set($section, 'attributes.' . $attribute, array_pull($section, $attribute));
45
                }
46
            }
47
        }
48
49
        $builder->setSections($sections);
50
    }
51
}
52