Download the PHP package ihor/nspl without Composer

On this page you can find all versions of the php package ihor/nspl. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package nspl

Non-standard PHP library (NSPL)

Non-standard PHP Library (NSPL) is a collection of modules that are meant to solve common day to day routine problems:

NSPL aims to make code compact and less verbose but still clear and readable. Look at the following example:

In pure PHP it would look like this:

You can see more examples in the here.

Installation

Using composer

Define the following requirement in your composer.json file:

or execute the following in the command line:

For the latest version which contains way more functionality require version

Manually

Checkout the code and include :

Reference

This is documentation for the dev version which contains the latest changes. For the version (latest stable version) documentation click here.

Here I assume that described functions are imported with use function:

If your PHP version is less than 5.6 you should import parent namespace and use functions with the namespace prefix:

Table of contents

nspl\f

Provides functions that act on other functions. Helps to write code in the functional programming paradigm.

id($value)

Identity function. Returns passed value.

apply($function, array $args = [])

Applies given function to arguments and returns the result

partial($function, $arg1)

Returns new partial function which will behave like with predefined left arguments passed to partial

rpartial($function, $arg1)

Returns new partial function which will behave like with predefined right arguments passed to rpartial

ppartial($function, array $args)

Returns new partial function which will behave like with predefined positional arguments passed to ppartial

flipped($function)

Returns function which accepts arguments in the reversed order

compose($f, $g)

Returns new function which applies each given function to the result of another from right to left is the same as

pipe($input, $function1, $function2)

Passes to composition of functions (functions have to be in the reversed order)

Tip

You can use chaining to get rid of partials in sequence transformations:

curried($function, $withOptionalArgs = false)

Returns a curried version of the function. If you are going to curry a function which reads args with then pass the number of args as the 2nd argument.

If the second argument is true, then curry function with optional args otherwise curry it only with required args. Alternatively, you can pass the exact number of args you want to curry.

uncurried($function)

Returns normal (uncurried) version of a curried function

memoized($function)

Returns memoized which returns the cached result when the same inputs occur again

which outputs

throttled($function, $wait)

Returns throttled version of the passed function, that, when invoked repeatedly, will only actually call the original function at most once per every wait milliseconds.

which outputs

Callbacks

provides all its functions as callbacks in its constants which have the same names as the functions.

Check more examples here.

nspl\op

Class provides functions that perform standard PHP operations and can be passed as callbacks to higher-order functions. Mimics Python's operator module. For example:

Callbacks

The module provides the following operations both as functions and callbacks. See an example below.

Function Operation
sum +
sub -
mul *
div /
mod %
inc ++
dec --
neg -
band &
bxor ^
bor |
bnot ~
lshift <<
rshift >>
lt <
le <=
eq ==
idnt ===
ne !=
nidnt !==
gt >
ge >=
and_ &&
or_ ||
xor_ xor
not !
concat .
instanceof_ instanceof
int (int)
bool (bool)
float (float)
str (string)
array_ (array)
object (object)
itemGetter($key)

Returns a function that returns key value for a given array

propertyGetter($property)

Returns a function that returns property value for a given object

methodCaller($method, array $args = array())

Returns a function that returns method result for a given object on predefined arguments

instanceCreator($class)

Returns a function that returns a new instance of a predefined class, passing its parameters to the constructor

Check more examples here.

nspl\a

Provides missing array functions which also can be applied to traversable sequences

all($sequence, $predicate)

Returns true if all of the items satisfy the predicate (or if the is empty). If the predicate was not passed returns true if all of the items are true.

any($sequence, $predicate)

Returns true if any of the items satisfy the predicate. If the predicate was not passed returns true if any of the items are true.

map($function, $sequence)

Applies function of one argument to each sequence item

flatMap($function, $sequence)

Applies function of one argument to each sequence item and flattens the result

zip($sequence1, $sequence2)

Zips two or more sequences

zipWith($function, $sequence1, $sequence2)

Generalises zip by zipping with the function given as the first argument, instead of an array-creating function

reduce($function, $sequence, $initial = 0)

Applies function of two arguments cumulatively to the sequence items, from left to right to reduce the sequence to a single value

filter($predicate, $sequence)

Returns sequence items that satisfy the predicate

filterNot($predicate, $sequence)

Returns sequence items that don't satisfy the predicate

take($sequence, $N, $step = 1)

Returns the first N sequence items with the given step

takeKeys($sequence, array $keys)

Returns sequence containing only the given keys

takeWhile($predicate, $sequence)

Returns the longest sequence prefix of all items which satisfy the predicate

first($sequence)

Returns the first sequence item

second($sequence)

Returns the second sequence item

drop($sequence, $N)

Drops the first N sequence items

dropKeys($sequence, array $keys)

Returns array containing all keys except the given ones

dropWhile($predicate, $sequence)

Drops the longest sequence prefix of all items which satisfy the predicate

last($sequence)

Returns the last sequence item

partition($predicate, $sequence)

Returns two lists, one containing values for which the predicate returned true, and the other containing the items that returned false

span($predicate, $sequence)

Returns two lists, one containing values for which your predicate returned true until the predicate returned false, and the other containing all the items that left

indexed($sequence, $by, $keepLast = true, $transform = null)

Returns array which contains indexed sequence items

is an array key or a function If is true only the last item with the key will be returned otherwise a list of items which share the same key value will be returned is a function that transforms list item after indexing

sorted($sequence, $reversed = false, $key = null, $cmp = null)

Returns array which contains sorted items from the passed sequence

If is true then return reversed sorted sequence. If is not boolean and was not passed then acts as a parameter is a function of one argument that is used to extract a comparison key from each item is a function of two arguments which returns a negative number, zero or positive number depending on whether the first argument is smaller than, equal to, or larger than the second argument

Check more examples here.

keySorted($sequence, $reversed = false)

Returns array which contains sequence items sorted by keys

flatten($sequence, $depth = null)

Flattens multidimensional sequence

pairs($sequence, $valueKey = false)

Returns a list of (key, value) pairs. If is true then returns (value, key) pairs.

merge($sequence1, $sequence2)

Returns array containing items and items

reorder(array $list, $from, $to)

Moves list item to another position

value($array, $key, $default = null)

Returns array value by key if it exists otherwise returns the default value

values($sequence)

Returns list of the sequence values

keys($sequence)

Returns list of the sequence keys

in($item, $sequence)

Checks if the item is present in array or traversable object

diff($sequence1, $sequence2)

Computes the difference of arrays or traversable objects

intersect($sequence1, $sequence2)

Computes the intersection of arrays or traversable objects

cartesianProduct($sequences)

Computes the cartesian product of two or more arrays or traversable objects

isList($var)

Returns true if the variable is a list

Chaining

It is possible to chain array function calls using the with function:

Callbacks

provides all its functions as callbacks in its constants which have the same names as the functions.

Check more examples here.

nspl\a\lazy

Provides lazy versions of functions from nspl\a

This module might be useful when you don't need to process all the values from an array or any other traversable sequence. To understand how these lazy functions work let's have a look at the following example.

Let's define a function which wraps a generator function and logs all the values it yields. It will help up us to see the order of function calls:

To have some data to operate on, let's define a function which returns all natural numbers. Since it returns all the natural numbers it never terminates:

Also, let's define the operations we want to perform on those numbers:

Now let's assume we want to take the first three even natural numbers and calculate their squares:

When we run this example we'll see the following output:

If we used regular non-lazy versions of these functions, we would generate all the natural numbers, then filtered only even numbers, then took only the first three of them and then calculated their squares. Instead of that, you see that functions were called one by one passing the result to the next function until we completed the full cycle:

  1. We took the first natural number – 1. It wasn't even, so we skipped it
  2. We took the next one – 2, it was even
  3. So it passed the function
  4. It was the first number we took, so it passed through the function as well
  5. Then we calculated its square and printed the result

The same repeated on steps 6-10 and 11-15. On step 14 the function took the last third number. So after step 15, when requested the next value didn't yield anything, and the whole iteration was finished.

Check this example here.

It possible to rewrite the code above using chaining:

Tip

Note that while functions from allow you to avoid redundant computations, in case when you need to process all sequence values, functions from will do the job faster.

nspl\args

Helps to validate function arguments

expects($constraints, $arg, $atPosition = null, $otherwiseThrow = '\InvalidArgumentException')

Checks that argument satisfies the required constraints otherwise throws the corresponding exception.

are callable(s) which return(s) true if the argument satisfies the requirements or it also might contain the required class name(s) If is null, then the position is calculated automatically comparing given argument to the actual arguments passed to the function defines exception which will be thrown if the given argument is invalid, it can be the exception class or exception object

Outputs:

expectsAll($constraints, array $args, array $atPositions = [], $otherwiseThrow = '\InvalidArgumentException')

Checks that all specified arguments satisfy the required constraints otherwise throws the corresponding exception.

expectsOptional($constraints, $arg, $atPosition = null, $otherwiseThrow = '\InvalidArgumentException')

Checks that argument is null or satisfies the required constraints otherwise throws the corresponding exception.

Predefined constraints

The module provides predefined constraints. Which can be one of the two types:

Callback Explanation Type
bool Checks that argument is a bool OR
int Checks that argument is an int OR
float Checks that argument is a float OR
numeric Checks that argument is numeric OR
string Checks that argument is a string OR
array_ Checks that argument is an array OR
object Checks that argument is an object OR
callable_ Checks that argument is callable OR
arrayKey Checks that argument can be an array key OR
traversable Checks that argument can be traversed with foreach OR
arrayAccess Checks that argument supports array index access OR
nonEmpty Checks that argument is not empty AND
positive Checks that argument is positive (> 0) AND
nonNegative Checks that argument is not negative (>= 0) AND
nonZero Checks that argument is not zero (!== 0) AND
any(constraint1, ..., constraintN) Checks constraints as if they were OR-constraints AND
all(constraint1, ..., constraintN) Checks constraints as if they were AND-constraints AND
not(constraint1, ..., constraintN) Checks that argument does't satisfy all listed constraints AND
values(value1, ..., valueN) Checks that argument is one of the specified values AND
longerThan($threshold) Checks that string argument is longer than given threshold AND
shorterThan($threshold) Checks that string argument is shorter than given threshold AND
biggerThan($threshold) Checks that number is bigger than given threshold AND
smallerThan($threshold) Checks that number is smaller than given threshold AND
hasKey($key) Checks that argument supports array index access and has given key AND
hasKeys($key1, ..., $keyN) Checks that argument supports array index access and has given keys AND
hasMethod($method) Checks that argument is an object and has given method AND
hasMethods($method1, ..., $methodN) Checks that argument is an object and has given methods AND

Duck-typing example:

Custom constraints

It is possible to use custom constraints. Just define a new function which returns true when argument satisfies the constraint:

or we can make it more convenient to use introducing a constant:

Outputs:

If you need to create a constraint which takes arguments, you must create a callable object which implements interface. It contains two methods:

nspl\ds

Provides non-standard data structures and methods to work with them

DefaultArray

Array with a default value for missing keys. If you pass a function as default value it will be called without arguments to provide a default value for the given key, this value will be inserted in the array for the key, and returned. Using DefaultArray turns this code:

into this:

defaultarray($default, $data = array())

Returns new DefaultArray

Set

An array-like collection that contains no duplicate elements. It supports basic set operations which take other sets, arrays and traversable objects as arguments

set

Returns new Set

Check more examples here.

nspl\rnd

randomString($length)

Returns a random alpha-numeric string of the given length

choice($sequence)

Returns a random item from a non-empty sequence

weightedChoice($weightPairs)

Returns a random item from a non-empty sequence of items with associated weights presented as pairs (item, weight)

sample($population, $length, $preserveKeys = false)

Returns a k length list of unique items chosen from the population sequence

Check more examples here.

nspl

getType($var)

Returns the variable type or its class name if it is an object

Roadmap

Contributing

This project uses semantic versioning to tag releases. Please submit your pull requests to the latest release branch where the issue was introduced.

Feedback

There are no mailing lists or discussion groups yet. Please use GitHub issues and pull request or follow me on Twitter @IhorBurlachenko


All versions of nspl with dependencies

PHP Build Version
Package Version
Requires php Version >=7.1.0
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package ihor/nspl contains the following files

Loading the files please wait ....