/ Gists / 21. Mediator
On gists

21. Mediator

PHP Patterns

pic.md Raw #

mediator1.php Raw #

<?php
/**
 * AbstractColleague.
 */
interface Filter
{
    public function filter($value);
}

/**
 * Colleague. We decide in the implementation phase
 * that Colleagues should not know the next Colleague
 * in the chain, resorting to a Mediator to link them together.
 * This choice succesfully avoids a base abstract class
 * for Filters.
 * Remember that this is an example: it is not only
 * Chain of Responsibility that can be alternatively implemented
 * as a Mediator.
 */
class TrimFilter implements Filter
{
     public function filter($value)
     {
         return trim($value);
     }
}

/**
 * Colleague.
 */
class NullFilter implements Filter
{
     public function filter($value)
     {
         return $value ? $value : '';
     }
}

/**
 * Colleague.
 */
class HtmlEntitiesFilter implements Filter
{
     public function filter($value)
     {
         return htmlentities($value);
     }
}

/**
 * The Mediator. We avoid referencing it from ConcreteColleagues
 * and so the need for an interface. We leave the implementation
 * of a bidirectional channel for the Observer pattern's example.
 * This class responsibility is to store the value and coordinate
 * filters computation when they have to be applied to the value.
 * Filtering responsibilities are obviously a concern of
 * the Colleagues, which are Filter implementations.
 */
class InputElement
{
    protected $_filters;
    protected $_value;

    public function addFilter(Filter $filter)
    {
        $this->_filters[] = $filter;
        return $this;
    }

    public function setValue($value)
    {
        $this->_value = $this->_filter($value);
    }

    protected function _filter($value)
    {
        foreach ($this->_filters as $filter) {
            $value = $filter->filter($value);
        }
        return $value;
    }

    public function getValue()
    {
        return $this->_value;
    }
}

$input = new InputElement();
$input->addFilter(new NullFilter())
      ->addFilter(new TrimFilter())
      ->addFilter(new HtmlEntitiesFilter());
$input->setValue(' You should use the <h1>-<h6> tags for your headings.');
echo $input->getValue(), "\n";

mediator2.php Raw #

<?php

class Logging
{
 public function addMessage($msg)
 {
  echo $msg;
 }
}

class Dispatcher
{
 private $listeners=array();
 
 public function addListener($eventName, $callback)
 {
  $this->listeners[$eventName]=$callback;
 }
 
 public function notify($eventName, $parameter1, $parameter2)
 {
  $this->listeners[$eventName]($parameter1, $parameter2);
 }
}

class Person
{
 private $name;
 private $dispatcher;
 
 public function __construct($dispatcher)
 {
  $this->dispatcher = $dispatcher;
 }
 
 public function setName($name)
 {
  $this->name = $name;
  $this->dispatcher->notify('property.changed', 'Name', $name);
 }
 
 public function getName()
 {
  return $this->name;
 }
}



$log = new Logging();

$dispatcher = new Dispatcher;
$dispatcher->addListener('property.changed', function ($propertyName, $value) use ($log) {
   $log->addMessage(
    "Property - {$propertyName} - was set to: {$value}"
   );
  });


$first = new Person($dispatcher);
$first->setName('Ion');

mediator3.php Raw #

<?php

interface Mediator
{
    public function getUser(string $username): string;
}

abstract class Colleague
{
    protected  $mediator;

    public function setMediator(Mediator $mediator)
    {
        $this->mediator = $mediator;
    }
}

class Ui extends Colleague
{
    public function outputUserInfo(string $username)
    {
        echo $this->mediator->getUser($username);
    }
}

class UserRepository extends Colleague
{
    public function getUserName(string $user): string
    {
        return 'User: ' . $user;
    }
}

class UserRepositoryUiMediator implements Mediator
{
    private  $userRepository;
    private  $ui;

    public function __construct(UserRepository $userRepository, Ui $ui)
    {
        $this->userRepository = $userRepository;
        $this->ui = $ui;

        $this->userRepository->setMediator($this);
        $this->ui->setMediator($this);
    }

    public function printInfoAbout(string $user)
    {
        $this->ui->outputUserInfo($user);
    }

    public function getUser(string $username): string
    {
        return $this->userRepository->getUserName($username);
    }
}

$mediator = new UserRepositoryUiMediator(new UserRepository(), new Ui());
$mediator->printInfoAbout('Dominik');

// User: Dominik