mirror of
https://github.com/DesignPatternsPHP/DesignPatternsPHP.git
synced 2025-02-24 01:32:22 +01:00
36 lines
751 B
PHP
36 lines
751 B
PHP
<?php
|
|
|
|
namespace DesignPatterns\Behavioral\Iterator;
|
|
|
|
class BookList implements \Countable
|
|
{
|
|
private $books;
|
|
|
|
public function getBook($bookNumberToGet)
|
|
{
|
|
if (isset($this->books[$bookNumberToGet])) {
|
|
return $this->books[$bookNumberToGet];
|
|
}
|
|
}
|
|
|
|
public function addBook(Book $book)
|
|
{
|
|
$this->books[] = $book;
|
|
}
|
|
|
|
public function removeBook(Book $bookToRemove)
|
|
{
|
|
foreach ($this->books as $key => $book) {
|
|
/** @var Book $book */
|
|
if ($book->getAuthorAndTitle() === $bookToRemove->getAuthorAndTitle()) {
|
|
unset($this->books[$key]);
|
|
}
|
|
}
|
|
}
|
|
|
|
public function count()
|
|
{
|
|
return count($this->books);
|
|
}
|
|
}
|