Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d225a43

Browse files
Behavioral\Iterator restructured
1 parent 3e515da commit d225a43

File tree

8 files changed

+240
-166
lines changed

8 files changed

+240
-166
lines changed

Behavioral/Iterator/Book.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
namespace DesignPatterns\Behavioral\Iterator;
4+
5+
class Book
6+
{
7+
8+
private $author;
9+
10+
private $title;
11+
12+
public function __construct($title, $author)
13+
{
14+
$this->author = $author;
15+
$this->title = $title;
16+
}
17+
18+
public function getAuthor()
19+
{
20+
return $this->author;
21+
}
22+
23+
public function getTitle()
24+
{
25+
return $this->title;
26+
}
27+
28+
public function getAuthorAndTitle()
29+
{
30+
return $this->getTitle() . ' by ' . $this->getAuthor();
31+
}
32+
}

Behavioral/Iterator/BookList.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
namespace DesignPatterns\Behavioral\Iterator;
4+
5+
class BookList implements \Countable
6+
{
7+
8+
private $books;
9+
10+
public function getBook($bookNumberToGet)
11+
{
12+
if ((int)$bookNumberToGet <= $this->count()) {
13+
return $this->books[$bookNumberToGet];
14+
}
15+
16+
return NULL;
17+
}
18+
19+
public function addBook(Book $book)
20+
{
21+
$this->books[] = $book;
22+
23+
return $this->count();
24+
}
25+
26+
public function removeBook(Book $bookToRemove)
27+
{
28+
foreach ($this as $key => $book) {
29+
/** @var Book $book */
30+
if ($book->getAuthorAndTitle() === $bookToRemove->getAuthorAndTitle()) {
31+
unset($this->books[$key]);
32+
}
33+
}
34+
35+
return $this->count();
36+
}
37+
38+
public function count()
39+
{
40+
return count($this->books);
41+
}
42+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<?php
2+
3+
namespace DesignPatterns\Behavioral\Iterator;
4+
5+
class BookListIterator implements \Iterator
6+
{
7+
8+
/**
9+
* @var BookList
10+
*/
11+
protected $bookList;
12+
13+
/**
14+
* @var int
15+
*/
16+
protected $currentBook = 0;
17+
18+
public function __construct(BookList $bookList)
19+
{
20+
$this->bookList = $bookList;
21+
}
22+
23+
/**
24+
* Return the current book
25+
* @link http://php.net/manual/en/iterator.current.php
26+
* @return Book Can return any type.
27+
*/
28+
public function current()
29+
{
30+
return $this->bookList->getBook($this->currentBook);
31+
}
32+
33+
/**
34+
* (PHP 5 &gt;= 5.0.0)<br/>
35+
* Move forward to next element
36+
* @link http://php.net/manual/en/iterator.next.php
37+
* @return void Any returned value is ignored.
38+
*/
39+
public function next()
40+
{
41+
$this->currentBook++;
42+
}
43+
44+
/**
45+
* (PHP 5 &gt;= 5.0.0)<br/>
46+
* Return the key of the current element
47+
* @link http://php.net/manual/en/iterator.key.php
48+
* @return mixed scalar on success, or null on failure.
49+
*/
50+
public function key()
51+
{
52+
return $this->currentBook;
53+
}
54+
55+
/**
56+
* (PHP 5 &gt;= 5.0.0)<br/>
57+
* Checks if current position is valid
58+
* @link http://php.net/manual/en/iterator.valid.php
59+
* @return boolean The return value will be casted to boolean and then evaluated.
60+
* Returns true on success or false on failure.
61+
*/
62+
public function valid()
63+
{
64+
return $this->currentBook < $this->bookList->count();
65+
}
66+
67+
/**
68+
* (PHP 5 &gt;= 5.0.0)<br/>
69+
* Rewind the Iterator to the first element
70+
* @link http://php.net/manual/en/iterator.rewind.php
71+
* @return void Any returned value is ignored.
72+
*/
73+
public function rewind()
74+
{
75+
$this->currentBook = 0;
76+
}
77+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace DesignPatterns\Behavioral\Iterator;
4+
5+
class BookListReverseIterator extends BookListIterator
6+
{
7+
8+
public function __construct(BookList $bookList)
9+
{
10+
$this->bookList = $bookList;
11+
$this->currentBook = $this->bookList->count() - 1;
12+
}
13+
14+
public function next()
15+
{
16+
$this->currentBook--;
17+
}
18+
19+
public function valid()
20+
{
21+
return 0 <= $this->currentBook;
22+
}
23+
}

Behavioral/Iterator/File.php

Lines changed: 0 additions & 39 deletions
This file was deleted.

Behavioral/Iterator/Row.php

Lines changed: 0 additions & 27 deletions
This file was deleted.

Behavioral/Iterator/RowSet.php

Lines changed: 0 additions & 100 deletions
This file was deleted.

0 commit comments

Comments
 (0)