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

calculateSelectOptionsSizeForced()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 11
rs 9.4286
cc 3
eloc 7
nc 3
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 DomBasicComponentsByDanielGP
37
{
38
39
    private function buildAttributesForTag($features)
40
    {
41
        if (!is_array($features)) {
42
            return '';
43
        }
44
        $attributes = '';
45
        foreach ($features as $key => $value) {
46
            $val = $this->buildAttributesForTagValueArray($value);
47
            $attributes .= ' ' . $key . '="' . $val . '"';
48
        }
49
        return $attributes;
50
    }
51
52
    private function buildAttributesForTagValueArray($value)
53
    {
54
        $val = $value;
55
        if (is_array($value)) {
56
            $valA = [];
57
            foreach ($value as $key2 => $value2) {
58
                $valA[] = $key2 . ':' . $value2;
59
            }
60
            $val = implode(';', $valA) . ';';
61
        }
62
        return $val;
63
    }
64
65
    private function calculateSelectOptionsSizeForced($aElements, $aFeatures = [])
66
    {
67
        $selectSize = 1;
68
        if (isset($aFeatures['size'])) {
69
            if ($aFeatures['size'] == 0) {
70
                $selectSize = count($aElements);
0 ignored issues
show
Unused Code introduced by
$selectSize is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
71
            }
72
            $selectSize = min(count($aElements), $aFeatures['size']);
73
        }
74
        return $selectSize;
75
    }
76
77
    /**
78
     * Calculate the optimal for all options within a select tag
79
     *
80
     * @param array $aElements
81
     * @param array $aFeatures
82
     * @return string|int
83
     */
84
    private function calculateSelectOptionsSize($aElements, $aFeatures = [])
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
85
    {
86
        if (is_array($aElements)) {
87
            if (!is_array($aFeatures)) {
88
                $aFeatures = [];
89
            }
90
            $selectSize = $this->calculateSelectOptionsSizeForced($aElements, $aFeatures);
91
            if ((in_array('include_null', $aFeatures)) && ($selectSize != '1')) {
92
                $selectSize++;
93
            }
94
            return $selectSize;
95
        }
96
        return '';
97
    }
98
99
    /**
100
     * Capatalize first letter of each word
101
     * AND filters only letters and numbers
102
     *
103
     * @param string $givenString
104
     * @return string
105
     */
106
    protected function cleanStringForId($givenString)
107
    {
108
        return preg_replace("/[^a-zA-Z0-9]/", '', ucwords($givenString));
109
    }
110
111
    /**
112
     * Cleans a string for certain internal rules
113
     *
114
     * @param type $urlString
115
     * @return type
116
     */
117
    protected function setCleanUrl($urlString)
118
    {
119
        $arrayToReplace = [
120
            '&#038;'    => '&amp;',
121
            '&'         => '&amp;',
122
            '&amp;amp;' => '&amp;',
123
            ' '         => '%20',
124
        ];
125
        $kys            = array_keys($arrayToReplace);
126
        $vls            = array_values($arrayToReplace);
127
        return str_replace($kys, $vls, filter_var($urlString, FILTER_SANITIZE_URL));
128
    }
129
130
    /**
131
     * Returns a div tag that clear any float
132
     *
133
     * @param integer $height
134
     */
135
    protected function setClearBoth1px($height = 1)
136
    {
137
        $divStyle = implode('', [
138
            'height:' . $height . 'px;',
139
            'line-height:' . $height . 'px;',
140
            'float:none;',
141
            'clear:both;',
142
            'margin:0px;'
143
        ]);
144
        return $this->setStringIntoTag('&nbsp;', 'div', ['style' => $divStyle]);
145
    }
146
147
    /**
148
     * Sets the no-cache header
149
     */
150
    protected function setHeaderNoCache($contentType = 'application/json')
151
    {
152
        header("Content-Type: " . $contentType . "; charset=utf-8");
153
        header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
154
        header("Cache-Control: no-store, no-cache, must-revalidate");
155
        header("Cache-Control: post-check=0, pre-check=0", false);
156
        header("Pragma: no-cache");
157
    }
158
159
    /**
160
     * Puts a given string into a specific short tag
161
     *
162
     * @param string $sTag
163
     * @param array $features
164
     * @return string
165
     */
166
    protected function setStringIntoShortTag($sTag, $features = null)
167
    {
168
        return '<' . $sTag . $this->buildAttributesForTag($features)
169
                . (isset($features['dont_close']) ? '' : '/') . '>';
170
    }
171
172
    /**
173
     * Puts a given string into a specific tag
174
     *
175
     * @param string $sString
176
     * @param string $sTag
177
     * @param array $features
178
     * @return string
179
     */
180
    protected function setStringIntoTag($sString, $sTag, $features = null)
181
    {
182
        return '<' . $sTag . $this->buildAttributesForTag($features) . '>' . $sString . '</' . $sTag . '>';
183
    }
184
}
185