Wzorzec adapter

<?php

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

interface ImageResizeInterface {

    public function setImage(string $fileName);

    public function resizeTo(string $width, string $height);

    public function resizeHeightByWidth(string $width);

    public function save();
}

class ResizeImage implements ImageResizeInterface {

    public function setImage(string $fileName) {
        //code
    }

    public function resizeTo(string $width, string $height) {
        //code
    }

    public function resizeHeightByWidth(string $width) {
        //code
    }

    public function save() {
        //code
    }

}

interface ImageInterface {

    public function setImageToResize(string $fileName);

    public function resizeImageTo(string $imageWidth, string $imageHeight);

    public function resizeImageHeightByWidth(string $width);

    public function saveImage();
}

class ImageResize implements ImageInterface {

    public function setImageToResize(string $fileName) {
        //code
    }

    public function resizeImageTo(string $imageWidth, string $imageHeight) {
        //code
    }

    public function resizeImageHeightByWidth(string $width) {
        //code
    }

    public function saveImage() {
        //code
    }

}

class ImageAdapter implements ImageResizeInterface {

    private $image;

    public function __construct(ImageResize $image) {
        $this->image = $image;
    }

    public function setImage(string $fileName) {
        $this->image->setImageToResize($fileName);
    }

    public function resizeTo(string $width, string $height) {
        $this->image->resizeImageTo($width, $height);
    }

    public function resizeHeightByWidth(string $width) {
        $this->image->resizeImageHeightByWidth($width);
    }

    public function save() {
        $this->image = saveImage();
    }

}

$imageAdapter = new ImageAdapter(new ImageResize());


var_dump($imageAdapter);

2//

<?php

//Book
class Book {

    private $author;
    private $title;

    function __construct(string $author, string $title) {
        $this->author = $author;
        $this->title = $title;
    }

    function getAuthor(): string {
        return $this->author;
    }

    function getTitle(): string {
        return $this->title;
    }

}

//book adapter
class BookAdapter {

    private $book;

    function __construct(Book $book) {
        $this->book = $book;
    }

    function getAuthorAndTitle(): string {
        return $this->book->getTitle() . ' by ' . $this->book->getAuthor();
    }

}

// client
$book = new Book("J.R.R. Tolkien", "Władca Pierścieni:");
$bookAdapter = new BookAdapter($book);

echo ('Author and Title: ' . $bookAdapter->getAuthorAndTitle());
Komentarze wyłączone