2.1. Adapter / Wrapper
- DB Client libraries adapter
- using multiple different webservices and adapters normalize data sothat the outcome is the same for all
You can also find this code on GitHub
Book.php
- <?php
- namespace DesignPatterns\Structural\Adapter;
- class Book implements BookInterface
- {
- /**
- * @var int
- */
- private $page;
- public function open()
- {
- $this->page = 1;
- }
- public function turnPage()
- {
- $this->page++;
- public function getPage(): int
- {
- return $this->page;
- }
- }
EBookAdapter.php
- <?php
- namespace DesignPatterns\Structural\Adapter;
- interface EBookInterface
- {
- public function unlock();
- public function pressNext();
- /**
- * returns current page and total number of pages, like [10, 100] is page 10 of 100
- *
- * @return int[]
- */
- public function getPage(): array;
- }
Kindle.php
Tests/AdapterTest.php
- <?php
- namespace DesignPatterns\Structural\Adapter\Tests;
- use DesignPatterns\Structural\Adapter\Book;
- use DesignPatterns\Structural\Adapter\EBookAdapter;
- use DesignPatterns\Structural\Adapter\Kindle;
- use PHPUnit\Framework\TestCase;
- class AdapterTest extends TestCase
- {
- public function testCanTurnPageOnBook()
- {
- $book = new Book();
- $book->open();
- $book->turnPage();
- $this->assertEquals(2, $book->getPage());
- }
- public function testCanTurnPageOnKindleLikeInANormalBook()
- {
- $kindle = new Kindle();
- $book = new EBookAdapter($kindle);
- $book->open();
- $book->turnPage();
- $this->assertEquals(2, $book->getPage());
- }