Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 1 addition & 8 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,10 @@ language: php
sudo: false

php:
- 5.3
- 5.4
- 5.5
- 5.6
- 7.0
- hhvm
- 7.1

matrix:
allow_failures:
- php: hhvm
- php: 7.0
fast_finish: true

cache:
Expand Down
14 changes: 10 additions & 4 deletions Behavioral/Iterator/Book.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,33 @@

class Book
{
/**
* @var string
*/
private $author;

/**
* @var string
*/
private $title;

public function __construct($title, $author)
public function __construct(string $title, string $author)
{
$this->author = $author;
$this->title = $title;
}

public function getAuthor()
public function getAuthor(): string
{
return $this->author;
}

public function getTitle()
public function getTitle(): string
{
return $this->title;
}

public function getAuthorAndTitle()
public function getAuthorAndTitle(): string
{
return $this->getTitle().' by '.$this->getAuthor();
}
Expand Down
47 changes: 37 additions & 10 deletions Behavioral/Iterator/BookList.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@

namespace DesignPatterns\Behavioral\Iterator;

class BookList implements \Countable
class BookList implements \Countable, \Iterator
{
private $books;
/**
* @var Book[]
*/
private $books = [];

public function getBook($bookNumberToGet)
{
if (isset($this->books[$bookNumberToGet])) {
return $this->books[$bookNumberToGet];
}
}
/**
* @var int
*/
private $currentIndex = 0;

public function addBook(Book $book)
{
Expand All @@ -21,15 +22,41 @@ public function addBook(Book $book)
public function removeBook(Book $bookToRemove)
{
foreach ($this->books as $key => $book) {
/** @var Book $book */
if ($book->getAuthorAndTitle() === $bookToRemove->getAuthorAndTitle()) {
unset($this->books[$key]);
}
}

$this->books = array_values($this->books);
}

public function count()
public function count(): int
{
return count($this->books);
}

public function current(): Book
{
return $this->books[$this->currentIndex];
}

public function key(): int
{
return $this->currentIndex;
}

public function next()
{
$this->currentIndex++;
}

public function rewind()
{
$this->currentIndex = 0;
}

public function valid(): bool
{
return isset($this->books[$this->currentIndex]);
}
}
86 changes: 0 additions & 86 deletions Behavioral/Iterator/BookListIterator.php

This file was deleted.

87 changes: 0 additions & 87 deletions Behavioral/Iterator/BookListReverseIterator.php

This file was deleted.

17 changes: 2 additions & 15 deletions Behavioral/Iterator/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
Purpose
-------

To make an object iterable and to make it appear like a collection of
objects.
To make an object iterable and to make it appear like a collection of objects.

Examples
--------
Expand Down Expand Up @@ -45,18 +44,6 @@ BookList.php
:language: php
:linenos:

BookListIterator.php

.. literalinclude:: BookListIterator.php
:language: php
:linenos:

BookListReverseIterator.php

.. literalinclude:: BookListReverseIterator.php
:language: php
:linenos:

Test
----

Expand All @@ -67,4 +54,4 @@ Tests/IteratorTest.php
:linenos:

.. _`GitHub`: https://github.com/domnikl/DesignPatternsPHP/tree/master/Behavioral/Iterator
.. __: http://en.wikipedia.org/wiki/Iterator_pattern
.. __: http://en.wikipedia.org/wiki/Iterator_pattern
Loading