Completed
Push — master ( 7942ad...1c8947 )
by WEBEWEB
01:50
created

ArrayUtility::get()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 2
eloc 2
nc 2
nop 3
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\Argument;
13
14
/**
15
 * Array utility.
16
 *
17
 * @author webeweb <https://github.com/webeweb/>
18
 * @package WBW\Library\Core\Utility\Argument
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