mirror of
https://github.com/DesignPatternsPHP/DesignPatternsPHP.git
synced 2025-07-27 02:00:20 +02:00
48 lines
998 B
PHP
48 lines
998 B
PHP
<?php declare(strict_types=1);
|
|
|
|
namespace DesignPatterns\Structural\Adapter;
|
|
|
|
/**
|
|
* This is the adapter here. Notice it implements Book,
|
|
* therefore you don't have to change the code of the client which is using a Book
|
|
*/
|
|
class EBookAdapter implements Book
|
|
{
|
|
/**
|
|
* @var EBook
|
|
*/
|
|
protected $eBook;
|
|
|
|
/**
|
|
* @param EBook $eBook
|
|
*/
|
|
public function __construct(EBook $eBook)
|
|
{
|
|
$this->eBook = $eBook;
|
|
}
|
|
|
|
/**
|
|
* This class makes the proper translation from one interface to another.
|
|
*/
|
|
public function open()
|
|
{
|
|
$this->eBook->unlock();
|
|
}
|
|
|
|
public function turnPage()
|
|
{
|
|
$this->eBook->pressNext();
|
|
}
|
|
|
|
/**
|
|
* notice the adapted behavior here: EBook::getPage() will return two integers, but Book
|
|
* supports only a current page getter, so we adapt the behavior here
|
|
*
|
|
* @return int
|
|
*/
|
|
public function getPage(): int
|
|
{
|
|
return $this->eBook->getPage()[0];
|
|
}
|
|
}
|