Skip to content

Commit d5b8a56

Browse files
author
Dominik Liebler
committed
DesignPatternsPHP#232 removed Iterator classes
1 parent 8887a56 commit d5b8a56

File tree

8 files changed

+553
-353
lines changed

8 files changed

+553
-353
lines changed

Behavioral/Iterator/Book.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,33 @@
44

55
class Book
66
{
7+
/**
8+
* @var string
9+
*/
710
private $author;
811

12+
/**
13+
* @var string
14+
*/
915
private $title;
1016

11-
public function __construct($title, $author)
17+
public function __construct(string $title, string $author)
1218
{
1319
$this->author = $author;
1420
$this->title = $title;
1521
}
1622

17-
public function getAuthor()
23+
public function getAuthor(): string
1824
{
1925
return $this->author;
2026
}
2127

22-
public function getTitle()
28+
public function getTitle(): string
2329
{
2430
return $this->title;
2531
}
2632

27-
public function getAuthorAndTitle()
33+
public function getAuthorAndTitle(): string
2834
{
2935
return $this->getTitle().' by '.$this->getAuthor();
3036
}

Behavioral/Iterator/BookList.php

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,17 @@
22

33
namespace DesignPatterns\Behavioral\Iterator;
44

5-
class BookList implements \Countable
5+
class BookList implements \Countable, \Iterator
66
{
7+
/**
8+
* @var Book[]
9+
*/
710
private $books;
811

9-
public function getBook($bookNumberToGet)
10-
{
11-
if (isset($this->books[$bookNumberToGet])) {
12-
return $this->books[$bookNumberToGet];
13-
}
14-
15-
return;
16-
}
12+
/**
13+
* @var int
14+
*/
15+
private $currentIndex = 0;
1716

1817
public function addBook(Book $book)
1918
{
@@ -30,8 +29,33 @@ public function removeBook(Book $bookToRemove)
3029
}
3130
}
3231

33-
public function count()
32+
public function count(): int
3433
{
3534
return count($this->books);
3635
}
36+
37+
public function current(): Book
38+
{
39+
return $this->books[array_keys($this->books)[$this->currentIndex]];
40+
}
41+
42+
public function key(): int
43+
{
44+
return array_keys($this->books)[$this->currentIndex];
45+
}
46+
47+
public function next()
48+
{
49+
$this->currentIndex++;
50+
}
51+
52+
public function rewind()
53+
{
54+
$this->currentIndex = 0;
55+
}
56+
57+
public function valid(): bool
58+
{
59+
return isset(array_keys($this->books)[$this->currentIndex]);
60+
}
3761
}

Behavioral/Iterator/BookListIterator.php

Lines changed: 0 additions & 86 deletions
This file was deleted.

Behavioral/Iterator/BookListReverseIterator.php

Lines changed: 0 additions & 87 deletions
This file was deleted.

Behavioral/Iterator/README.rst

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@
44
Purpose
55
-------
66

7-
To make an object iterable and to make it appear like a collection of
8-
objects.
7+
To make an object iterable and to make it appear like a collection of objects.
98

109
Examples
1110
--------
@@ -45,18 +44,6 @@ BookList.php
4544
:language: php
4645
:linenos:
4746

48-
BookListIterator.php
49-
50-
.. literalinclude:: BookListIterator.php
51-
:language: php
52-
:linenos:
53-
54-
BookListReverseIterator.php
55-
56-
.. literalinclude:: BookListReverseIterator.php
57-
:language: php
58-
:linenos:
59-
6047
Test
6148
----
6249

@@ -67,4 +54,4 @@ Tests/IteratorTest.php
6754
:linenos:
6855

6956
.. _`GitHub`: https://github.com/domnikl/DesignPatternsPHP/tree/master/Behavioral/Iterator
70-
.. __: http://en.wikipedia.org/wiki/Iterator_pattern
57+
.. __: http://en.wikipedia.org/wiki/Iterator_pattern

0 commit comments

Comments
 (0)