Completed
Push — master ( 64ee51...635b22 )
by WEBEWEB
01:15
created

ArrayHelper::isObject()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
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\Helper;
13
14
use WBW\Library\Core\Argument\Exception\ArrayArgumentException;
15
16
/**
17
 * Array helper.
18
 *
19
 * @author webeweb <https://github.com/webeweb/>
20
 * @package WBW\Library\Core\Argument\Helper
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
     * @return void
41
     * @throws ArrayArgumentException Throws an Array argument exception if the value is not of expected type.
42
     */
43
    public static function isArray($value) {
44
        if (false === is_array($value)) {
45
            throw new ArrayArgumentException($value);
46
        }
47
    }
48
49
    /**
50
     * Determines if an array is an object.
51
     *
52
     * @param array $array The array.
53
     * @return bool Returns true in case of success, false otherwise.
54
     */
55
    public static function isObject(array $array) {
56
        return range(0, count($array) - 1) !== array_keys($array);
57
    }
58
59
    /**
60
     * Set a value.
61
     *
62
     * @param array $array The array.
63
     * @param string $key The key.
64
     * @param mixed $value The value.
65
     * @param array $tests The tests.
66
     * @return void
67
     */
68
    public static function set(array &$array, $key, $value, array $tests = []) {
69
        foreach ($tests as $current) {
70
            if ($current !== $value) {
71
                continue;
72
            }
73
            return;
74
        }
75
        $array[$key] = $value;
76
    }
77
}
78