Skip to content

Commit

Permalink
Iterator pattern added
Browse files Browse the repository at this point in the history
  • Loading branch information
sohelamin committed Jan 25, 2018
1 parent e65d868 commit bd9b071
Showing 1 changed file with 81 additions and 0 deletions.
81 changes: 81 additions & 0 deletions Iterator/Iterator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php

class Book
{
private $title;

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

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

class BookList implements Iterator, Countable
{
private $books = [];

private $currentIndex = 0;

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

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

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

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

public function valid()
{
return isset($this->books[$this->currentIndex]);
}

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

public function addBook(Book $book)
{
$this->books[] = $book;
}

public function removeBook(Book $bookToRemove)
{
foreach ($this->books as $key => $book) {
if ($book->getTitle() === $bookToRemove->getTitle()) {
unset($this->books[$key]);
}
}

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

$bookList = new BookList();
$bookList->addBook(new Book('Design Pattern'));
$bookList->addBook(new Book('Head First Design Pattern'));
$bookList->addBook(new Book('Clean Code'));
$bookList->addBook(new Book('The Pragmatic Programmer'));

$bookList->removeBook(new Book('Design Pattern'));

foreach ($bookList as $book) {
echo $book->getTitle() . PHP_EOL;
}

0 comments on commit bd9b071

Please sign in to comment.