Download the PHP package peekandpoke/psi without Composer
On this page you can find all versions of the php package peekandpoke/psi. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download peekandpoke/psi
More information about peekandpoke/psi
Files in peekandpoke/psi
Package psi
Short Description Php Simple Iterations. Inspired by Java Streams Api.
License BSD-3-Clause
Informations about the package psi
Introduction
PSI helps you to write cleaner and more stable PHP-Code. Especially when it comes to iterating, filtering, mapping, sorting arrays or array-like data.
PSI consists of multiple small operations that can be combined and chained. Doing so results in easier to read and more expressive code.
PSI supports PHP versions >= 7.0
TOC
- Examples
- Details Docs
- UnitTests
- Releases
- Todos and ideas
Examples
Let's have a look at some examples.
Get all adults
In plain PHP this might looks like:
Get all adults and sort them by age
In plain PHP this might looks like:
Get all adults and sort them by age descending
Mapping
With Psi::map() you can convert incoming data.
For example lets convert all persons to ints (the persons age)
This might result in
We could also convert each Person into something else.
This might result in
Advanced filtering
With Psi::filterBy() you can combine mapping and filtering.
Let's get all adults by checking the age using Psi\IsGreaterThanOrEqual().
The example below will also give you all Persons with an age greater than or equal to 18.
Check if all or any elements match
With Psi::all() you can check if all elements match the given condition
With Psi::any() you can check if there is at least one element matching the condition
Grouping
Let's group all persons by their age
This might output:
Multiple inputs
You can pass multiple array or array-like parameters to Psi::it(... $inputs)
Skip and Limit
Let's get up to 5 adults but skip the first 10:
Or let's skip the first 10 no matter what they are and then get up to 5 adults
Or let's skip the first 10, then get up to 5 and then filter the adults out of these
Count, sum, min, max, average, median
Let's count the number of adults:
Let's sum the age of all persons:
Let's get the youngest age of all people we know:
Let's get the oldest age of all:
Let's get the average age:
Let's get the median age:
Let's get the median age of all adults:
Get first, last, random
Let's get the first person that fits a certain condition (name starting with "A")
Let's get the last person that fits a certain condition (name starting with "A")
Let's get a random person that fits a certain condition (name starting with "A")
Detailed Documentation
Filters - type checks
IsArray and IsNotArray
Filters for Arrays using is_array()
IsBool and IsNotBool
Filter for Booleans using is_bool()
IsCallable and IsNotCallable
Filter for Callables using is_callable()
IsDateString and IsNotDateString
Checks if the given string is a string that would be understood be new \DateTime($str)
IsEmpty and IsNotEmpty
Filter for empty things using empty()
IsInstanceOf and IsNotInstanceOf
Filter for class instance
IsInteger and IsNotInteger
Filter for integers using is_int()
IsIntegerString and IsNotIntegerString
Filter for strings that contain an integer
IsNull and IsNotNull
Filter for nulls
IsNumeric and IsNotNumeric
Filter for numeric values using is_numeric()
IsObject and IsNotObject
Filter for objects
IsResource and IsNotResource
Filter for resources
IsScalar and IsNotScalar
Filter for scalar values using is_scalar()
Filter - comparison
IsEqualTo and IsNotEqualTo
Filter for NON type safe eqaulity (==). See IsSame / IsNotSame for === comparison
UnitTests
First install all dependencies
Then run all tests
Releases
new in v1.2.0
REMOVED PHP 5.6 SUPPORT
Added
- Psi::all()
- Psi::any()
new in v1.1.0
Moved mapper ToFloat, ToInteger, ToString to Psi\Map...
Old versions are kept for compatibility and marked as deprecated.
new in v0.6.4
Psi::chunk
Will split the stream into chunks of the given size.
will result in
Psi::skip
Skips the first n elements in the current stream
will result in
Psi::limit
Limits the result to the first n element in the current stream
will result in
Psi::takeWhile
Take all elements of the input stream while the condition is met
will result in
Psi::takeUntil
Take all elements of the input stream until the condition is met
will result in
Psi::getLast
Get the last element of the stream.
will result in
Psi::getRandom
Will select a random element from the stream.
will result in
Num::IsMultipleOf and Num::IsNotMultipleOf
Filters all numbers that are a multiple of the given factor
will result in
Num::IsPrime and Num::IsNotPrime
Filters all number that are prime number.
CAUTION: do not use this impl for big numbers, a it can be very slow.
will result in
new in v0.6.3
Psi::filterBy()
In order to by able to use all other matchers on nested property / values of input objects, the method Psi::filterBy() was added
Psi::uniqueBy
Finds unique elements by first passing them through a mapper.
ToFloat
Maps inputs to floats.
ToInteger
Maps inputs to integers.
ToString
Maps inputs to strings.
Str::IsStartingWith and Str::IsNotStartingWith - with or with case
Filters all strings starting with the given needle. By default case-sensitive. Pass false as the second parameter to be non-case-sensitive.
Str::IsEndingWith and Str::IsNotEndingWith - with or without case
Filters all strings ending with the given needle. By default case-sensitive. Pass false as the second parameter to be non-case-sensitive.
Str::IsContaining and Str::IsNotContaining - with or without case
Filters all strings containing the given needle. By default case-sensitive. Pass false as the second parameter to be non-case-sensitive.
Str::IsMatchingRegex and Str::IsNotMatchingRegex
Filters all strings containing the given needle. By default case-sensitive. Pass false as the second parameter to be non-case-sensitive.
Str::WithoutAccents
Modifies a string by replacing special characters with the "normal" form, e.g.
new in v0.6.0 to v0.6.2
The internal folder structure was changed:
- public interface live now on the root level
- some over-engineering was removed (some classes removed)
- interface no longer have names like "UnaryFunctionInterface" but "UnaryFunction"
new in v0.5.0
Psi::groupBy
would become
Psi::sortBy
Sort a list of objects by one of their properties, e.g. sorting persons by their age
Todos and ideas
general
- make custom TerminalOperations possible Psi::reduce()
Unary filter functions for strings
String-Mappers
- Str::Camelize (StrToCamelCase)
- ... camel to dashes (StrToSlug)
- Str::UcFirst, Str::LcFirst
- Str::StrReplace
- Str::StrMbReplace
- Str::StrRegexReplace
- Str::StrMbRegexReplace
... for PHP-Types ... LocalDate::isSameDay
All versions of psi with dependencies
ext-pcre Version *
peekandpoke/php-types Version ^1.2.0
symfony/polyfill-mbstring Version ^1.6.0