Skip to content

Commit d151e30

Browse files
author
Dominik Liebler
committed
PHP7 Adapter
1 parent f55008d commit d151e30

File tree

8 files changed

+81
-86
lines changed

8 files changed

+81
-86
lines changed

Structural/Adapter/Book.php

+11-8
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,25 @@
22

33
namespace DesignPatterns\Structural\Adapter;
44

5-
/**
6-
* Book is a concrete and standard paper book.
7-
*/
8-
class Book implements PaperBookInterface
5+
class Book implements BookInterface
96
{
107
/**
11-
* {@inheritdoc}
8+
* @var int
129
*/
10+
private $page;
11+
1312
public function open()
1413
{
14+
$this->page = 1;
1515
}
1616

17-
/**
18-
* {@inheritdoc}
19-
*/
2017
public function turnPage()
2118
{
19+
$this->page++;
20+
}
21+
22+
public function getPage(): int
23+
{
24+
return $this->page;
2225
}
2326
}

Structural/Adapter/BookInterface.php

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
namespace DesignPatterns\Structural\Adapter;
4+
5+
interface BookInterface
6+
{
7+
public function turnPage();
8+
9+
public function open();
10+
11+
public function getPage(): int;
12+
}

Structural/Adapter/EBookAdapter.php

+18-14
Original file line numberDiff line numberDiff line change
@@ -3,41 +3,45 @@
33
namespace DesignPatterns\Structural\Adapter;
44

55
/**
6-
* EBookAdapter is an adapter to fit an e-book like a paper book.
7-
*
8-
* This is the adapter here. Notice it implements PaperBookInterface,
9-
* therefore you don't have to change the code of the client which using paper book.
6+
* This is the adapter here. Notice it implements BookInterface,
7+
* therefore you don't have to change the code of the client which is using a Book
108
*/
11-
class EBookAdapter implements PaperBookInterface
9+
class EBookAdapter implements BookInterface
1210
{
1311
/**
1412
* @var EBookInterface
1513
*/
1614
protected $eBook;
1715

1816
/**
19-
* Notice the constructor, it "wraps" an electronic book.
20-
*
21-
* @param EBookInterface $ebook
17+
* @param EBookInterface $eBook
2218
*/
23-
public function __construct(EBookInterface $ebook)
19+
public function __construct(EBookInterface $eBook)
2420
{
25-
$this->eBook = $ebook;
21+
$this->eBook = $eBook;
2622
}
2723

2824
/**
2925
* This class makes the proper translation from one interface to another.
3026
*/
3127
public function open()
3228
{
33-
$this->eBook->pressStart();
29+
$this->eBook->unlock();
3430
}
3531

36-
/**
37-
* turns pages.
38-
*/
3932
public function turnPage()
4033
{
4134
$this->eBook->pressNext();
4235
}
36+
37+
/**
38+
* notice the adapted behavior here: EBookInterface::getPage() will return two integers, but BookInterface
39+
* supports only a current page getter, so we adapt the behavior here
40+
*
41+
* @return int
42+
*/
43+
public function getPage(): int
44+
{
45+
return $this->eBook->getPage()[0];
46+
}
4347
}

Structural/Adapter/EBookInterface.php

+5-11
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,16 @@
22

33
namespace DesignPatterns\Structural\Adapter;
44

5-
/**
6-
* EBookInterface is a contract for an electronic book.
7-
*/
85
interface EBookInterface
96
{
10-
/**
11-
* go to next page.
12-
*
13-
* @return mixed
14-
*/
7+
public function unlock();
8+
159
public function pressNext();
1610

1711
/**
18-
* start the book.
12+
* returns current page and total number of pages, like [10, 100] is page 10 of 100
1913
*
20-
* @return mixed
14+
* @return int[]
2115
*/
22-
public function pressStart();
16+
public function getPage(): array;
2317
}

Structural/Adapter/Kindle.php

+20-4
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,37 @@
33
namespace DesignPatterns\Structural\Adapter;
44

55
/**
6-
* Kindle is a concrete electronic book.
6+
* this is the adapted class. In production code, this could be a class from another package, some vendor code.
7+
* Notice that it uses another naming scheme and the implementation does something similar but in another way
78
*/
89
class Kindle implements EBookInterface
910
{
1011
/**
11-
* {@inheritdoc}
12+
* @var int
1213
*/
14+
private $page = 1;
15+
16+
/**
17+
* @var int
18+
*/
19+
private $totalPages = 100;
20+
1321
public function pressNext()
22+
{
23+
$this->page++;
24+
}
25+
26+
public function unlock()
1427
{
1528
}
1629

1730
/**
18-
* {@inheritdoc}
31+
* returns current page and total number of pages, like [10, 100] is page 10 of 100
32+
*
33+
* @return int[]
1934
*/
20-
public function pressStart()
35+
public function getPage(): array
2136
{
37+
return [$this->page, $this->totalPages];
2238
}
2339
}

Structural/Adapter/PaperBookInterface.php

-23
This file was deleted.

Structural/Adapter/README.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ Code
2828

2929
You can also find these code on `GitHub`_
3030

31-
PaperBookInterface.php
31+
BookInterface.php
3232

3333
.. literalinclude:: PaperBookInterface.php
3434
:language: php

Structural/Adapter/Tests/AdapterTest.php

+14-25
Original file line numberDiff line numberDiff line change
@@ -5,37 +5,26 @@
55
use DesignPatterns\Structural\Adapter\Book;
66
use DesignPatterns\Structural\Adapter\EBookAdapter;
77
use DesignPatterns\Structural\Adapter\Kindle;
8-
use DesignPatterns\Structural\Adapter\PaperBookInterface;
98

10-
/**
11-
* AdapterTest shows the use of an adapted e-book that behave like a book
12-
* You don't have to change the code of your client.
13-
*/
149
class AdapterTest extends \PHPUnit_Framework_TestCase
1510
{
16-
/**
17-
* @return array
18-
*/
19-
public function getBook()
11+
public function testCanTurnPageOnBook()
2012
{
21-
return array(
22-
array(new Book()),
23-
// we build a "wrapped" electronic book in the adapter
24-
array(new EBookAdapter(new Kindle())),
25-
);
13+
$book = new Book();
14+
$book->open();
15+
$book->turnPage();
16+
17+
$this->assertEquals(2, $book->getPage());
2618
}
2719

28-
/**
29-
* This client only knows paper book but surprise, surprise, the second book
30-
* is in fact an electronic book, but both work the same way.
31-
*
32-
* @param PaperBookInterface $book
33-
*
34-
* @dataProvider getBook
35-
*/
36-
public function testIAmAnOldClient(PaperBookInterface $book)
20+
public function testCanTurnPageOnKindleLikeInANormalBook()
3721
{
38-
$this->assertTrue(method_exists($book, 'open'));
39-
$this->assertTrue(method_exists($book, 'turnPage'));
22+
$kindle = new Kindle();
23+
$book = new EBookAdapter($kindle);
24+
25+
$book->open();
26+
$book->turnPage();
27+
28+
$this->assertEquals(2, $book->getPage());
4029
}
4130
}

0 commit comments

Comments
 (0)