Completed
Push — master ( 54383e...ea3df8 )
by WEBEWEB
01:41
created

ArrayUtility::set()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
cc 3
eloc 6
nc 3
nop 4
1
<?php
2
3
/**
4
 * This file is part of the core-library package.
5
 *
6
 * (c) 2018 WEBEWEB
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace WBW\Library\Core\Utility;
13
14
/**
15
 * Array utility.
16
 *
17
 * @author webeweb <https://github.com/webeweb/>
18
 * @package WBW\Library\Core\Utility
19
 * @final
20
 */
21
final class ArrayUtility {
22
23
    /**
24
     * Get a value.
25
     *
26
     * @param array $array The array.
27
     * @param mixed $key The key.
28
     * @param mixed $default The default value.
29
     * @return mixed Returns the value in case of success, $default otherwise.
30
     */
31
    public static function get(array $array, $key, $default = null) {
32
        return true === array_key_exists($key, $array) ? $array[$key] : $default;
33
    }
34
35
    /**
36
     * Set a value.
37
     *
38
     * @param array $array The array.
39
     * @param string $key The key.
40
     * @param mixed $value The value.
41
     * @param array $tests The tests.
42
     */
43
    public static function set(array &$array, $key, $value, array $tests = []) {
44
        foreach ($tests as $current) {
45
            if ($current !== $value) {
46
                continue;
47
            }
48
            return;
49
        }
50
        $array[$key] = $value;
51
    }
52
53
}
54