ArrayHelper::validateInput()   A
last analyzed

Complexity

Conditions 5
Paths 3

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 5

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 8
cts 8
cp 1
rs 9.5222
c 0
b 0
f 0
cc 5
nc 3
nop 2
crap 5
1
<?php
2
/**
3
 * This file is part of the NeedleProject\Common package.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
namespace NeedleProject\Common\Helper;
9
10
use NeedleProject\Common\Exception\NotFoundException;
11
12
/**
13
 * Class ErrorToExceptionConverter
14
 *
15
 * @package NeedleProject\Common\Helper
16
 * @author Adrian Tilita <[email protected]>
17
 * @copyright 2017 Adrian Tilita
18
 * @license https://opensource.org/licenses/MIT MIT Licence
19
 */
20
class ArrayHelper
21
{
22
    const USAGE_EXISTS = 'hasKey';
23
    const USAGE_EXTRACT = 'getValue';
24
25
    /**
26
     * Verify if a key exist in depth
27
     *
28
     * @param array $haystack  - The array in which to search
29
     * @param array $keys - an array with the linear items treated as depth.
30
     *                      Ex: array('first_level','second_level','third_level')
31
     * @return bool
32
     * @throws \NeedleProject\Common\Exception\NotFoundException
33
     */
34 11
    public function hasKeysInDepth($haystack, $keys)
35
    {
36 11
        $this->validateInput($haystack, $keys);
37 4
        return $this->getNode($haystack, $keys, static::USAGE_EXISTS);
38
    }
39
40
    /**
41
     * Get a value in depth of an array
42
     *
43
     * @param array $haystack - The array in which to search
44
     * @param array $keys     - an array with the linear items treated as depth.
45
     *                          Ex: array('first_level','second_level','third_level')
46
     * @return mixed
47
     * @throws \NeedleProject\Common\Exception\NotFoundException
48
     */
49 10
    public function getValueFromDepth($haystack, $keys)
50
    {
51 10
        $this->validateInput($haystack, $keys);
52 3
        return $this->getNode($haystack, $keys, static::USAGE_EXTRACT);
53
    }
54
55
    /**
56
     * @param array  $haystack
57
     * @param array  $keys
58
     * @param string $searchType    Added search type to reuse the same code
59
     *                              even if it increases the complexity (for ~20 line)
60
     * @throws \NeedleProject\Common\Exception\NotFoundException
61
     * @return mixed
62
     */
63 7
    private function getNode($haystack, $keys, $searchType)
64
    {
65 7
        $depth = count($keys);
66 7
        $node = '';
67 7
        for ($i = 0; $i < $depth; $i++) {
68 7
            $needle = $keys[$i];
69 7
            $isLast = ($i + 1) === $depth;
70 7
            if ($isLast && array_key_exists($needle, $haystack)) {
71 4
                $node = $haystack[$needle];
72 4
                break;
73
            }
74 7
            if ($isLast || !isset($haystack[$needle]) || !is_array($haystack[$needle])) {
75 3
                if (static::USAGE_EXISTS === $searchType) {
76 2
                    return false;
77
                }
78 1
                throw new NotFoundException("Given path does not exists.");
79
            }
80 6
            $haystack = $haystack[$needle];
81 6
        }
82 4
        return (static::USAGE_EXISTS === $searchType) ? true : $node;
83
    }
84
85
    /**
86
     * Validate if the given input are valid array
87
     * @param array $haystack
88
     * @param array $keys
89
     */
90 21
    private function validateInput($haystack, $keys)
91
    {
92 21
        if (!is_array($haystack) || empty($haystack)) {
93 12
            throw new \InvalidArgumentException(
94
                "Provided input array is either empty or not an array."
95 12
            );
96
        }
97 9
        if (!is_array($keys) || empty($keys)) {
98 2
            throw new \InvalidArgumentException(
99
                "Provided input keys argument is either empty or not an array."
100 2
            );
101
        }
102 7
    }
103
}
104