Completed
Push — master ( 8f5bc0...179bc9 )
by WEBEWEB
03:06
created

ArrayHelper::isArray()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
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\Argument;
13
14
use WBW\Library\Core\Exception\Argument\ArrayArgumentException;
15
16
/**
17
 * Array helper.
18
 *
19
 * @author webeweb <https://github.com/webeweb/>
20
 * @package WBW\Library\Core\Argument
21
 */
22
class ArrayHelper {
23
24
    /**
25
     * Get a value.
26
     *
27
     * @param array $array The array.
28
     * @param mixed $key The key.
29
     * @param mixed $default The default value.
30
     * @return mixed Returns the value in case of success, $default otherwise.
31
     */
32
    public static function get(array $array, $key, $default = null) {
33
        return true === array_key_exists($key, $array) ? $array[$key] : $default;
34
    }
35
36
    /**
37
     * Determines if a value is an array.
38
     *
39
     * @param mixed $value The value.
40
     * @throws ArrayArgumentException Throws an Array argument exception if the value is not of expected type.
41
     */
42
    public static function isArray($value) {
43
        if (false === is_array($value)) {
44
            throw new ArrayArgumentException($value);
45
        }
46
    }
47
48
    /**
49
     * Set a value.
50
     *
51
     * @param array $array The array.
52
     * @param string $key The key.
53
     * @param mixed $value The value.
54
     * @param array $tests The tests.
55
     */
56
    public static function set(array &$array, $key, $value, array $tests = []) {
57
        foreach ($tests as $current) {
58
            if ($current !== $value) {
59
                continue;
60
            }
61
            return;
62
        }
63
        $array[$key] = $value;
64
    }
65
66
}
67