Download the PHP package hopr/pipeable without Composer
On this page you can find all versions of the php package hopr/pipeable. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download hopr/pipeable
More information about hopr/pipeable
Files in hopr/pipeable
Package pipeable
Short Description A collection of pipeable proxy functions for PHP's standard library
License
Informations about the package pipeable
🐇 Hopr\Pipeable
A collection of pipeable functions from PHP's standard library, reworked for the pipe operator. Designed to be straightforward and minimalistic. This repository will not see frequent updates, as it is not intended for continuous development.
Table of Contents
- Description
- Installation
- Usage
- Array Functions
- String Functions
- Available Functions
- Contributing
- License
Description
This library provides a set of functions that can be chained together using the |>
operator to create expressive and readable data transformations. It offers utilities for both arrays and strings, allowing for a more functional programming style in PHP.
The pipe operator only allows us to use single-argument functions, which is inconvenient. It forces us to create closures for almost everything. Hopr\Pipeable
tries to solves this issue.
Without Hopr\Pipeable
:
With Hopr\Pipeable
:
Nothing fancy, just straight to the point.
Behind the scenes, filter
(or reduce
) are implemented like this:
It desugars to the exact equivalent of the example without Hopr\Pipeable
!
You can also use this library to take advantage of partial application and lazy operations without using the pipe operator.
It allows us to have a more declarative way of coding.
Installation
Usage
Array Functions
String Functions
Available Functions
**Array Functions**
- `map(callable $callback)`: Applies a callback to each element of an array. - `filter(callable $callback)`: Filters an array using a callback. - `filter_map(callable $callback)`: Filters and maps an array in a single step. - `reduce(callable $callback, mixed $initial)`: Reduces an array to a single value. - `first()`: Gets the first element of an array. - `last()`: Gets the last element of an array. - `reverse()`: Reverses the order of elements in an array. - `unique()`: Removes duplicate values from an array. - `slice(int $offset, ?int $length = null)`: Extracts a slice of an array. - `sort()`: Sorts an array. - `asort()`: Sorts an array and maintains index association. - `shuffle()`: Shuffles an array. - `count()`: Counts the number of elements in an array. - `contains(mixed $value)`: Checks if a value exists in an array. - `hasKey(mixed $key)`: Checks if a key exists in an array. - `values()`: Gets all values from an array. - `keys()`: Gets all keys from an array.**String Functions**
- `map(callable $callback, string $separator = '')`: Maps a callable over each character of a string. - `filter(callable $callback)`: Filters the characters of a string using a callable. - `join(string|array $with)`: Joins the elements of an array into a string. - `reduce(callable $callback, mixed $initial)`: Reduces a string to a single value. - `first()`: Gets the first character of a string. - `last()`: Gets the last character of a string. - `reverse()`: Reverses a string. - `unique()`: Removes duplicate characters from a string. - `slice(int $offset, ?int $length = null)`: Extracts a substring from a string. - `sort()`: Sorts the characters of a string alphabetically. - `asort()`: Sorts the characters of a string alphabetically. - `shuffle()`: Shuffles the characters of a string randomly. - `count()`: Gets the length of a string. - `contains(string $value)`: Checks if a string contains a given value. - `values()`: Converts a string into an array of characters. - `toUpper()`: Converts a string to uppercase. - `toLower()`: Converts a string to lowercase. - `trim()`: Strips whitespace from the beginning and end of a string. - `replace(string $search, string $replace)`: Replaces occurrences of a substring within a string. - `chunk(int $by)`: Chunks a string into parts of a specified size. - `split(string $delimiter = '', int $chunk_by = 1)`: Splits a string either by a delimiter or into chunks.**Other Functions**
- `dump_and_die(mixed $var)`: Dumps a variable and terminates the script.Contributing
Contributions are welcome! Please feel free to submit a pull request or open an issue.
For major changes, please open an issue first to discuss what you would like to change. Please make sure to update tests as appropriate.
License
This project is licensed under the MIT License.