Passed
Push — master ( eb2ccf...f34259 )
by Eliseev
01:10
created

Arrays::getValueCheckParam()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 1
nc 4
nop 2
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace cse\helpers;
6
7
/**
8
 * Class Arrays
9
 *
10
 * @package cse\helpers
11
 */
12
class Arrays
13
{
14
    /**
15
     * Get array value by key
16
     *
17
     * @param array $data
18
     * @param $key
19
     * @param $default
20
     *
21
     * @return mixed|null
22
     */
23
    public static function get(array $data, $key, $default = null)
24
    {
25
        return array_key_exists($key, $data) ? $data[$key] : $default;
26
    }
27
28
    /**
29
     * Pull array key
30
     *
31
     * @param array $data
32
     * @param $key
33
     * @param $default
34
     *
35
     * @return mixed|null
36
     */
37
    public static function pullKey(array &$data, $key, $default = null)
38
    {
39
        $result = self::get($data, $key, $default);
40
        unset($data[$key]);
41
42
        return $result;
43
    }
44
45
    /**
46
     * Object to array
47
     *
48
     * @param $data
49
     *
50
     * @return array
51
     */
52
    public static function objectToArray($data): array
53
    {
54
        return json_decode(json_encode($data), true);
55
    }
56
57
    /**
58
     * To string key and value
59
     *
60
     * @param array $data
61
     * @param string $preFix
62
     * @param string $postFix
63
     *
64
     * @return string
65
     */
66
    public static function toString(array $data, string $preFix = ':', $postFix = ';'): string
67
    {
68
        $string = '';
69
70
        foreach ($data as $key => $option) {
71
            $string .= $key . $preFix . $option . $postFix;
72
        }
73
74
        return $string;
75
    }
76
77
    /**
78
     * Array to html
79
     *
80
     * @param array $data
81
     *
82
     * @return string
83
     */
84
    public static function toTags(array $data): string
85
    {
86
        $string = '';
87
88
        foreach ($data as $key => $item) {
89
            $info = [];
90
            if (is_int($key)) {
91
                $info['tag'] = $item;
92
            } elseif (is_array($item)) {
93
                $info = self::replaceTagInfo($item);
94
                $info['tag'] = $key;
95
            } else {
96
                $info['content'] = $item;
97
                $info['tag'] = $key;
98
            }
99
100
            $string .= '<' . $info['tag'] . (empty($info['attr']) ? '' : $info['attr']) . (empty($info['content']) ? ' />' : '>' . $info['content'] . '</' . $info['tag'] . '>');
101
        }
102
103
        return $string;
104
    }
105
106
    /**
107
     * Building Maps
108
     *
109
     * @param $data
110
     * @param $keyGroup
111
     * @param $keyValue
112
     *
113
     * @return array
114
     */
115
    public static function map(array $data, $keyGroup, $keyValue = null): array
116
    {
117
        $result = [];
118
119
        foreach ($data as $item) {
120
            if (array_key_exists($keyGroup, $item)) {
121
                $result[$item[$keyGroup]] = self::getValueCheckParam($item, $keyValue);
122
            }
123
        }
124
125
        return $result;
126
    }
127
128
    /**
129
     * Group array
130
     *
131
     * @param array $data
132
     * @param $keyGroup
133
     * @param $keyValue
134
     *
135
     * @return array
136
     */
137
    public static function group(array $data, $keyGroup, $keyValue = null): array
138
    {
139
        $result = [];
140
141
        foreach ($data as $item) {
142
            if (array_key_exists($keyGroup, $item)) {
143
                $key = $item[$keyGroup];
144
145
                if (empty($result[$key])) $result[$key] = [];
146
147
                $result[$key][] = self::getValueCheckParam($item, $keyValue);
148
            }
149
        }
150
151
        return $result;
152
    }
153
    /**
154
     * Index group array
155
     *
156
     * @param array $data
157
     * @param $keyGroup
158
     * @param $keyValue
159
     *
160
     * @return array
161
     */
162
    public static function index(array $data, $keyGroup, $keyValue = null): array
163
    {
164
        $result = [];
165
166
        foreach ($data as $item) {
167
            if (array_key_exists($keyGroup, $item)) {
168
                $result[$item[$keyGroup]][] = self::getValueCheckParam($item, $keyValue);
169
            }
170
        }
171
172
        return $result;
173
    }
174
175
    /**
176
     * Append not empty array data
177
     *
178
     * @param array $first
179
     * @param array $second
180
     *
181
     * @return array
182
     */
183
    public static function appendNotEmptyData(array $first, array $second): array
184
    {
185
        $result = null;
186
187
        switch (true) {
188
            case empty($first) && empty($second):
189
                $result = [];
190
                break;
191
            case empty($second):
192
                $result = $first;
193
                break;
194
            case empty($first):
195
                $result = self::removeEmpty($second);
196
                break;
197
            default:
198
                $result = self::appendNotEmpty($first, $second);
199
                break;
200
        }
201
202
        return $result;
203
    }
204
205
    /**
206
     * Replace empty array data to not empty array data
207
     *
208
     * @param array $first
209
     * @param array $second
210
     *
211
     * @return array
212
     */
213
    public static function replaceEmptyNotEmptyData(array $first = [], array $second = []): array
214
    {
215
        if (!empty($first) && !empty($second)) {
216
            foreach ($first as $key => &$value) {
217
                if (empty($value) && array_key_exists($key, $second)) $value = $second[$key];
218
            }
219
        }
220
        return $first;
221
    }
222
223
    /**
224
     * Replace not empty array data
225
     *
226
     * @param array $first
227
     * @param array $second
228
     *
229
     * @return array
230
     */
231
    public static function replaceNotEmptyData(array $first = [], array $second = []): array
232
    {
233
        if (!empty($first) && !empty($second)) {
234
            foreach ($first as $key => &$value) {
235
                if (!empty($second[$key])) $value = $second[$key];
236
            }
237
        }
238
        return $first;
239
    }
240
241
    /**
242
     * Merge not empty array data
243
     *
244
     * @param array $first
245
     * @param array $second
246
     *
247
     * @return array
248
     */
249
    public static function mergeNotEmptyData(array $first = [], array $second = []): array
250
    {
251
        if (!empty($first) && !empty($second)) {
252
            foreach ($second as $key => $value) {
253
                if (!empty($value)) $first[$key] = $value;
254
            }
255
        }
256
        return $first;
257
    }
258
259
    /**
260
     * Convert empty to null
261
     *
262
     * @param array $data
263
     * @param bool $recursive
264
     *
265
     * @return array
266
     */
267
    public static function emptyToNull(array $data, bool $recursive = false): array
268
    {
269
        foreach ($data as $key => &$value) {
270
            if (empty($value)) {
271
                $value = null;
272
            } elseif ($recursive && is_array($value)) {
273
                $value = self::emptyToNull($value, $recursive);
274
            }
275
        }
276
        return $data;
277
    }
278
279
    /**
280
     * Remove empty data to array
281
     *
282
     * @param array $data
283
     * @param bool $recursive
284
     *
285
     * @return array
286
     */
287
    public static function removeEmpty(array $data, bool $recursive = false): array
288
    {
289
        foreach ($data as $key => &$value) {
290
            if (empty($value)) {
291
                unset($data[$key]);
292
            } elseif ($recursive && is_array($value)) {
293
                $value = self::removeEmpty($value, $recursive);
294
            }
295
        }
296
        return $data;
297
    }
298
299
    /**
300
     * Remove null data to array
301
     *
302
     * @param array $data
303
     * @param bool $recursive
304
     *
305
     * @return array
306
     */
307
    public static function removeNull(array $data, bool $recursive = false): array
308
    {
309
        foreach ($data as $key => &$value) {
310
            if (is_null($value)) {
311
                unset($data[$key]);
312
            } elseif ($recursive && is_array($value)) {
313
                $value = self::removeNull($value, $recursive);
314
            }
315
        }
316
        return $data;
317
    }
318
319
    /**
320
     * Trim value to array
321
     *
322
     * @param array $data
323
     * @param bool $recursive
324
     *
325
     * @return array
326
     */
327
    public static function trim(array $data, bool $recursive = true): array
328
    {
329
        foreach ($data as $key => &$value) {
330
            if (is_string($value)) {
331
                $value = trim($value);
332
            } elseif ($recursive && is_array($value)) {
333
                $value = self::trim($value);
334
            }
335
        }
336
        return $data;
337
    }
338
339
    /**
340
     * Append not empty
341
     *
342
     * @param array $first
343
     * @param array $second
344
     *
345
     * @return array
346
     */
347
    protected static function appendNotEmpty(array $first, array $second): array
348
    {
349
        foreach ($second as $key => $value) {
350
            if (!empty($value) && !array_key_exists($key, $first)) $first[$key] = $value;
351
        }
352
        return $first;
353
    }
354
355
    /**
356
     * Replace tag info
357
     *
358
     * @param array $info
359
     *
360
     * @return array
361
     */
362
    protected static function replaceTagInfo(array $info): array
363
    {
364
        $result = ['attr' => '', 'content' => ''];
365
366
        foreach ($info as $key => $item) {
367
            if (is_int($key) && is_array($item)) {
368
                $result['attr'] .= ' ' . trim(self::toString($item, '="', '" '));
369
            } elseif (is_int($key)) {
370
                $result['content'] = $item;
371
            } else {
372
                $result['attr'] .= ' ' . $key .'="' . $item . '"';
373
            }
374
        }
375
376
        return $result;
377
    }
378
379
    /**
380
     * Get value check param
381
     *
382
     * @param array $data
383
     * @param $keyValue
384
     *
385
     * @return mixed
386
     */
387
    protected static function getValueCheckParam(array $data, $keyValue)
388
    {
389
        return is_null($keyValue) ? $data : (array_key_exists($keyValue, $data) ? $data[$keyValue] : null);
390
    }
391
}
392