Behavioral\Iterator restructured

This commit is contained in:
Antonio Spinelli
2014-03-24 10:40:08 -03:00
parent 3e515daa0f
commit d225a43de7
8 changed files with 240 additions and 166 deletions

View File

@@ -0,0 +1,42 @@
<?php
namespace DesignPatterns\Behavioral\Iterator;
class BookList implements \Countable
{
private $books;
public function getBook($bookNumberToGet)
{
if ((int)$bookNumberToGet <= $this->count()) {
return $this->books[$bookNumberToGet];
}
return NULL;
}
public function addBook(Book $book)
{
$this->books[] = $book;
return $this->count();
}
public function removeBook(Book $bookToRemove)
{
foreach ($this as $key => $book) {
/** @var Book $book */
if ($book->getAuthorAndTitle() === $bookToRemove->getAuthorAndTitle()) {
unset($this->books[$key]);
}
}
return $this->count();
}
public function count()
{
return count($this->books);
}
}