Passed
Push — master ( e4897b...79b76a )
by smiley
01:48
created

ArraySearch::__construct()   B

Complexity

Conditions 6
Paths 5

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 10
nc 5
nop 1
dl 0
loc 16
rs 8.8571
c 0
b 0
f 0
1
<?php
2
/**
3
 * Trait ArraySearch
4
 *
5
 * @filesource   ArraySearch.php
6
 * @created      04.12.2017
7
 * @package      chillerlan\Traits
8
 * @author       Smiley <[email protected]>
9
 * @copyright    2017 Smiley
10
 * @license      MIT
11
 */
12
13
namespace chillerlan\Traits;
14
15
use ArrayIterator, ArrayObject, RecursiveArrayIterator, RecursiveIteratorIterator, Traversable;
16
17
class ArraySearch{
18
19
	/**
20
	 * @var \IteratorIterator|\RecursiveIteratorIterator
21
	 */
22
	protected $iterator;
23
24
	/**
25
	 * @var array
26
	 */
27
	protected $array;
28
29
	/**
30
	 * ExtendedIteratorTrait constructor.
31
	 *
32
	 * @param array|object|\Traversable|\ArrayIterator|\ArrayObject|null $array
33
	 */
34
	public function __construct($array = null){
35
36
		if(($array instanceof ArrayObject) || ($array instanceof ArrayIterator)){
37
			$this->array = $array->getArrayCopy();
38
		}
39
		elseif($array instanceof Traversable){
40
			$this->array = iterator_to_array($array);
41
		}
42
		elseif(gettype($array) === 'object'){
43
			$this->array = get_object_vars($array);
0 ignored issues
show
Bug introduced by
It seems like $array can also be of type array; however, parameter $object of get_object_vars() does only seem to accept object, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

43
			$this->array = get_object_vars(/** @scrutinizer ignore-type */ $array);
Loading history...
44
		}
45
		elseif(is_array($array)){
46
			$this->array = $array;
47
		}
48
		else{
49
			$this->array = [];
50
		}
51
52
	}
53
54
	/**
55
	 * @param string $dotKey
56
	 *
57
	 * @return mixed
58
	 */
59
	public function arraySearch(string $dotKey){
60
		$this->iterator = $this->getRecursiveIteratorIterator();
61
62
		foreach($this->iterator as $v){
63
64
			if($this->getPath() === $dotKey){
65
				return $v;
66
			}
67
68
		}
69
70
		return null;
71
	}
72
73
	/**
74
	 * @param string $dotKey
75
	 *
76
	 * @return bool
77
	 */
78
	public function arrayIsset(string $dotKey):bool{
79
		$this->iterator = $this->getRecursiveIteratorIterator();
80
81
		foreach($this->iterator as $v){
82
83
			if($this->getPath() === $dotKey){
84
				return true;
85
			}
86
87
		}
88
89
		return false;
90
	}
91
92
	/**
93
	 * @return \RecursiveIteratorIterator
94
	 */
95
	private function getRecursiveIteratorIterator():RecursiveIteratorIterator{
96
		return new RecursiveIteratorIterator(new RecursiveArrayIterator($this->array), RecursiveIteratorIterator::SELF_FIRST);
97
	}
98
99
	/**
100
	 * @return string
101
	 */
102
	private function getPath():string{
103
		return implode('.', array_map(function(int $depth):string {
104
			return $this->iterator->getSubIterator($depth)->key();
0 ignored issues
show
Bug introduced by
The method getSubIterator() does not exist on IteratorIterator. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

104
			return $this->iterator->/** @scrutinizer ignore-call */ getSubIterator($depth)->key();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
105
		}, range(0, $this->iterator->getDepth())));
0 ignored issues
show
Bug introduced by
The method getDepth() does not exist on IteratorIterator. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

105
		}, range(0, $this->iterator->/** @scrutinizer ignore-call */ getDepth())));

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
106
	}
107
108
}
109