Completed
Push — master ( 45724e...d399d9 )
by Daniel
06:49
created

DomDynamicSelectByDanielGP::buildSelectId()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
rs 9.4286
cc 2
eloc 5
nc 2
nop 2
1
<?php
2
3
/**
4
 *
5
 * The MIT License (MIT)
6
 *
7
 * Copyright (c) 2015 Daniel Popiniuc
8
 *
9
 * Permission is hereby granted, free of charge, to any person obtaining a copy
10
 * of this software and associated documentation files (the "Software"), to deal
11
 * in the Software without restriction, including without limitation the rights
12
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13
 * copies of the Software, and to permit persons to whom the Software is
14
 * furnished to do so, subject to the following conditions:
15
 *
16
 * The above copyright notice and this permission notice shall be included in all
17
 * copies or substantial portions of the Software.
18
 *
19
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24
 *  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25
 * SOFTWARE.
26
 *
27
 */
28
29
namespace danielgp\common_lib;
30
31
/**
32
 * DOM component functions
33
 *
34
 * @author Daniel Popiniuc
35
 */
36
trait DomDynamicSelectByDanielGP
37
{
38
39
    protected function buildSelectId($selectName, $featArray)
40
    {
41
        $selectId = str_replace(['[', ']'], ['', ''], $selectName);
42
        if (isset($featArray['id_no'])) {
43
            $selectId .= $featArray['id_no'];
44
        }
45
        return $selectId;
46
    }
47
48
    private function eventOnChange($featArray)
49
    {
50
        $tempString = '';
51
        if (in_array('autosubmit', $featArray)) {
52
            return ' onchange="javascript:' . $tempString . 'submit();"';
53
        }
54
        if (in_array('additional_javascript_action', $featArray)) {
55
            return ' onchange="javascript:' . $featArray['additional_javascript_action'] . '"';
56
        }
57
        return '';
58
    }
59
60 View Code Duplication
    private function featureArraySimpleDirect($featArray, $identifier)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
61
    {
62
        if (in_array($identifier, $featArray)) {
63
            return ' ' . $identifier . '="' . $identifier . '"';
64
        }
65
        return '';
66
    }
67
68
    private function featureArraySimpleTranslated($featArray, $identifier)
69
    {
70
        $translation = [
71
            'hidden'      => ' style="visibility: hidden;',
72
            'multiselect' => ' multiple="multiple"',
73
        ];
74
        if (in_array($identifier, $featArray)) {
75
            return $translation[$identifier];
76
        }
77
        return '';
78
    }
79
80 View Code Duplication
    private function featureArrayAssociative($featArray, $identifier)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
81
    {
82
        if (in_array($identifier, $featArray)) {
83
            return ' ' . $identifier . '="' . $featArray[$identifier] . '"';
84
        }
85
        return '';
86
    }
87
88
    protected function setArrayToSelectNotReadOnly($aElements, $sDefaultValue, $selectName, $featArray = null)
89
    {
90
        if (!is_array($featArray)) {
91
            $featArray = [];
92
        }
93
        return '<select name="' . $selectName . '" '
94
                . 'id="' . $this->buildSelectId($selectName, $featArray) . '" '
95
                . 'size="' . $this->calculateSelectOptionsSize($aElements, $featArray) . '"'
0 ignored issues
show
Bug introduced by
It seems like calculateSelectOptionsSize() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
96
                . $this->eventOnChange($featArray)
97
                . $this->featureArraySimpleDirect($featArray, 'disabled')
98
                . $this->featureArraySimpleTranslated($featArray, 'hidden')
99
                . $this->featureArraySimpleTranslated($featArray, 'multiselect')
100
                . $this->featureArrayAssociative($featArray, 'style')
101
                . '>'
102
                . $this->setOptionsForSelect($aElements, $sDefaultValue, $featArray) . '</select>';
103
    }
104
105
    private function setOptionGroupEnd($crtGroup, $featArray)
106
    {
107
        if (isset($featArray['grouping'])) {
108
            if ($crtGroup != '') {
109
                return '</optgroup>';
110
            }
111
        }
112
        return '';
113
    }
114
115
    private function setOptionGroupFooterHeader($featArray, $crtValue, $crtGroup)
116
    {
117
        $sReturn = [];
118
        if (isset($featArray['grouping'])) {
119
            $tempString = substr($crtValue, 0, strpos($crtValue, $featArray['grouping']) + 1);
120
            if ($crtGroup != $tempString) {
121
                $sReturn[] = $this->setOptionGroupEnd($crtGroup, $featArray);
122
                $crtGroup  = $tempString;
123
                $sReturn[] = '<optgroup label="' . str_replace($featArray['grouping'], '', $crtGroup) . '">';
124
            }
125
        }
126
        return ['crtGroup' => $crtGroup, 'groupFooterHeader' => implode('', $sReturn)];
127
    }
128
129
    private function setOptionEmptyWithNullValue($featArray)
130
    {
131
        if (in_array('include_null', $featArray)) {
132
            return '<option value="NULL">&nbsp;</option>';
133
        }
134
        return '';
135
    }
136
137
    private function setOptionSelected($optionValue, $sDefaultValue, $featArray)
138
    {
139
        if (is_array($sDefaultValue)) {
140
            if (in_array($optionValue, $sDefaultValue)) {
141
                return ' selected="selected"';
142
            }
143
        }
144
        if (strcasecmp($optionValue, $sDefaultValue) === 0) {
145
            return ' selected="selected"';
146
        }
147
        if (isset($featArray['defaultValue_isSubstring'])) {
148
            $defaultValueArray = explode($featArray['defaultValue_isSubstring'], $sDefaultValue);
149
            if (in_array($optionValue, $defaultValueArray)) {
150
                return ' selected="selected"';
151
            }
152
        }
153
        return '';
154
    }
155
156
    private function setOptionStyle($featArray)
157
    {
158
        if (array_key_exists('styleForOption', $featArray)) {
159
            return ' style="' . $featArray['style'] . '"';
160
        }
161
        return '';
162
    }
163
164
    /**
165
     * Creates all the child tags required to populate a SELECT tag
166
     *
167
     * @param array $aElements
168
     * @param string|array $sDefaultValue
169
     * @param array $featArray
170
     * @return string
171
     */
172
    private function setOptionsForSelect($aElements, $sDefaultValue, $featArray = null)
173
    {
174
        if (is_null($featArray)) {
175
            $featArray = [];
176
        }
177
        $sReturn  = [];
178
        $crtGroup = null;
179
        foreach ($aElements as $key => $value) {
180
            $aFH       = $this->setOptionGroupFooterHeader($featArray, $value, $crtGroup);
181
            $crtGroup  = $aFH['crtGroup'];
182
            $sReturn[] = $aFH['groupFooterHeader']
183
                    . '<option value="' . $key . '"' . $this->setOptionSelected($key, $sDefaultValue, $featArray)
184
                    . $this->setOptionStyle($featArray) . '>'
185
                    . str_replace(['&', $crtGroup], ['&amp;', ''], $value) . '</option>';
186
        }
187
        $sReturn[] = $this->setOptionGroupEnd($crtGroup, $featArray);
188
        return $this->setOptionEmptyWithNullValue($featArray) . implode('', $sReturn);
189
    }
190
}
191