Giter VIP home page Giter VIP logo

php-property-method-delegator's Introduction

b2rPHP: PropertyMethodDelegator

Build Status

Delegate method to property instance

Features

  • Delegate methods to target property
  • Resolve method automatically
  • Define method alias

$propertyMethodDelegator structure

  • Key: strign Property name
  • Value: array Method definitions
    • Key: string Method name in LOWER case
    • Value: string|bool Delegate target method name or bool
      • true: Use same name
      • false: DO NOT resolve method if target has public metdhod

Usage

Simple

use b2r\Component\PropertyMethodDelegator\PropertyMethodDelegator;

class ArrayObjectWrapper
{
    use PropertyMethodDelegator;

    protected static $propertyMethodDelegator = [
        'arrayObject' => [],
    ];

    private $arrayObject;

    public function __construct()
    {
        $this->arrayObject = new ArrayObject();
    }
}

$a = new ArrayObjectWrapper();
$a->append(1);
var_dump($a->getArrayCopy()); #=>[1]

Mixed

use b2r\Component\PropertyMethodDelegator\PropertyMethodDelegator;

class Foo
{
    public function doFoo()
    {
        return __METHOD__;
    }

    public function execute()
    {
        return __METHOD__;
    }

    public function run()
    {
        return __METHOD__;
    }

    public function hello() {
        return __METHOD__;
    }

    public function publicHidden()
    {
        return __METHOD__;
    }

    protected function protectedMethod()
    {
        return __METHOD__;
    }
}

class Bar
{
    public function doBar()
    {
        return __METHOD__;
    }

    public function execute()
    {
        return __METHOD__;
    }

    public function run()
    {
        return __METHOD__;
    }

    public function hello() {
        return __METHOD__;
    }

    public function publicHidden()
    {
        return __METHOD__;
    }

    protected function protectedMethod()
    {
        return __METHOD__;
    }
}

class FooBar
{
    use PropertyMethodDelegator;

    protected static $propertyMethodDelegator = [
        'foo' => [
            'execute' => true, // FooBar::execute invoke FooBar::$foo::execute
            'foo' => 'doFoo',  // FooBar::foo invoke FooBar::$foo::doFoo
            'publichidden' => false, // DO NOT invoke FooBar::$foo::publicHidden
        ],
        'bar' => [
            'run' => true, // FooBar::run invoke FooBar::$bar::run
            'bar' => 'doBar', // FooBar::bar invoke FooBar::$bar::doBar
            'publichidden' => false,  // DO NOT invoke FooBar::$bar::publicHidden
        ],
    ];

    protected $foo;
    protected $bar;

    public function __construct()
    {
        $this->foo = new Foo();
        $this->bar = new Bar();
    }
}


$foobar = new FooBar();

echo $foobar->execute(),"\n"; #=>'Foo::execute'
echo $foobar->run(),"\n"; #=>'Bar::run'
echo $foobar->foo(),"\n"; #=>'Foo::doFoo'
echo $foobar->bar(),"\n"; #=>'Bar::doBar'
echo $foobar->doFoo(),"\n"; #=>'Foo::doFoo' Automatically resolved
echo $foobar->doBar(),"\n"; #=>'Bar::doBar' Automatically resolved
echo $foobar->hello(),"\n"; #=>'Foo::hello' Automatically resolved(foo is first)

// `protectedMethod` is protected, cannot resolve delegate method
var_dump($foobar->resolveDelegateMethod('protectedMethod')); #=> false

// `publicHidden` is hidden both foo and bar, cannot resolve delegate method
var_dump($foobar->resolveDelegateMethod('publicHidden')); #=> false

#------------------------------------------------------------

/**
 * Change delegate method resolving order: bar, foo
 */
class BarFoo extends FooBar
{
    protected static $propertyMethodDelegator = [
        'bar' => [],
        'foo' => [],
    ];
}

$barfoo = new BarFoo();
echo $barfoo->hello(),"\n"; #=>'Bar::hello' Automatically resolved(bar is first)

php-property-method-delegator's People

Contributors

b2r avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.