Download the PHP package dseguy/data-combinator without Composer
On this page you can find all versions of the php package dseguy/data-combinator. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package data-combinator
Data Combinator for PHP
When you want to list all possible combinations of various lists of data.
I.e., from to
This component allows you to create large dataset by combining each possible values with the others.
Applications
- Generates all possible combinations for a command line tool
- Generates all variations for incoming data
- Produces structured data
Installation
With Composer, do the timeless
Simple example
This generates all combinaisons for 1,2,3 with 4,5,6. A total of 9 array.
APIs
- Common behaviors - common behaviors to the public methods
- generate() - yields one value
- addConstant() - adds a unique value
- addSet() - adds a list of values
- addLambda() - calls an arbitrary function to generate a value
- addPermute() - creates all permutations from a list
- addCombine() - creates all combinations from a list
- addCopy() - clones objects instead of copying them by value
- addMatrix() - nests matrices within matrices
- addAlias() - reuses a previously generateur value
- addSequence() - creates values that differ by their identifier
- setClass() - selects the resulting object type : array, list or object
- count() - estimates the number of elements that will be produced
- toArray() - returns all the possible combinaisons as an array
Common-behaviors
Public methods are called with at least two arguments ; the name of the value, and the actual value or a generator for that value.
Each value name is a string, or null. When the name is null, automatic id is generated, starting from 0 (a la PHP).
Index for arrays may be provided with a string, which will be turned into an array index by PHP. Using null and Matrix::TYPE_ARRAY has the same effect as using Matrix::TYPE_LIST. Using Matrix::TYPE_LIST with named values is legit : the names will be dropped at generation time, yet, they provide some readability at configuration time.
It is possible to overwrite a previously set property by adding it again.
It is recommended to avoid using any other format beside string, integers in strings and null.
addConstant
This adds a unique value to the Matrix. No repetition with this one. Note that the value might be an array.
See also addCopy() for cloning objects.
generate
Returns a generator for usage with foreach() structures.
The generator will return the same values as the toArray()
method, but yielded, one by one.
After a full generation, the generate() method will yield again the same values, as per cache.
AddSet
This adds a list of values to the Matrix. Each value will be repeated once. Provide an array of arrays, to combine those arrays.
addLambda
This method adds a closure, a callback or an arrow function as a value. The closure will be called for each item to generate a new value.
The lambda may be constant or dynamic. The first one is calculated once, then always the same (for example, today's date). The second one is calculated each time, and may yield different value (for example, rand() value, or lambda that is dependent on previously generated values).
The closure receives an (array) argument with all the previously created values. That way, it may create a new value, based on previously generated values. That array is filled in the order of addition to the Matrix : in particular, this means that all values are not always available, since some of them may still be pending. Also, values added as 'alias', are processed last, and are not available.
The provided argument is an array. The type of its values are the type of values added to the Matrix. For sub-matrices, it may be another array or an object, depending on configuration.
When using a closure or an arrow function, it is possible to access a unique Identifier with the $this->uniqueId
property. The uniqueId is an int, starting at 1, and incremented each usage.
addPermute
Uses the list as one argument, and generates all possible permutations of the values in it.
addCombine
Uses the list as one set, and generates all possible combinaisons of them, from none (empty array) to all of them.
addCopy
While addConstant() makes a value copy of the constant, addCopy clones the input object each time. They will look identical, at generation time, but are actually distinct objects.
addMatrix
Adding a Matrix to another is the way to nest matrices. It also allows the creation of objects through the combining process.
Matrices have 2 options :
- Cache, which caches the values once they are generated.
- Matrix::WITHOUT_CACHE re-generate the Matrix each time,
- Matrix::WITH_CACHE generates the cache once, and keep reusing it later.
- Write mode, which configures the behavior in case of multiple definitions
- Matrix::OVERWRITE is the default. The value is replaced with the new definition.
- Matrix::SKIP skips any previously defined value
- Matrix::WARN throws an exception when a previously defined name is reused.
addAlias
Reuse a previously generated value in another slot of the generated data. This is useful when the same value has to be set at two (or more) slots.
Aliases may be added in any order : it is possible to call an alias created in a sub-matrix from the top-matrix, or vice-versa.
Aliases are processed as the last elements in a Matrix. They might not be available in a Lambda call, which will always happen before.
addSequence
Generates sequential data, from min to max, optionally updated by a closure.
setClass
By default, the matrix generates values of type array. This is the most versatile format.
It is possible to turn those arrays into objects, by giving it a class, or into a list (automatically indexed array) by using the Matrix::TYPE_LIST constant. It is possible to force the default type with the Matrix::TYPE_ARRAY constant.
The object is created with an instanciation without arguments. Then, public properties are set by public access. Private and protected properties are omitted; missing values are left untouched; extra values are omitted too.
You may also get a stdClass object by using its fully qualified name. Then, all passed values will be turned into a property.
count
Counts the number of elements to be produced.
toArray
Returns all the possible combinaisons as an array
FAQ
How to shuffle the results?
Use the toArray() method, and apply the PHP native shuffle() function on it.
How to limit the results?
Use foreach() with the generate() method, and count the number of element needed. Then, break when all the needed results were yielded.