OnArray.php 5.73 KB
Newer Older
1
2
3
4
5
6
7
8
9
<?php
/**
 * Created by PhpStorm.
 * User: crose
 * Date: 1/10/16
 * Time: 11:47 AM
 */


10
namespace qfq;
11
12
13

require_once(__DIR__ . '/../../qfq/Constants.php');

Carsten  Rose's avatar
Carsten Rose committed
14
15
const SUBSTITUTE = '#%SUB%#';

Carsten  Rose's avatar
Carsten Rose committed
16
17
/**
 * Class OnArray
18
 * @package qfq
Carsten  Rose's avatar
Carsten Rose committed
19
 */
20
21
22
class OnArray {

    /**
23
24
25
26
27
28
     * Builds a string from an assoc array.
     *
     * key/value are combined with $keyValueGlue.
     * values are enclosed by $encloseValue.
     * rows combined with $rowGlue
     *
29
30
31
     * @param array $dataArray
     * @param string $keyValueGlue
     * @param string $rowGlue
32
     * @param string $encloseValue  - char (or string) to enclose the value with.
33
34
     * @return string
     */
35
    public static function toString(array $dataArray, $keyValueGlue = '=', $rowGlue = '&', $encloseValue = '') {
36
37
38
        $dataString = '';

        foreach ($dataArray as $key => $value) {
39
            if (is_array($value)) {
40
41
42
                $value = self::toString($value);
            }

43
            $dataString .= $key . $keyValueGlue . $encloseValue . $value . $encloseValue . $rowGlue;
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
        }

        $glueLength = strlen($rowGlue);
        return substr($dataString, 0, strlen($dataString) - $glueLength);
    }

    /**
     * Sort array by array keys
     *
     * @param array $a
     */
    public static function sortKey(array &$a) {

        $result = array();

        $keys = array_keys($a);
        sort($keys);
        foreach ($keys as $key) {
            $result[$key] = $a[$key];
        }
        $a = $result;

        return;
    }
68
69
70
71
72
73
74
75
76

    /**
     * Trim all elemements in an array.
     * The array has to be a 1-dimensional array.
     *
     * @param array $arr
     * @param string $character_mask
     * @return array
     */
77
    public static function trimArray(array $arr, $character_mask = " \t\n\r\0\x0B") {
78
79
80
81
82
        foreach ($arr as $key => $item) {
            $arr[$key] = trim($item, $character_mask);
        }
        return $arr;
    }
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101

    /**
     * Iterates over all records and return those with $row[$column]==$value
     *
     * @param array $dataArray
     * @param $column
     * @param $value
     * @return array
     */
    public static function filter(array $dataArray, $column, $value) {
        $result = array();

        foreach ($dataArray as $row) {
            if (isset($row[$column]) && $row[$column] == $value) {
                $result[] = $row;
            }
        }
        return $result;
    }
102

103
104
105
106
107
108
109
110
111
112
113
114
    /**
     * Converts a onedimensional array by using htmlentities on all elements
     *
     * @param array $arr
     * @return array
     */
    public static function htmlentitiesOnArray(array $arr) {
        foreach ($arr as $key => $value) {
            $arr[$key] = htmlentities($arr[$key], ENT_QUOTES);
        }
        return $arr;
    }
Carsten  Rose's avatar
Carsten Rose committed
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134

    /**
     * Split Array around $str to $arr around $delimiter. Escaped $delimiter will be preserved.
     *
     * @param string $delimiter
     * @param string $str
     * @return array
     * @throws UserReportException
     */
    public static function explodeWithoutEscaped($delimiter, $str) {

        if (strpos($str, SUBSTITUTE) !== false) {
            throw  new UserReportException ("Can't replace token by SUBSTITUTE, cause SUBSTITUTE already exist", ERROR_SUBSTITUTE_FOUND);
        }

        $encodedStr = str_replace('\\' . $delimiter, SUBSTITUTE, $str);

        $arr = explode($delimiter, $encodedStr);

        for ($ii = 0; $ii < count($arr); $ii++) {
135
136
//            $arr[$ii] = str_replace(SUBSTITUTE, '\\' . $delimiter, $arr[$ii]);
            $arr[$ii] = str_replace(SUBSTITUTE, $delimiter, $arr[$ii]);
Carsten  Rose's avatar
Carsten Rose committed
137
138
139
140
141
        }

        return $arr;
    }

142
    /**
Carsten  Rose's avatar
Carsten Rose committed
143
     * Iterates over $arr and removes all empty (='') elements. Preserves keys
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
     *
     * @param array $arr
     * @return array
     */
    public static function removeEmptyElementsFromArray(array $arr) {

        $new = array();

//        for($ii=0; $ii<count($arr); $ii++) {
//            if($arr[$ii]!='') {
//                $new[]=$arr[$ii];
//            }
//        }

        $ii = 0;
        foreach ($arr as $key => $value) {
            if ($value != '') {
                if (is_int($key)) {
                    $key = $ii;
                    $ii++;
                }
                $new[$key] = $value;
            }
        }

        return $new;
    }
171

Carsten  Rose's avatar
Carsten Rose committed
172
173
174
175
176
    /**
     * Convert all values of an array to lowercase.
     * @param array $arr
     * @return array
     */
177
178
179
180
181
182
183
184
    public static function arrayValueToLower(array $arr) {
        $new = array();

        foreach ($arr as $key => $value) {
            $new[$key] = strtolower($value);
        }
        return $new;
    }
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230

    /**
     * Search in array $dest for all $keyNames if they exist. If not, check if they exist in $src. If yes, copy.
     *
     * @param array $src
     * @param array $dest
     * @param array $keyNames
     * @return array $dest filled with new values
     */
    public static function copyArrayItemsIfNotAlreadyExist(array $src, array $dest, array $keyNames) {

        foreach ($keyNames as $key) {

            if (!isset($dest[$key])) {
                if (isset($src[$key])) {
                    $dest[$key] = $src[$key];
                }
            }
        }

        return $dest;
    }

    /**
     * Copies all items whose $keyNames are listed to $new and return $new
     *
     * @param array $src
     * @param array $keyNames
     * @param bool $createMissing
     * @return array
     */
    public static function getArrayItems(array $src, array $keyNames, $createMissing = false) {
        $new = array();

        // Extract necessary elements
        foreach ($keyNames as $key) {
            if (isset($src[$key])) {
                $new[$key] = $src[$key];
            } elseif ($createMissing == true) {
                $new[$key] = '';
            }
        }

        return $new;
    }

231
}