mirror of
https://github.com/DesignPatternsPHP/DesignPatternsPHP.git
synced 2025-08-19 13:01:24 +02:00
Behavioral\Iterator restructured
This commit is contained in:
42
Behavioral/Iterator/BookList.php
Normal file
42
Behavioral/Iterator/BookList.php
Normal 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);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user