2017 © Pedro Peláez
 

library handlers-list

Implementation of generic list of arbitrary handlers

image

flying/handlers-list

Implementation of generic list of arbitrary handlers

  • Sunday, June 17, 2018
  • by Flying
  • Repository
  • 1 Watchers
  • 0 Stars
  • 3 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 3 Versions
  • 0 % Grown

The README.md

Generic list of arbitrary handlers

Build Status Scrutinizer Code Quality Code Coverage, (*1)

This very simple library provides implementation of generic list of arbitrary handlers., (*2)

Its primary goal is to simplify process of organizing multiple related objects into iterable list of handlers with:, (*3)

  1. Ensuring type correctness by providing class constraint, handlers need to implement / extend
  2. Support for defining handlers priority

Requirements

No dependencies are required, just PHP 7.4 or 8.x., (*4)

Example

interface MyHandler {
    public function doSomething(): void;
}

class Foo implements MyHandler {

}

class Bar implements MyHandler {

}

class Baz extends Bar {

}

// Creating list of handlers
$handlers = new HandlersList([
    new Foo(), 
    new Bar(),
    new Baz(),
], MyHandler::class);

// ... later in code ...
foreach($handlers as $handler) {
    // We can be sure that $handler is of type MyHandler::class
    $handler->doSomething(); 
}

In a case if some handler implements PrioritizedHandlerInterface - its priority is considered:, (*5)

interface MyHandler {
    public function name(): string;
}

class A implements MyHandler {
    public function name(): string {
        return 'A';
    }
}

class B implements MyHandler, PrioritizedHandlerInterface {
    public function name(): string {
        return 'B';
    }

    public function getHandlerPriority(): int {
        return 10;
    }
}

$handlers = new HandlersList([
    new A(), 
    new B(),
], MyHandler::class);

foreach($handlers as $handler) {
    echo $handler->name() . ' '; 
}

Example above will output B A because B have higher priority then A despite the fact that it was put later in the list of handlers., (*6)

Methods

It is, of course, possible to modify list of handlers: - set() - set new list of handlers - add() - add a new handler to the list - remove() - remove given handler from the list - clear() - remove all handlers from the list, (*7)

There is also several methods for inspecting list of handlers: - isEmpty() - check if handlers list is empty - count() - get number of handlers in the list - accepts() - check if list accepts a given object or objects of given cass
- contains() - check if given handler is available in the list - filter() - filter handlers list using provided test callable and return array of matching handlers - find() - searches for a handler using provided callable - getIterator() - get handlers from the list as iterator - toArray() - get handlers from the list as array, (*8)

And remaining methods: - getConstraint() - get class constraint that is applied to handlers in this list, (*9)

Immutable handlers list

Besides default, mutable implementation of handlers list there is also immutable version: ImmutableHandlersList. Its functionality is completely same in except of list modification methods that returns new copy of the list instead of modifying original one., (*10)

License

MIT License, (*11)

The Versions

17/06 2018

dev-master

9999999-dev

Implementation of generic list of arbitrary handlers

  Sources   Download

MIT

The Requires

  • php >=7.1

 

The Development Requires

by Alexander Grimalovsky

17/06 2018

v2.0.0

2.0.0.0

Implementation of generic list of arbitrary handlers

  Sources   Download

MIT

The Requires

  • php >=7.1

 

The Development Requires

by Alexander Grimalovsky

17/06 2018

v1.0.0

1.0.0.0

Implementation of generic list of arbitrary handlers

  Sources   Download

MIT

The Requires

  • php >=7.1

 

The Development Requires

by Alexander Grimalovsky