PHP7 Adapter

This commit is contained in:
Dominik Liebler
2016-09-22 20:09:07 +02:00
parent f55008ddc7
commit d151e309c5
8 changed files with 81 additions and 86 deletions

View File

@@ -2,22 +2,25 @@
namespace DesignPatterns\Structural\Adapter;
/**
* Book is a concrete and standard paper book.
*/
class Book implements PaperBookInterface
class Book implements BookInterface
{
/**
* {@inheritdoc}
* @var int
*/
private $page;
public function open()
{
$this->page = 1;
}
/**
* {@inheritdoc}
*/
public function turnPage()
{
$this->page++;
}
public function getPage(): int
{
return $this->page;
}
}

View File

@@ -0,0 +1,12 @@
<?php
namespace DesignPatterns\Structural\Adapter;
interface BookInterface
{
public function turnPage();
public function open();
public function getPage(): int;
}

View File

@@ -3,12 +3,10 @@
namespace DesignPatterns\Structural\Adapter;
/**
* EBookAdapter is an adapter to fit an e-book like a paper book.
*
* This is the adapter here. Notice it implements PaperBookInterface,
* therefore you don't have to change the code of the client which using paper book.
* This is the adapter here. Notice it implements BookInterface,
* therefore you don't have to change the code of the client which is using a Book
*/
class EBookAdapter implements PaperBookInterface
class EBookAdapter implements BookInterface
{
/**
* @var EBookInterface
@@ -16,13 +14,11 @@ class EBookAdapter implements PaperBookInterface
protected $eBook;
/**
* Notice the constructor, it "wraps" an electronic book.
*
* @param EBookInterface $ebook
* @param EBookInterface $eBook
*/
public function __construct(EBookInterface $ebook)
public function __construct(EBookInterface $eBook)
{
$this->eBook = $ebook;
$this->eBook = $eBook;
}
/**
@@ -30,14 +26,22 @@ class EBookAdapter implements PaperBookInterface
*/
public function open()
{
$this->eBook->pressStart();
$this->eBook->unlock();
}
/**
* turns pages.
*/
public function turnPage()
{
$this->eBook->pressNext();
}
/**
* notice the adapted behavior here: EBookInterface::getPage() will return two integers, but BookInterface
* supports only a current page getter, so we adapt the behavior here
*
* @return int
*/
public function getPage(): int
{
return $this->eBook->getPage()[0];
}
}

View File

@@ -2,22 +2,16 @@
namespace DesignPatterns\Structural\Adapter;
/**
* EBookInterface is a contract for an electronic book.
*/
interface EBookInterface
{
/**
* go to next page.
*
* @return mixed
*/
public function unlock();
public function pressNext();
/**
* start the book.
* returns current page and total number of pages, like [10, 100] is page 10 of 100
*
* @return mixed
* @return int[]
*/
public function pressStart();
public function getPage(): array;
}

View File

@@ -3,21 +3,37 @@
namespace DesignPatterns\Structural\Adapter;
/**
* Kindle is a concrete electronic book.
* this is the adapted class. In production code, this could be a class from another package, some vendor code.
* Notice that it uses another naming scheme and the implementation does something similar but in another way
*/
class Kindle implements EBookInterface
{
/**
* {@inheritdoc}
* @var int
*/
private $page = 1;
/**
* @var int
*/
private $totalPages = 100;
public function pressNext()
{
$this->page++;
}
public function unlock()
{
}
/**
* {@inheritdoc}
* returns current page and total number of pages, like [10, 100] is page 10 of 100
*
* @return int[]
*/
public function pressStart()
public function getPage(): array
{
return [$this->page, $this->totalPages];
}
}

View File

@@ -1,23 +0,0 @@
<?php
namespace DesignPatterns\Structural\Adapter;
/**
* PaperBookInterface is a contract for a book.
*/
interface PaperBookInterface
{
/**
* method to turn pages.
*
* @return mixed
*/
public function turnPage();
/**
* method to open the book.
*
* @return mixed
*/
public function open();
}

View File

@@ -28,7 +28,7 @@ Code
You can also find these code on `GitHub`_
PaperBookInterface.php
BookInterface.php
.. literalinclude:: PaperBookInterface.php
:language: php

View File

@@ -5,37 +5,26 @@ namespace DesignPatterns\Structural\Adapter\Tests;
use DesignPatterns\Structural\Adapter\Book;
use DesignPatterns\Structural\Adapter\EBookAdapter;
use DesignPatterns\Structural\Adapter\Kindle;
use DesignPatterns\Structural\Adapter\PaperBookInterface;
/**
* AdapterTest shows the use of an adapted e-book that behave like a book
* You don't have to change the code of your client.
*/
class AdapterTest extends \PHPUnit_Framework_TestCase
{
/**
* @return array
*/
public function getBook()
public function testCanTurnPageOnBook()
{
return array(
array(new Book()),
// we build a "wrapped" electronic book in the adapter
array(new EBookAdapter(new Kindle())),
);
$book = new Book();
$book->open();
$book->turnPage();
$this->assertEquals(2, $book->getPage());
}
/**
* This client only knows paper book but surprise, surprise, the second book
* is in fact an electronic book, but both work the same way.
*
* @param PaperBookInterface $book
*
* @dataProvider getBook
*/
public function testIAmAnOldClient(PaperBookInterface $book)
public function testCanTurnPageOnKindleLikeInANormalBook()
{
$this->assertTrue(method_exists($book, 'open'));
$this->assertTrue(method_exists($book, 'turnPage'));
$kindle = new Kindle();
$book = new EBookAdapter($kindle);
$book->open();
$book->turnPage();
$this->assertEquals(2, $book->getPage());
}
}