Download the PHP package mpetrovich/dash without Composer
On this page you can find all versions of the php package mpetrovich/dash. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download mpetrovich/dash
More information about mpetrovich/dash
Files in mpetrovich/dash
Package dash
Short Description A functional programming library for PHP. Inspired by Underscore, Lodash, and Ramda.
License MIT
Homepage https://github.com/mpetrovich/dash
Informations about the package dash
Dash
A functional programming library for PHP. Inspired by Underscore, Lodash, and Ramda.
Jump to:
- Functions / Operations
- Highlights
- Why use Dash?
- Installation
- Usage
- Standalone
- Chaining
- Supported data types
- Currying
- Lazy evaluation
- Custom operations
- Tips
- Changelog
- Roadmap
- Contributing
Operations
View full list of operations here
Highlights
DirectoryIterator
, and more- Chaining
- Currying
- Lazy evaluation
- Custom operations
- Well-tested: Comprehensive tests with nearly 3,000 test cases and 100% code coverage
Why use Dash?
PHP's built-in array_*
functions are limited, difficult to compose, inconsistent, and don't work across many data types.
For instance, let's say we want to find the average age of males in this list:
Using PHP's built-in in functions, we might write something like this:
Dash makes common data transformation operations simpler:
This is just a tiny subset of what Dash can do. See the full list of operations here.
Installation
Requires PHP 7.4+
Usage
Dash operations are pure functions that can be used alone or chained together.
Standalone
Operations can be called as namespaced functions:
or as static methods:
Dash\_
can also be used as an alias for Dash\Dash
:
Chaining
Multiple operations can be chained in sequence using chain()
. Call value()
to return the final value.
To explicitly convert the value to an array or stdClass
, use arrayValue()
or objectValue()
respectively:
For convenience, Dash\chain()
can be aliased to a global function using addGlobalAlias()
. It only needs to be called once during your application bootstrap:
Sometimes you don't need the return value of the chain. However, the chain isn't processed until value()
is called. For semantic convenience, run()
is also an alias for value()
:
Supported data types
Dash can work with a wide variety of data types, including:
- arrays
- objects (eg.
stdClass
) - generators (still in development)
- anything that implements the
Traversable
interface DirectoryIterator
, which is also aTraversable
but cannot normally be used withiterator_to_array()
due to a PHP bug. Dash works around this transparently.
Examples
With an array:
With an object:
With a Traversable
:
With a DirectoryIterator
:
Currying
curry()
and related operations can be used to create curried functions from any callable:
Most Dash functions have a curried version that accepts input data as the last parameter instead of as the first. Curried versions are located in the Dash\Curry
namespace:
Similarly, partial()
and related operations can be used to create partially-applied functions:
Lazy evaluation
Chained operations are not evaluated until value()
or run()
is called. Furthermore, the input data can be changed and evaluated multiple times using with()
. This makes it simple to create reusable chains:
Chains can also be cloned and extended:
When value()
is called, the result is cached until the chain is modified or the input is changed using with()
.
Custom operations
Custom operations can be added, retrieved, and removed using setCustom()
, getCustom()
, and unsetCustom()
, respectively. Dash\custom()
is also an alias for Dash::getCustom()
:
When chained, the current input is passed as the first parameter to the custom operation:
Tips
If you find that Dash doesn't have an operation that you need, fear not. Custom logic can be added without giving up Dash chaining or other features. The simplest way to integrate missing operations is via the Dash\thru()
operation, which allows custom logic to modify and seamlessly pass through its results to the next step in the chain.
For example, suppose we want to use array_change_key_case()
and keep the usual Dash chaining semantics. With thru()
, it's simple:
Alternatively, if you find yourself needing to use array_change_key_case()
often, it may be better to add a new custom operation:
which you can then use like any other chainable Dash method:
Feedback
Found a bug or have a suggestion? Please create a new GitHub issue. We want your feedback!