TypeMapper

Since v1.2016060501

Description

TypeMapper is an interface. It is the base interface that all dispatch table-based type mappers implement.

Public Interface

TypeMapper has the following public interface:

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

/**
 * use an input item's data type to work out which method we should call
 */
interface TypeMapper
{
    /**
     * this is the result we return when there's no matching method
     * in the dispatch table that we're given
     */
    const FALLBACK_RESULT = "nothingMatchesTheInputType";

    /**
     * use an input item's data type to work out which method we should
     * call
     *
     * @param  mixed $item
     *         the item we want to dispatch
     * @param  array $dispatchTable
     *         the list of methods that are available
     * @param  string $fallback
     *         the value to return if there's no suitable entry for $item
     *         in $dispatchTable
     * @return string
     *         the name of the method to call
     */
    public function __invoke(
        $item,
        array $dispatchTable,
        $fallback = TypeMapper::FALLBACK_RESULT
    );

    /**
     * use an input item's data type to work out which method we should
     * call
     *
     * @param  mixed $item
     *         the item we want to dispatch
     * @param  array $dispatchTable
     *         the list of methods that are available
     * @param  string $fallback
     *         the value to return if there's no suitable entry for $item
     *         in $dispatchTable
     * @return string
     *         the name of the method to call
     */
    public static function using(
        $item,
        array $dispatchTable,
        $fallback = TypeMapper::FALLBACK_RESULT
    );
}

How To Use

The Dispatch Table

Each TypeMapper needs to support the dispatch table.

$dispatchTable is an associative array.

Notes

None at this time.