Download the PHP package brainite/splash without Composer
On this page you can find all versions of the php package brainite/splash. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download brainite/splash
More information about brainite/splash
Files in brainite/splash
Package splash
Short Description Chainable SPL and custom iterators.
License MIT
Homepage https://github.com/brainite/splash
Informations about the package splash
Splash
Tested against PHP 5.6/7.1/HHVM
Splash is a chainable (and therefore concise) way to interact with the SPL iterators. The Standard PHP Library (SPL) provides various utility classes and interfaces to address common problems. Some of the most visible solutions center around the SPL Iterators. The ability to iterate over a non-array collection and then to iterate over that iterator creates an interesting design pattern for building with PHP. However, the iterators deprioritize conciseness and therefore (to some degree) readability.
Example Default SPL Iterator Usage
This example is extracted from the PHP manual.
As with any non-chainable procedural code, you have to read this from the inside out in an unnatural and error-prone way.
Example Splash Alternative
The value of $object should be identical within the foreach loop compared to the approach above. However, the layers of iterators and their corresponding constructor values are now organized linearly and with less code to improve readability. Ultimately, that is the singular purpose of Splash - to leverage the benefits of chaining in the context of Iterators.
Getting Started
- Install using composer: brainite/splash.
- Create splash objects via one of these methods:
$splash = new \Splash\Splash();
// This is a basic approach.$splash = \Splash\Splash::go();
// This returns a Splash object that allows you to immediately chain.\Splash\Splash::mount();
// This makes the \splash() global function available for you to use for even more succinct coding. It creates a Splash object, and any arguments are added to an ArrayIterator that is thrown into the initial object via the push() method.
Equivalent examples:
Option 1
Option 2
Option 3
Option 4
For more examples, take a look at the unit tests.
Splash Custom Iterators
Splash comes bundles with some iterators that did not make it into SPL. Currently, this includes:
InverseRegexIterator
This is a simple iterator that excludes any items that match a regular expression.
`
SliceIterator
This is a simple iterator that narrows results to a slice like array_slice().
`
CallbackIterator
This is an iterator that runs a callback against each element and allows the callback to either return TRUE to retain values (like with CallbackFilterIterator) or to directly manipulate the new iterator. The direct advanced option allows for the splitting of values via the callback.
`