Skip to content

FunctionalInterface::filter() should support passing keys/both in all PHP versions #14

@johnkary

Description

@johnkary

Supplying the $flag parameter to FunctionalInterface::filter(callable $func = null, $flag = null); currently relies on PHP 5.6 constants \USE_KEY and \USE_BOTH to decide whether the callback is called with the value, key or both <value, key>. This functionality should instead be available for all PHP versions supported via composer.json require statement, currently >= 5.5.9.

I see two possible approaches here...

1) Haystack adds its own constants and uses them for filter($func, null $flag = null).

  • null - Pass value.
  • FunctionalInterface::USE_KEY - Pass key.
  • FunctionalInterface::USE_BOTH - Pass both.

2) Haystack adds two new methods that don't require calling code to provide the flags.

  • filter($func, null $flag = null) -- Passes container value to callback unless $flag given. $flag can be one of null || FunctionalInterface::USE_KEY || FunctionalInterface::USE_BOTH. Return true preserves key => value in new container.
  • filterByKey($func) -- Passes container key to callback. Implementation delegates to filter() and supplies the correct constant. Return true preserves key => value in new container.
  • filterByBoth($func) -- Passes container key as first callback argument, value as second argument. Implementation delegates to filter() and supplies the correct constant. Return true preserves key => value in new container.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions