2017 © Pedro Peláez
 

library selector

Selector pattern abstract class to manage polymorphism on the user level or run-time arguments.

image

virtualcomplete/selector

Selector pattern abstract class to manage polymorphism on the user level or run-time arguments.

  • Sunday, January 15, 2017
  • by virtualcomplete
  • Repository
  • 2 Watchers
  • 0 Stars
  • 0 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 1 Versions
  • 0 % Grown

The README.md

Build Status , (*1)

Selector

This is an abstract class that allows you to easily implement the provided selector pattern., (*2)

Requirements: PHP >5.4.0, (*3)

What is the selector pattern?

This selector pattern provides an easy way to deal with polymorphism at the user, or run-time, level. What does that mean? Well, if you code to an interface, you should already be familiar with polymorphism. It allows you to plug in different services using the same interface. Many frameworks handle the dependency injection of these services by what you bind on an application level. However, what if you have multiple services that can be applicable based on user settings? That is where this pattern will come in handy. See the payment method example below., (*4)

Usage

Just extend the Selector class and fill in the abstract methods as documented. getKey() should return one of the keys in your setMappings() array based on what arguments your application passes to it., (*5)

Example

class PaymentMethodSelector extends Selector
{
    protected function setInterface()
    {
        return 'VirtualComplete\Selector\Example\PaymentMethodInterface';
        // Easier on PHP >= 5.5
        // return PaymentMethodInterface::class;
    }

    protected function setMappings()
    {
        return [
            'cash' => 'VirtualComplete\Selector\Example\CashPaymentMethod',
            'check' => 'VirtualComplete\Selector\Example\CheckPaymentMethod',
            'credit' => 'VirtualComplete\Selector\Example\CreditPaymentMethod'
        ];
    }

    public function getKey($arguments)
    {
        $user = $arguments[0];
        return $user['paymentMethod'];
    }

    protected function defaultKey()
    {
        return 'cash';
    }
}

You'd then retrieve one of the classes by passing your $user data., (*6)

$user['paymentMethod'] = 'credit';
$method = $selector->selectFrom($user); // Returns CreditPaymentMethod instance

Use dependency injection with your selector for the best results., (*7)

The Versions

15/01 2017

dev-master

9999999-dev https://github.com/virtualcomplete/selector

Selector pattern abstract class to manage polymorphism on the user level or run-time arguments.

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

by Devon Bessemer

laravel design pattern selector polymorphism