2017 © Pedro PelĂĄez
 

library delegator

Simple Delegator for PHP 5.4+

image

m1lt0n/delegator

Simple Delegator for PHP 5.4+

  • Friday, September 4, 2015
  • by m1lt0n
  • Repository
  • 1 Watchers
  • 1 Stars
  • 6 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 3 Versions
  • 0 % Grown

The README.md

Build Status, (*1)

Delegator

A simple delegation mechanism for objects., (*2)

Delegator is a simple library that allows delegation of methods from an object to others. It can be useful in combination with implementing Delegators, where one can partly decorate an object and partly delegate to the original, undecorated object., (*3)

Εxample

<?php

use Delegator\DelegatorInterface;

// Assume we have a Person class
class Person implements DelegatorInterface
{
    use \Delegator\DelegatorTrait;

    // Implement the delegateMap method. This is the only requirement
    // of the DelegatorInterface
    public function delegateMap()
    {
        return [
            'address' => [ 'fullAddress' ],
            'settings' => static::ANY,
        ];
    }

    public function __construct(Address $address)
    {
        $this->address = $address;
    }
}

class Address
{
    protected $street;
    protected $number;
    protected $city;

    public function __construct($street, $number, $city)
    {
        $this->street = $street;
        $this->number = $number;
        $this->city = $city;
    }

    public function fullAddress()
    {
        return "{$this->number}, {$this->street}, {$this->city}";
    }
}

class Settings
{
    public function all()
    {
        return 'all settings';
    }

    public function something()
    {
        return 'some setting';
    }
}

// Simply by using the DelegatorTrait and implementing the delegateMap method,
// we can now delegate calls to the mapped delegates.
$address = new Address('Percy str.', 9, 'London');
$settings = new Settings();
$me = new Person($address, $settings);

echo $me->fullAddress(); // this will be delegated to $me->address->fullAddress();
echo $me->all(); // this will be delegated to $me->settings->all();

// this is equivalent to $me->address->fullAddress, but we have simplified
// our API and hide the internals, while ensuring separation of concerns and
// limited responsibility for each class.

Note: Several delegates can be assigned in the mapping., (*4)

The Versions

04/09 2015

dev-master

9999999-dev https://github.com/m1lt0n/delegator

Simple Delegator for PHP 5.4+

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

decoration delegation

04/09 2015

1.1.0

1.1.0.0 https://github.com/m1lt0n/delegator

Simple Delegator for PHP 5.4+

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

decoration delegation

03/09 2015

1.0.0

1.0.0.0 https://github.com/m1lt0n/delegator

Simple Delegator for PHP 5.4+

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

decoration delegation