DispatchTable

Since v1.2016060501

Description

DispatchTable is an interface. It is the interface implemented by all caching dispatch table classes.

Public Interface

DispatchTable has the following public interface:

// DispatchTable lives in this namespace
namespace GanbaroDigital\Polymorphism\V1\Interfaces;

// our input type(s) and return type(s)
use GanbaroDigital\Polymorphism\V1\Interfaces\TypeMapper;

interface DispatchTable
{
    /**
     * create a new dispatch table
     *
     * @param array $typeMethods
     *        a list of supported types and the method names they map onto
     * @param TypeMapper $typeMapper
     *        the TypeMapper to use to inspect
     * @param string $fallback
     *        what value do we return if our TypeMapper does not find a match?
     */
    public function __construct(
        $typeMethods,
        TypeMapper $typeMapper,
        $fallback = TypeMapper::FALLBACK_RESULT
    );

    /**
     * inspect a variable, and determine which method name to return
     *
     * @param  mixed $item
     *         the item to describe
     * @return string
     *         the method name that you should call
     */
    public function mapTypeToMethodName($item);
}

How To Use

Caching TypeMapper Results

The whole point of a DispatchTable is to cache results from a TypeMapper. There are three opportunities to do so:

You can combine these caching approaches in a single class.

For example, the AllPurposeDispatchTable uses both the post-cache and pass-through strategies. This guarantees maximum compatibility with all TypeMappers.

Notes

None at this time.