Completed
Push — master ( 17e079...5b9b0c )
by Nelson
03:33
created

Arrays::ensureIsArray()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 8
ccs 0
cts 5
cp 0
crap 6
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * PHP: Nelson Martell Library file
4
 *
5
 * Copyright © 2019 Nelson Martell (http://nelson6e65.github.io)
6
 *
7
 * Licensed under The MIT License (MIT)
8
 * For full copyright and license information, please see the LICENSE
9
 * Redistributions of files must retain the above copyright notice.
10
 *
11
 * @copyright 2019 Nelson Martell
12
 * @link      http://nelson6e65.github.io/php_nml/
13
 * @since     1.0.0
14
 * @license   http://www.opensource.org/licenses/mit-license.php The MIT License (MIT)
15
 * */
16
namespace NelsonMartell\Extensions;
17
18
use InvalidArgumentException;
19
20
use NelsonMartell\IComparer;
21
use NelsonMartell\StrictObject;
22
23
use function NelsonMartell\msg;
24
use function NelsonMartell\typeof;
25
26
/**
27
 * Provides extension methods to handle arrays.
28
 *
29
 * @since 1.0.0
30
 * @author Nelson Martell <[email protected]>
31
 * */
32
class Arrays implements IComparer
33
{
34
    /**
35
     * Ensures that object given is an `array`. Else, throw an exception.
36
     *
37
     * @param mixed $obj Object to validate.
38
     *
39
     * @return array Same object given.
40
     *
41
     * @throws InvalidArgumentException if object is not an `array`.
42
     */
43
    public static function ensureIsArray($obj)
44
    {
45
        if (!is_array($obj)) {
46
            $msg = msg('Provided object must to be an array; "{0}" given.', typeof($obj));
47
            throw new InvalidArgumentException($msg);
48
        }
49
50
        return $obj;
51
    }
52
53
    /**
54
     * {@inheritDoc}
55
     *
56
     * @param array $left
57
     * @param array $right
58
     *
59
     * @return int|null Returns null if one of them is not an `array`.
60
     *
61
     * @since 1.0.0  Move implementation of array comparation from `StrictObject::compare()`.
62
     */
63 14
    public static function compare($left, $right)
64
    {
65 14
        if (!is_array($left) || !is_array($right)) {
1 ignored issue
show
introduced by
The condition is_array($left) is always true.
Loading history...
introduced by
The condition is_array($right) is always true.
Loading history...
66
            return null;
67
        }
68
69 14
        $r = count($left) - count($right);
70
71 14
        if ($r === 0) {
72 8
            reset($left);
73 8
            reset($right);
74
75
            do {
76 8
                $lKey   = key($left);
77 8
                $lValue = current($left);
78 8
                $rKey   = key($right);
79 8
                $rValue = current($right);
80
81 8
                $r = StrictObject::compare((string) $lKey, (string) $rKey);
82
83 8
                if ($r === 0) {
84
                    // Recursive call to compare values
85 6
                    $r = StrictObject::compare($lValue, $rValue);
86
                }
87
88 8
                next($left);
89 8
                next($right);
90 8
            } while (key($left) !== null && key($right) !== null && $r === 0);
91
        }
92
93 14
        return $r;
94
    }
95
}
96