Patterns / Structural patterns
image

<?php

//Target
interface IText {
    //Request()
    function getText(): string;
}

//Adaptee
class StringList {
    private $rows = [];

    //SpecificRequest()
    function getString(): string {
        return implode("\n", $this->rows);
    }

    function add(string $value) {
        $this->rows[] = $value;
    }
}

//Adapter
class TextAdapter implements IText {

    public $rowList = null;

    //Request()
    function getText(): string {
        if ($this->rowList == null)
            return "";
        return $this->rowList->getString();
    }
}

function getTextAdapter(): TextAdapter {        
    $adapter = new TextAdapter();
    $rowList = new StringList();
    $rowList->add("line 1");
    $rowList->add("line 2");
    $adapter->rowList = $rowList;
    return $adapter;
}

//Client
$adapter = getTextAdapter();
$text = $adapter->getText();
//text: line 1
//      line 2
echo $text;


Convert the interface of a class into another interface clients expect. Adapter lets classes work together that couldn't otherwise because of incompatible interfaces.