Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Iterator #2

Merged
merged 4 commits into from Feb 19, 2019
Merged
Diff settings

Always

Just for now

Copy path View file
@@ -0,0 +1,17 @@
<?php
require __DIR__. '/vendor/autoload.php';
use DP\Iterator\BookShelf;
use DP\Iterator\Book;
$bookShelf = new BookShelf();
$bookShelf->appendBook(new Book('星の王子様'));
$bookShelf->appendBook(new Book('かいけつゾロリ'));
$bookShelf->appendBook(new Book('ミッケ!'));
$bookShelfIt = $bookShelf->getIterator();
while ($bookShelfIt->hasNext())
{
echo $bookShelfIt->next()->getName() . "\n";
}
Copy path View file
@@ -0,0 +1,17 @@
<?php
require __DIR__. '/vendor/autoload.php';
use DP\Iterator\BookShelf;
use DP\Iterator\Book;
$bookShelf = new BookShelf();
$bookShelf->appendBook(new Book('星の王子様'));
$bookShelf->appendBook(new Book('かいけつゾロリ'));
$bookShelf->appendBook(new Book('ミッケ!'));
$bookShelfIt = $bookShelf->getIterator_FILO();
while ($bookShelfIt->hasNext())
{
echo $bookShelfIt->next()->getName() . "\n";
}
Copy path View file
@@ -0,0 +1,19 @@
<?php
namespace DP\Iterator;
class Book
{
private $bookName;
public function __construct(string $bookName)
{
$this->bookName = $bookName;
}
public function getName(): string
{
return $this->bookName;
}
}
Copy path View file
@@ -0,0 +1,44 @@
<?php
namespace DP\Iterator;
/**
* 何か(=本)の集合体[Aggregate]
*
* Class BookShelf
* @package DP\Iterator
*/
class BookShelf
{
private $books = [];
public function __construct()
{
$this->books = [];
}
public function getBookAt(int $index): Book
{
return $this->books[$index];
}
public function appendBook(Book $book): void
{
$this->books[] = $book;
}
public function getLength(): int
{
return count($this->books);
}
public function getIterator(): BookShelfIterator
{
return new BookShelfIterator($this);
}
public function getIterator_FILO(): BookShelfIterator_FILO
{
return new BookShelfIterator_FILO($this);
}
}
@@ -0,0 +1,33 @@
<?php
namespace DP\Iterator;
/**
* 何か(=本)の集合体を数える人[Iterator]
*
* Class BookShelfIterator
* @package DP\Iterator
*/
class BookShelfIterator
{
private $bookShelf;
private $index;
public function __construct(BookShelf $bookShelf)
{
$this->bookShelf = $bookShelf;
$this->index = 0;
}
public function hasNext(): bool
{
return $this->index < $this->bookShelf->getLength();
}
public function next(): Book
{
$book = $this->bookShelf->getBookAt($this->index);
$this->index++;
return $book;
}
}
@@ -0,0 +1,34 @@
<?php
namespace DP\Iterator;
/**
* 何か(=本)の集合体を数える人[Iterator]
* 数え方:LIFO
*
* Class BookShelfIterator
* @package DP\Iterator
*/
class BookShelfIterator_FILO
{
private $bookShelf;
private $index;
public function __construct(BookShelf $bookShelf)
{
$this->bookShelf = $bookShelf;
$this->index = $this->bookShelf->getLength() - 1;
}
public function hasNext(): bool
{
return $this->index >= 0;
}
public function next(): Book
{
$book = $this->bookShelf->getBookAt($this->index);
$this->index--;
return $book;
}
}
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.