chillerlan /
php-traits
| 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 | * SearchableArray 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(); // @codeCoverageIgnore |
||||
| 34 | } |
||||
| 35 | elseif($array instanceof Traversable){ |
||||
| 36 | $this->array = \iterator_to_array($array); // @codeCoverageIgnore |
||||
| 37 | } |
||||
| 38 | // yields unexpected results with DotArray |
||||
| 39 | elseif(\gettype($array) === 'object'){ |
||||
| 40 | $this->array = \get_object_vars($array); // @codeCoverageIgnore |
||||
| 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 | /** |
||||
| 71 | * @param mixed $value |
||||
| 72 | * |
||||
| 73 | * @return array |
||||
| 74 | */ |
||||
| 75 | public function searchByValue($value):array{ |
||||
| 76 | |||||
| 77 | $matches = []; |
||||
| 78 | |||||
| 79 | foreach($this->iterator as $v){ |
||||
| 80 | |||||
| 81 | if($v === $value){ |
||||
| 82 | $matches[$this->getPath()] = $value; |
||||
| 83 | } |
||||
| 84 | |||||
| 85 | } |
||||
| 86 | |||||
| 87 | return $matches; |
||||
| 88 | } |
||||
| 89 | |||||
| 90 | /** |
||||
| 91 | * @param string $dotKey |
||||
| 92 | * |
||||
| 93 | * @return bool |
||||
| 94 | */ |
||||
| 95 | public function isset(string $dotKey):bool{ |
||||
| 96 | |||||
| 97 | foreach($this->iterator as $v){ |
||||
| 98 | |||||
| 99 | if($this->getPath() === $dotKey){ |
||||
| 100 | return true; |
||||
| 101 | } |
||||
| 102 | |||||
| 103 | } |
||||
| 104 | |||||
| 105 | return false; |
||||
| 106 | } |
||||
| 107 | |||||
| 108 | /** |
||||
| 109 | * @return string |
||||
| 110 | */ |
||||
| 111 | private function getPath():string{ |
||||
| 112 | return \implode('.', \array_map(function(int $depth):string { |
||||
| 113 | return $this->iterator->getSubIterator($depth)->key(); |
||||
|
0 ignored issues
–
show
|
|||||
| 114 | }, \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
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...
|
|||||
| 115 | } |
||||
| 116 | |||||
| 117 | } |
||||
| 118 |
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.