Download the PHP package babenkoivan/fluent-array without Composer
On this page you can find all versions of the php package babenkoivan/fluent-array. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download babenkoivan/fluent-array
More information about babenkoivan/fluent-array
Files in babenkoivan/fluent-array
Package fluent-array
Short Description The package provides fluent interface for array management
License MIT
Informations about the package fluent-array
FluentArray
- Introduction
- Installation
- Configuration
- Macros
- Fixed methods
- Dynamic methods
- Implemented interfaces
- Code Formatting
Introduction
The fluent array library provides you with a convenient chainable interface. If you like object-oriented syntax or you just want to have more readable array declaration, the fluent array is at your service.
Basic usage
If we convert the fluent array to an associative array, by calling $order->toArray()
,
we will get the following output:
Storage array
Every time you call get, or any other method, that modifies or retrieves the state, you update the internal storage of fluent array.
Installation
Use composer to install the library:
Configuration
The configuration allows you to change a fluent array new functionality.
Local scope
To configure a specific fluent array instance use local scope.
Global scope
To configure all fluent arrays use global scope.
Macros
You can use macros to extend a fluent array functionality. It can be done via configuration in local scope.
Naming strategies
Naming strategies describe key transformation in dynamic methods.
For example, we want all our keys to be underscored in the storage array.
Now we want them to be camel-cased.
The supported naming strategies are:
Strategy | Example |
---|---|
CamelCaseStrategy | MyValue |
NullStrategy | myValue |
UnderscoreStrategy | my_value |
The default naming strategy is UnderscoreStrategy
.
Fixed methods
- all
- clean
- config
- count
- each
- filter
- first
- fromArray
- fromJson
- get
- globalConfig
- has
- keys
- krsort
- ksort
- last
- map
- pluck
- pushWhen
- push
- rsort
- setWhen
- set
- sort
- toArray
- toJson
- unset
- usort
- values
- when
all
The all
method returns the storage array.
clean
The clean
method removes all keys and values from the storage array.
config
The config
method allows you to set or retrieve local configuration.
count
The count
method returns the amount of values in the storage array.
each
The each
method iterates over the values in the storage array.
To stop the iteration return false
from the callback.
filter
The filter
method filters the storage array using the given callback.
Return false
from the callback to remove a value.
If callback is not specified, all values, that can be converted to false
will be removed.
first
The first
method retrieves the first value from the storage array.
fromArray
The fromArray
method converts an array to a fluent array.
fromJson
The fromJson
method converts JSON to a fluent array.
get
The get
method retrieves the value from the storage array,
that corresponds the given key.
globalConfig
The globalConfig
method allows you to set or retrieve global configuration.
has
The has
method checks if the given key exists in the storage array.
keys
The keys
method retrieves all the keys from the storage array.
krsort
The krsort
method sorts the storage array by keys in descending order.
You can specify sort flags as a first argument.
ksort
The ksort
method sorts the storage array by keys in ascending order.
You can specify sort flags as a first parameter.
last
The last
method retrieves the last value from the storage array.
map
The map
method applies the given callback to all values in the storage array
and returns a new fluent array.
pluck
The pluck
method extracts values with the given key, from child fluent arrays to a new fluent array.
push
The push
method appends the given value to the storage array.
Another way of using the push
method:
pushWhen
The pushWhen
method appends the given value to the storage array,
if the first argument is equivalent to true
.
Another way of using the pushWhen
method:
rsort
The rsort
method sorts the storage array in descending order.
You can specify sort flags as a first parameter.
set
The set
method sets the given key and value in the storage array.
setWhen
The setWhen
method sets the given key and value in the storage array,
if the first argument is equivalent to true
.
sort
The sort
method sorts the storage array in ascending order.
You can specify sort flags as a first parameter.
toArray
The toArray
method converts a fluent array to an array.
toJson
The toJson
method converts a fluent array to JSON.
unset
The unset
method removes the storage array value by the given key.
usort
The usort
method sorts the storage array using the given comparison function.
values
The values
method retrieves all the values from the storage array.
when
The when
method executes the given callback, if the first argument is equivalent to true
.
You can specify a default callback, that will be executed,
if the first argument is equivalent to false
.
Dynamic methods
- Dynamic setter
- Dynamic getter
- Dynamic has
- Dynamic pluck
- Dynamic unset
Dynamic setter
You can also set a key-value pair in the storage array using a dynamic setter.
If you want to set the key, that is reserved for a method name, you can escape it.
Add When
to set the given value if the first argument is equivalent to true
.
You can also chain creation of child fluent arrays.
Dynamic getter
To retrieve a value from the storage array you can use a dynamic getter.
Dynamic has
To check if a key exists in the storage array you can use a dynamic has
method.
Dynamic pluck
To extract values from child fluent arrays you can use a dynamic pluck
method.
Dynamic unset
To remove a value from the storage array you can use a dynamic unset
method.
Implemented interfaces
- Countable
- Serializable
- ArrayAccess
- IteratorAggregate
Countable
The Countable
interface provides the count
method support.
See more here.
Serializable
The Serializable
interface provides serialization support.
See more here.
ArrayAccess
The ArrayAccess
interface provides array access.
See more here.
IteratorAggregate
The IteratorAggregate
interface enables iteration over the storage array.
See more here.
Code formatting
If you use PhpStorm
and code auto formatting, you will likely face the issue, that the following code:
Will be transformed by PhpStorm
to:
Now the code is less readable, but luckily we can configure PhpStorm
to disable auto formatting the specified peace of code.
To do so, open PhpStorm
preferences, go to the Editor > Code Style
section and select option Enable formatter markers in comments
.
Now you can turn the formatter off for the specific part of your code: