Completed
Push — master ( 4b07de...908eb1 )
by smiley
02:55
created

SearchableArray::isset()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 5
nc 3
nop 1
1
<?php
2
/**
3
 * Trait SearchableArray
4
 *
5
 * @filesource   SearchableArray.php.php
6
 * @created      04.12.2017
7
 * @package      chillerlan\Traits\ArrayHelpers
8
 * @author       Smiley <[email protected]>
9
 * @copyright    2017 Smiley
10
 * @license      MIT
11
 */
12
13
namespace chillerlan\Traits\ArrayHelpers;
14
15
use ArrayIterator, ArrayObject, RecursiveArrayIterator, RecursiveIteratorIterator, Traversable;
16
17
trait SearchableArray{
18
	use DotArray;
19
20
	/**
21
	 * @var \IteratorIterator|\RecursiveIteratorIterator
22
	 */
23
	protected $iterator;
24
25
	/**
26
	 * ExtendedIteratorTrait constructor.
27
	 *
28
	 * @param array|object|\Traversable|\ArrayIterator|\ArrayObject|null $array
29
	 */
30
	public function __construct($array = null){
31
32
		if(($array instanceof ArrayObject) || ($array instanceof ArrayIterator)){
33
			$this->array = $array->getArrayCopy();
34
		}
35
		elseif($array instanceof Traversable){
36
			$this->array = iterator_to_array($array);
37
		}
38
		// yields unexpected results with DotArray
39
		elseif(gettype($array) === 'object'){
40
			$this->array = get_object_vars($array);
41
		}
42
		elseif(is_array($array)){
43
			$this->array = $array;
44
		}
45
46
		$this->iterator = new RecursiveIteratorIterator(
47
			new RecursiveArrayIterator($this->array),
48
			RecursiveIteratorIterator::SELF_FIRST
49
		);
50
	}
51
52
	/**
53
	 * @param string $dotKey
54
	 *
55
	 * @return mixed
56
	 */
57
	public function searchByKey(string $dotKey){
58
59
		foreach($this->iterator as $v){
60
61
			if($this->getPath() === $dotKey){
62
				return $v;
63
			}
64
65
		}
66
67
		return null;
68
	}
69
70
	public function searchByValue($value):array {
71
72
		$matches = [];
73
74
		foreach($this->iterator as $v){
75
76
			if($v === $value){
77
				$matches[$this->getPath()] = $value;
78
			}
79
80
		}
81
82
		return $matches;
83
	}
84
85
	/**
86
	 * @param string $dotKey
87
	 *
88
	 * @return bool
89
	 */
90
	public function isset(string $dotKey):bool{
91
92
		foreach($this->iterator as $v){
93
94
			if($this->getPath() === $dotKey){
95
				return true;
96
			}
97
98
		}
99
100
		return false;
101
	}
102
103
	/**
104
	 * @return string
105
	 */
106
	private function getPath():string{
107
		return implode('.', array_map(function(int $depth):string {
108
			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

108
			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...
109
		}, 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

109
		}, 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...
110
	}
111
112
}
113