mirror of
https://github.com/DesignPatternsPHP/DesignPatternsPHP.git
synced 2025-08-03 21:47:25 +02:00
PHP7 Adapter
This commit is contained in:
@@ -2,22 +2,25 @@
|
|||||||
|
|
||||||
namespace DesignPatterns\Structural\Adapter;
|
namespace DesignPatterns\Structural\Adapter;
|
||||||
|
|
||||||
/**
|
class Book implements BookInterface
|
||||||
* Book is a concrete and standard paper book.
|
|
||||||
*/
|
|
||||||
class Book implements PaperBookInterface
|
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* {@inheritdoc}
|
* @var int
|
||||||
*/
|
*/
|
||||||
|
private $page;
|
||||||
|
|
||||||
public function open()
|
public function open()
|
||||||
{
|
{
|
||||||
|
$this->page = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* {@inheritdoc}
|
|
||||||
*/
|
|
||||||
public function turnPage()
|
public function turnPage()
|
||||||
{
|
{
|
||||||
|
$this->page++;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getPage(): int
|
||||||
|
{
|
||||||
|
return $this->page;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
12
Structural/Adapter/BookInterface.php
Normal file
12
Structural/Adapter/BookInterface.php
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace DesignPatterns\Structural\Adapter;
|
||||||
|
|
||||||
|
interface BookInterface
|
||||||
|
{
|
||||||
|
public function turnPage();
|
||||||
|
|
||||||
|
public function open();
|
||||||
|
|
||||||
|
public function getPage(): int;
|
||||||
|
}
|
@@ -3,12 +3,10 @@
|
|||||||
namespace DesignPatterns\Structural\Adapter;
|
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 BookInterface,
|
||||||
*
|
* therefore you don't have to change the code of the client which is using a 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.
|
|
||||||
*/
|
*/
|
||||||
class EBookAdapter implements PaperBookInterface
|
class EBookAdapter implements BookInterface
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* @var EBookInterface
|
* @var EBookInterface
|
||||||
@@ -16,13 +14,11 @@ class EBookAdapter implements PaperBookInterface
|
|||||||
protected $eBook;
|
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()
|
public function open()
|
||||||
{
|
{
|
||||||
$this->eBook->pressStart();
|
$this->eBook->unlock();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* turns pages.
|
|
||||||
*/
|
|
||||||
public function turnPage()
|
public function turnPage()
|
||||||
{
|
{
|
||||||
$this->eBook->pressNext();
|
$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];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@@ -2,22 +2,16 @@
|
|||||||
|
|
||||||
namespace DesignPatterns\Structural\Adapter;
|
namespace DesignPatterns\Structural\Adapter;
|
||||||
|
|
||||||
/**
|
|
||||||
* EBookInterface is a contract for an electronic book.
|
|
||||||
*/
|
|
||||||
interface EBookInterface
|
interface EBookInterface
|
||||||
{
|
{
|
||||||
/**
|
public function unlock();
|
||||||
* go to next page.
|
|
||||||
*
|
|
||||||
* @return mixed
|
|
||||||
*/
|
|
||||||
public function pressNext();
|
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;
|
||||||
}
|
}
|
||||||
|
@@ -3,21 +3,37 @@
|
|||||||
namespace DesignPatterns\Structural\Adapter;
|
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
|
class Kindle implements EBookInterface
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* {@inheritdoc}
|
* @var int
|
||||||
*/
|
*/
|
||||||
|
private $page = 1;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var int
|
||||||
|
*/
|
||||||
|
private $totalPages = 100;
|
||||||
|
|
||||||
public function pressNext()
|
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];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -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();
|
|
||||||
}
|
|
@@ -28,7 +28,7 @@ Code
|
|||||||
|
|
||||||
You can also find these code on `GitHub`_
|
You can also find these code on `GitHub`_
|
||||||
|
|
||||||
PaperBookInterface.php
|
BookInterface.php
|
||||||
|
|
||||||
.. literalinclude:: PaperBookInterface.php
|
.. literalinclude:: PaperBookInterface.php
|
||||||
:language: php
|
:language: php
|
||||||
|
@@ -5,37 +5,26 @@ namespace DesignPatterns\Structural\Adapter\Tests;
|
|||||||
use DesignPatterns\Structural\Adapter\Book;
|
use DesignPatterns\Structural\Adapter\Book;
|
||||||
use DesignPatterns\Structural\Adapter\EBookAdapter;
|
use DesignPatterns\Structural\Adapter\EBookAdapter;
|
||||||
use DesignPatterns\Structural\Adapter\Kindle;
|
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
|
class AdapterTest extends \PHPUnit_Framework_TestCase
|
||||||
{
|
{
|
||||||
/**
|
public function testCanTurnPageOnBook()
|
||||||
* @return array
|
|
||||||
*/
|
|
||||||
public function getBook()
|
|
||||||
{
|
{
|
||||||
return array(
|
$book = new Book();
|
||||||
array(new Book()),
|
$book->open();
|
||||||
// we build a "wrapped" electronic book in the adapter
|
$book->turnPage();
|
||||||
array(new EBookAdapter(new Kindle())),
|
|
||||||
);
|
$this->assertEquals(2, $book->getPage());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
public function testCanTurnPageOnKindleLikeInANormalBook()
|
||||||
* 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)
|
|
||||||
{
|
{
|
||||||
$this->assertTrue(method_exists($book, 'open'));
|
$kindle = new Kindle();
|
||||||
$this->assertTrue(method_exists($book, 'turnPage'));
|
$book = new EBookAdapter($kindle);
|
||||||
|
|
||||||
|
$book->open();
|
||||||
|
$book->turnPage();
|
||||||
|
|
||||||
|
$this->assertEquals(2, $book->getPage());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user