Completed
Push — master ( a5597c...b362ee )
by smiley
01:52
created

src/ArrayHelpers/SearchableArray.php (5 issues)

Labels
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
	 * @var array
27
	 */
28
	protected $array;
29
30
	/**
31
	 * ExtendedIteratorTrait constructor.
32
	 *
33
	 * @param array|object|\Traversable|\ArrayIterator|\ArrayObject|null $array
34
	 */
35
	public function __construct($array = null){
36
37
		if(($array instanceof ArrayObject) || ($array instanceof ArrayIterator)){
38
			$this->array = $array->getArrayCopy();
0 ignored issues
show
The method getArrayCopy() does not exist on null. ( Ignorable by Annotation )

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

38
			/** @scrutinizer ignore-call */ 
39
   $this->array = $array->getArrayCopy();

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...
The method getArrayCopy() does not exist on Traversable. It seems like you code against a sub-type of Traversable such as ArrayObject or ArrayIterator or RecursiveArrayIterator. ( Ignorable by Annotation )

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

38
			/** @scrutinizer ignore-call */ 
39
   $this->array = $array->getArrayCopy();
Loading history...
39
		}
40
		elseif($array instanceof Traversable){
41
			$this->array = iterator_to_array($array);
42
		}
43
		// yields unexpected results with DotArray
44
		elseif(gettype($array) === 'object'){
45
			$this->array = get_object_vars($array);
0 ignored issues
show
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

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

105
			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...
106
		}, range(0, $this->iterator->getDepth())));
0 ignored issues
show
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

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