src/Control/Pattern.php
protected static function __match(array $patterns)
    {
        return function (...$args) use ($patterns) {
            $parameterTypes = array_map(self::getType(), $args);

            try {
                $keysToValues = Arrays::zip(array_keys($patterns), array_values($patterns));

                list($key, $matchingPattern) = Arrays::first(
                    Pattern::patternApplies($parameterTypes, $args),
                    $keysToValues
                );
            } catch (ElementNotFoundException $e) {
                throw new IncompletePatternMatchException(
                    'Incomplete pattern match expression. (missing ' . implode(', ', $parameterTypes) . ')'
                );
            }


            /**
             * Upcoming Feature, Array DSL for matching via `x::xs` etc.
             */
//            // if key is a string, manually match
//            if (is_string($key)) {
//
//                throw new \Exception('Array DSL not available.');
//            }

            list($hasExtractable, $unwrappedArgs) = self::unwrapArgs($args);

            $value = $matchingPattern(...$args);
            $isCallable = is_callable($value);

            if ($hasExtractable && ! $isCallable) {
                throw new InvalidPatternMatchException(
                    'Invalid pattern match expression. (one of ' . implode(', ', $parameterTypes)
                    . ' requires a callback to unwrap)'
                );
            }

            /**
             * Extractable requires a callback to feed args into.
             */
            if ($hasExtractable && $isCallable) {
                return $matchingPattern(...$args)(...$unwrappedArgs);
            }

            /**
             * No extractable so we can just return the value directly.
             */
            return $value;
        };
    }
protected static function __match(array $patterns)
    {
        return function (...$args) use ($patterns) {
            $parameterTypes = array_map(self::getType(), $args);

            try {
                $keysToValues = Arrays::zip(array_keys($patterns), array_values($patterns));

                list($key, $matchingPattern) = Arrays::first(
                    Pattern::patternApplies($parameterTypes, $args),
                    $keysToValues
                );
            } catch (ElementNotFoundException $e) {
                throw new IncompletePatternMatchException(
                    'Incomplete pattern match expression. (missing ' . implode(', ', $parameterTypes) . ')'
                );
            }


            /**
             * Upcoming Feature, Array DSL for matching via `x::xs` etc.
             */
//            // if key is a string, manually match
//            if (is_string($key)) {
//
//                throw new \Exception('Array DSL not available.');
//            }

            list($hasExtractable, $unwrappedArgs) = self::unwrapArgs($args);

            $value = $matchingPattern(...$args);
            $isCallable = is_callable($value);

            if ($hasExtractable && ! $isCallable) {
                throw new InvalidPatternMatchException(
                    'Invalid pattern match expression. (one of ' . implode(', ', $parameterTypes)
                    . ' requires a callback to unwrap)'
                );
            }

            /**
             * Extractable requires a callback to feed args into.
             */
            if ($hasExtractable && $isCallable) {
                return $matchingPattern(...$args)(...$unwrappedArgs);
            }

            /**
             * No extractable so we can just return the value directly.
             */
            return $value;
        };
    }
protected static function __match ( array $ patterns ) { return function ( ... $ args ) use ( $ patterns ) { $ parameterTypes = array_map ( self :: getType ( ) , $ args ) ; try { $ keysToValues = Arrays :: zip ( array_keys ( $ patterns ) , array_values ( $ patterns ) ) ; list ( $ key , $ matchingPattern ) = Arrays :: first ( Pattern :: patternApplies ( $ parameterTypes , $ args ) , $ keysToValues ) ; } catch ( ElementNotFoundException $ e ) { throw new IncompletePatternMatchException ( 'Incomplete pattern match expression. (missing ' . implode ( ', ' , $ parameterTypes ) . ')' ) ; } /**
             * Upcoming Feature, Array DSL for matching via `x::xs` etc.
             */ //            // if key is a string, manually match //            if (is_string($key)) { // //                throw new \Exception('Array DSL not available.'); //            } list ( $ hasExtractable , $ unwrappedArgs ) = self :: unwrapArgs ( $ args ) ; $ value = $ matchingPattern ( ... $ args ) ; $ isCallable = is_callable ( $ value ) ; if ( $ hasExtractable && ! $ isCallable ) { throw new InvalidPatternMatchException ( 'Invalid pattern match expression. (one of ' . implode ( ', ' , $ parameterTypes ) . ' requires a callback to unwrap)' ) ; } /**
             * Extractable requires a callback to feed args into.
             */ if ( $ hasExtractable && $ isCallable ) { return $ matchingPattern ( ... $ args ) ( ... $ unwrappedArgs ) ; } /**
             * No extractable so we can just return the value directly.
             */ return $ value ; } ; }
Pattern Matching. Use switch-case for explicit values, this for everything else.
@param array $patterns
@return mixed
Pattern Matching . Use switch - case for explicit values this for everything else .