| 1 |
<?php |
| 2 |
|
| 3 |
/* |
| 4 |
* This file is part of the JsonSchema package. |
| 5 |
* |
| 6 |
* For the full copyright and license information, please view the LICENSE |
| 7 |
* file that was distributed with this source code. |
| 8 |
*/ |
| 9 |
|
| 10 |
namespace JsonSchema\Iterator; |
| 11 |
|
| 12 |
/** |
| 13 |
* @package JsonSchema\Iterator |
| 14 |
* |
| 15 |
* @author Joost Nijhuis <[email protected]> |
| 16 |
*/ |
| 17 |
class ObjectIterator implements \Iterator, \Countable |
| 18 |
{ |
| 19 |
/** @var object */ |
| 20 |
private $object; |
| 21 |
|
| 22 |
/** @var int */ |
| 23 |
private $position = 0; |
| 24 |
|
| 25 |
/** @var array */ |
| 26 |
private $data = array(); |
| 27 |
|
| 28 |
/** @var bool */ |
| 29 |
private $initialized = false; |
| 30 |
|
| 31 |
/** |
| 32 |
* @param object $object |
| 33 |
*/ |
| 34 |
public function __construct($object) |
| 35 |
{ |
| 36 |
$this->object = $object; |
| 37 |
} |
| 38 |
|
| 39 |
/** |
| 40 |
* {@inheritdoc} |
| 41 |
*/ |
| 42 |
public function current() |
| 43 |
{ |
| 44 |
$this->initialize(); |
| 45 |
|
| 46 |
return $this->data[$this->position]; |
| 47 |
} |
| 48 |
|
| 49 |
/** |
| 50 |
* {@inheritdoc} |
| 51 |
*/ |
| 52 |
public function next() |
| 53 |
{ |
| 54 |
$this->initialize(); |
| 55 |
$this->position++; |
| 56 |
} |
| 57 |
|
| 58 |
/** |
| 59 |
* {@inheritdoc} |
| 60 |
*/ |
| 61 |
public function key() |
| 62 |
{ |
| 63 |
$this->initialize(); |
| 64 |
|
| 65 |
return $this->position; |
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* {@inheritdoc} |
| 70 |
*/ |
| 71 |
public function valid() |
| 72 |
{ |
| 73 |
$this->initialize(); |
| 74 |
|
| 75 |
return isset($this->data[$this->position]); |
| 76 |
} |
| 77 |
|
| 78 |
/** |
| 79 |
* {@inheritdoc} |
| 80 |
*/ |
| 81 |
public function rewind() |
| 82 |
{ |
| 83 |
$this->initialize(); |
| 84 |
$this->position = 0; |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* {@inheritdoc} |
| 89 |
*/ |
| 90 |
public function count() |
| 91 |
{ |
| 92 |
$this->initialize(); |
| 93 |
|
| 94 |
return count($this->data); |
| 95 |
} |
| 96 |
|
| 97 |
/** |
| 98 |
* Initializer |
| 99 |
*/ |
| 100 |
private function initialize() |
| 101 |
{ |
| 102 |
if (!$this->initialized) { |
| 103 |
$this->data = $this->buildDataFromObject($this->object); |
| 104 |
$this->initialized = true; |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
/** |
| 109 |
* @param object $object |
| 110 |
* |
| 111 |
* @return array |
| 112 |
*/ |
| 113 |
private function buildDataFromObject($object) |
| 114 |
{ |
| 115 |
$result = array(); |
| 116 |
|
| 117 |
$stack = new \SplStack(); |
| 118 |
$stack->push($object); |
| 119 |
|
| 120 |
while (!$stack->isEmpty()) { |
| 121 |
$current = $stack->pop(); |
| 122 |
if (is_object($current)) { |
| 123 |
array_push($result, $current); |
| 124 |
} |
| 125 |
|
| 126 |
foreach ($this->getDataFromItem($current) as $propertyName => $propertyValue) { |
| 127 |
if (is_object($propertyValue) || is_array($propertyValue)) { |
| 128 |
$stack->push($propertyValue); |
| 129 |
} |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
return $result; |
| 134 |
} |
| 135 |
|
| 136 |
/** |
| 137 |
* @param object|array $item |
| 138 |
* |
| 139 |
* @return array |
| 140 |
*/ |
| 141 |
private function getDataFromItem($item) |
| 142 |
{ |
| 143 |
if (!is_object($item) && !is_array($item)) { |
| 144 |
return array(); |
| 145 |
} |
| 146 |
|
| 147 |
return is_object($item) ? get_object_vars($item) : $item; |
| 148 |
} |
| 149 |
} |