Skip to content

Commit 8df8acf

Browse files
committed
add iterator pattern example
1 parent d840af9 commit 8df8acf

File tree

12 files changed

+206
-144
lines changed

12 files changed

+206
-144
lines changed

app/Controller/BehavioralController.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ public function command()
2424

2525
public function iterator()
2626
{
27-
(new \App\Factory\Behavioral\Iterator\UserRoles\Application)->run();
27+
// (new \App\Factory\Behavioral\Iterator\UserRoles\Application)->run();
28+
(new \App\Factory\Behavioral\Iterator\HotelSuppliers\Application)->run();
2829
}
2930
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php namespace App\Factory\Behavioral\Iterator\HotelSuppliers;
2+
3+
class Application
4+
{
5+
public function run()
6+
{
7+
$iterator = new \AppendIterator;
8+
$iterator->append(new DTWSupplierIterator(new DTWSupplier));
9+
$iterator->append(new HTOSupplierIterator(new HTOSupplier));
10+
11+
foreach ($iterator as $value) {
12+
echo '<pre>value: '. $value;
13+
}
14+
}
15+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php namespace App\Factory\Behavioral\Iterator\HotelSuppliers;
2+
3+
class DTWSupplier
4+
{
5+
private $_hotels = [
6+
['id' => 1, 'name' => 'Sri Sutra Hotel Kuala Lumpur'],
7+
['id' => 2, 'name' => 'Hotel Inn Kuala Lumpur'],
8+
['id' => 3, 'name' => 'Moven Pick Hotel Kuala Lumpur']
9+
];
10+
11+
public function getHotels()
12+
{
13+
return $this->_hotels;
14+
}
15+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php namespace App\Factory\Behavioral\Iterator\HotelSuppliers;
2+
3+
class DTWSupplierIterator implements \Iterator
4+
{
5+
private $_hotels;
6+
/**
7+
* counter for supplier items
8+
*/
9+
private $_counter = 0;
10+
11+
public function __construct(DTWSupplier $supplier)
12+
{
13+
$this->_hotels = $supplier->getHotels();
14+
}
15+
/**
16+
* current element
17+
*
18+
* @param null
19+
* @return string
20+
*/
21+
public function current(): string
22+
{
23+
return $this->_hotels[$this->_counter]['name'];
24+
}
25+
/**
26+
* get the current index
27+
*
28+
* @param null
29+
* @return int
30+
*/
31+
public function key(): int
32+
{
33+
return $this->_counter;
34+
}
35+
/**
36+
* get the next element in the array
37+
*
38+
* @param null
39+
* @return void
40+
*/
41+
public function next(): void
42+
{
43+
$this->_counter++;
44+
}
45+
/**
46+
* check weather we have next element in the array
47+
*
48+
* @param null
49+
* @return bool
50+
*/
51+
public function valid(): bool
52+
{
53+
return (count($this->_hotels) - 1) > $this->_counter;
54+
}
55+
/**
56+
* rewind from the start
57+
*
58+
* @param null
59+
* @return void
60+
*/
61+
public function rewind(): void
62+
{
63+
$this->_counter = 0;
64+
}
65+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php namespace App\Factory\Behavioral\Iterator\HotelSuppliers;
2+
3+
class HTOSupplier
4+
{
5+
private $_hotels = [
6+
['id' => 1, 'title' => 'Avari tower hotel Kuala Lumpur'],
7+
['id' => 2, 'title' => 'Mehran hotel Kuala Lumpur'],
8+
['id' => 3, 'title' => 'Foo Hotel Kuala Lumpur']
9+
];
10+
11+
public function getHotels()
12+
{
13+
return $this->_hotels;
14+
}
15+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php namespace App\Factory\Behavioral\Iterator\HotelSuppliers;
2+
3+
class HTOSupplierIterator implements \Iterator
4+
{
5+
private $_hotels;
6+
/**
7+
* counter for supplier items
8+
*/
9+
private $_counter = 0;
10+
11+
public function __construct(HTOSupplier $supplier)
12+
{
13+
$this->_hotels = $supplier->getHotels();
14+
}
15+
/**
16+
* current element
17+
*
18+
* @param null
19+
* @return string
20+
*/
21+
public function current(): string
22+
{
23+
return $this->_hotels[$this->_counter]['title'];
24+
}
25+
/**
26+
* get the current index
27+
*
28+
* @param null
29+
* @return int
30+
*/
31+
public function key(): int
32+
{
33+
return $this->_counter;
34+
}
35+
/**
36+
* get the next element in the array
37+
*
38+
* @param null
39+
* @return void
40+
*/
41+
public function next(): void
42+
{
43+
$this->_counter++;
44+
}
45+
/**
46+
* check weather we have next element in the array
47+
*
48+
* @param null
49+
* @return bool
50+
*/
51+
public function valid(): bool
52+
{
53+
return (count($this->_hotels) - 1) > $this->_counter;
54+
}
55+
/**
56+
* rewind from the start
57+
*
58+
* @param null
59+
* @return void
60+
*/
61+
public function rewind(): void
62+
{
63+
$this->_counter = 0;
64+
}
65+
}

app/Factory/Behavioral/Iterator/UserRoles/Application.php

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

app/Factory/Behavioral/Iterator/UserRoles/Role.php

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

app/Factory/Behavioral/Iterator/UserRoles/RoleIterator.php

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

app/Factory/Behavioral/Iterator/UserRoles/User.php

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

app/Factory/Behavioral/Iterator/UserRoles/UserRole.php

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

tests/IteratorPatternTest.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
use PHPUnit\Framework\TestCase;
4+
use App\Factory\Behavioral\Iterator\HotelSuppliers\{
5+
HTOSupplierIterator,
6+
HTOSupplier,
7+
DTWSupplierIterator,
8+
DTWSupplier
9+
};
10+
11+
class IteratorPatternTest extends TestCase {
12+
public function testCanIteratorThroughHTOSupplierIterator()
13+
{
14+
$supplier = new HTOSupplier;
15+
$iterator = new HTOSupplierIterator($supplier);
16+
17+
$hotelName = $iterator->current();
18+
$this->assertSame($supplier->getHotels()[0]['title'], $hotelName);
19+
}
20+
21+
public function testCanIteratorThroughDTWSupplierIterator()
22+
{
23+
$supplier = new DTWSupplier;
24+
$iterator = new DTWSupplierIterator($supplier);
25+
26+
$hotelName = $iterator->current();
27+
$this->assertSame($supplier->getHotels()[0]['name'], $hotelName);
28+
}
29+
}

0 commit comments

Comments
 (0)