Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit df64d60

Browse files
author
Dominik Liebler
authored
Merge pull request DesignPatternsPHP#233 from domnikl/iterator-bug
DesignPatternsPHP#232 removed Iterator classes
2 parents 3d6db19 + 57c2e4f commit df64d60

File tree

14 files changed

+566
-380
lines changed

14 files changed

+566
-380
lines changed

.travis.yml

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,10 @@ language: php
33
sudo: false
44

55
php:
6-
- 5.3
7-
- 5.4
8-
- 5.5
9-
- 5.6
106
- 7.0
11-
- hhvm
7+
- 7.1
128

139
matrix:
14-
allow_failures:
15-
- php: hhvm
16-
- php: 7.0
1710
fast_finish: true
1811

1912
cache:

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: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,17 @@
22

33
namespace DesignPatterns\Behavioral\Iterator;
44

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

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

1617
public function addBook(Book $book)
1718
{
@@ -21,15 +22,41 @@ public function addBook(Book $book)
2122
public function removeBook(Book $bookToRemove)
2223
{
2324
foreach ($this->books as $key => $book) {
24-
/** @var Book $book */
2525
if ($book->getAuthorAndTitle() === $bookToRemove->getAuthorAndTitle()) {
2626
unset($this->books[$key]);
2727
}
2828
}
29+
30+
$this->books = array_values($this->books);
2931
}
3032

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

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)