diff --git a/Structural/Adapter/Book.php b/Structural/Adapter/Book.php index 6458beb..05d15cc 100644 --- a/Structural/Adapter/Book.php +++ b/Structural/Adapter/Book.php @@ -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; } } diff --git a/Structural/Adapter/BookInterface.php b/Structural/Adapter/BookInterface.php new file mode 100644 index 0000000..6ebabf4 --- /dev/null +++ b/Structural/Adapter/BookInterface.php @@ -0,0 +1,12 @@ +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]; + } } diff --git a/Structural/Adapter/EBookInterface.php b/Structural/Adapter/EBookInterface.php index bd3dc26..bae5033 100644 --- a/Structural/Adapter/EBookInterface.php +++ b/Structural/Adapter/EBookInterface.php @@ -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; } diff --git a/Structural/Adapter/Kindle.php b/Structural/Adapter/Kindle.php index 06d4489..a150767 100644 --- a/Structural/Adapter/Kindle.php +++ b/Structural/Adapter/Kindle.php @@ -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]; } } diff --git a/Structural/Adapter/PaperBookInterface.php b/Structural/Adapter/PaperBookInterface.php deleted file mode 100644 index 4b62149..0000000 --- a/Structural/Adapter/PaperBookInterface.php +++ /dev/null @@ -1,23 +0,0 @@ -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()); } }