AbstractFilter   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 5
eloc 10
c 2
b 0
f 0
dl 0
loc 34
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getArrayElementValueFromKey() 0 5 2
A getValueFromKeysArray() 0 13 3
1
<?php
2
/**
3
 * This file is part of the ArrayQuery package.
4
 *
5
 * (c) Mauro Cassani<https://github.com/mauretto78>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace ArrayQuery\Filters;
12
13
use ArrayQuery\Constants;
14
use ArrayQuery\Exceptions\NotValidKeyElementInArrayException;
15
use ArrayQuery\Helpers\ArrayHelper;
16
17
abstract class AbstractFilter
18
{
19
    /**
20
     * @param $key
21
     * @param $arrayElement
22
     * @return mixed
23
     */
24
    protected static function getArrayElementValueFromKey($key, $arrayElement)
25
    {
26
        return self::getValueFromKeysArray(
27
            explode(Constants::ARRAY_SEPARATOR, $key),
28
            (is_object($arrayElement)) ? ArrayHelper::convertObjectToArray($arrayElement) : $arrayElement
29
        );
30
    }
31
32
    /**
33
     * @param $keysArray
34
     * @param $arrayElement
35
     * @return mixed
36
     * @throws NotValidKeyElementInArrayException
37
     */
38
    private static function getValueFromKeysArray($keysArray, $arrayElement)
39
    {
40
        if (count($keysArray)>1) {
41
            $key = array_shift($keysArray);
42
43
            return self::getValueFromKeysArray($keysArray, ArrayHelper::convertToPlainArray($arrayElement[$key]));
44
        }
45
46
        if (!isset($arrayElement[$keysArray[0]])) {
47
            throw new NotValidKeyElementInArrayException($keysArray[0].' is not a valid key in the array.');
48
        }
49
50
        return $arrayElement[$keysArray[0]];
51
    }
52
}
53