class-blockswithmetadataentityreader.php
6 days ago
class-databasecontententityreader.php
6 days ago
class-databaserowsentityreader.php
6 days ago
class-entityreaderiterator.php
6 days ago
class-epubentityreader.php
6 days ago
class-filesystementityreader.php
6 days ago
class-htmlentityreader.php
6 days ago
class-wxrentityreader.php
6 days ago
interface-entity-reader.php
6 days ago
class-entityreaderiterator.php
96 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WordPress\DataLiberation\EntityReader; |
| 4 | |
| 5 | use Iterator; |
| 6 | use ReturnTypeWillChange; |
| 7 | use WordPress\DataLiberation\DataLiberationException; |
| 8 | |
| 9 | /** |
| 10 | * An iterator that reads entities from a WP_Entity_Reader. |
| 11 | */ |
| 12 | class EntityReaderIterator implements Iterator { |
| 13 | |
| 14 | /** |
| 15 | * @var EntityReader |
| 16 | */ |
| 17 | private $entity_reader; |
| 18 | private $is_initialized = false; |
| 19 | private $key = 0; |
| 20 | |
| 21 | public function __construct( EntityReader $entity_reader ) { |
| 22 | $this->entity_reader = $entity_reader; |
| 23 | } |
| 24 | |
| 25 | public function get_entity_reader() { |
| 26 | return $this->entity_reader; |
| 27 | } |
| 28 | |
| 29 | #[ReturnTypeWillChange] |
| 30 | public function current() { |
| 31 | $this->ensure_initialized(); |
| 32 | |
| 33 | return $this->entity_reader->get_entity(); |
| 34 | } |
| 35 | |
| 36 | #[ReturnTypeWillChange] |
| 37 | public function next() { |
| 38 | $this->ensure_initialized(); |
| 39 | $this->advance_to_next_entity(); |
| 40 | } |
| 41 | |
| 42 | #[ReturnTypeWillChange] |
| 43 | public function key() { |
| 44 | $this->ensure_initialized(); |
| 45 | |
| 46 | return $this->key; |
| 47 | } |
| 48 | |
| 49 | #[ReturnTypeWillChange] |
| 50 | public function valid() { |
| 51 | $this->ensure_initialized(); |
| 52 | if ( $this->entity_reader->is_finished() ) { |
| 53 | return false; |
| 54 | } |
| 55 | // @TODO: Remove these checks once we figure out why. |
| 56 | // WXREntityReader says next_entity() succeeds. |
| 57 | // one time once the data stream is exhausted. |
| 58 | $entity = $this->entity_reader->get_entity(); |
| 59 | if ( ! $entity ) { |
| 60 | return false; |
| 61 | } |
| 62 | if ( ! $entity->get_type() ) { |
| 63 | return false; |
| 64 | } |
| 65 | |
| 66 | return true; |
| 67 | } |
| 68 | |
| 69 | #[ReturnTypeWillChange] |
| 70 | public function rewind() { |
| 71 | // rewind is not supported except for the first rewind call that initializes the iterator. |
| 72 | if ( $this->is_initialized ) { |
| 73 | throw new DataLiberationException( 'EntityReaderIterator does not support rewinding.' ); |
| 74 | } |
| 75 | $this->is_initialized = true; |
| 76 | $this->advance_to_next_entity(); |
| 77 | } |
| 78 | |
| 79 | private function ensure_initialized() { |
| 80 | if ( ! $this->is_initialized ) { |
| 81 | $this->is_initialized = true; |
| 82 | $this->advance_to_next_entity(); |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | private function advance_to_next_entity() { |
| 87 | if ( $this->entity_reader->next_entity() ) { |
| 88 | ++$this->key; |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | public function get_reentrancy_cursor() { |
| 93 | return $this->entity_reader->get_reentrancy_cursor(); |
| 94 | } |
| 95 | } |
| 96 |