Download the PHP package eftec/chaosmachineone without Composer
On this page you can find all versions of the php package eftec/chaosmachineone. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download eftec/chaosmachineone
More information about eftec/chaosmachineone
Files in eftec/chaosmachineone
Package chaosmachineone
Short Description A Chaos Machine for PHP
License LGPL-3.0
Homepage https://github.com/EFTEC/ChaosMachineOne
Informations about the package chaosmachineone
ChaosMachineOne for PHP
A controlled random generator data for PHP. The objective of the library is to help generate and fill random values to memory, Mysql, Sql Server and Oracle while we could add some tendencies/biases so the values are not as fully random.
Table of contents
- ChaosMachineOne for PHP
- What is the objective?
- fields
- gen
- minilang
- Reserved variables
- Logic
- Range Functions (numbers)
- ramp($fromX, $toX, $fromY, $toY)
- log($startX,$startY,$scale=1)
- exp($startX,$startY,$scale=1)
- sin($startX,$startY,$speed=1,$scale=1,$angle=null)
- atan($centerX,$startY,$speed=1,$scale=1)
- parabola($centerX,$startY,$scaleA=1,$scaleB=1,$scale=1)
- bell($centerX, $startY, $sigma=1, $scaleY=1)
- Fixed functions (numbers)
- randomprop(...$args)
- random($from,$to,$jump=1,...$probs)
- field.speed=xxxx
- field.accel=xxxx
- field.value=xxxx
- field.getvalue
- field.valueabs
- field.day , field.month , field.year, field.hour, field.minute, field.weekday
- field.stop=xxxx
- field.add=xxxx
- field.concat=xxxx
- field.skip=xxx
- Arrays and texts
- ->setArray('arrayname',[])
- randomarray("arrayname",'field'=null)
- ->setFormat('formatName',[])
- randomformat($nameFormat)
- randomtext($starting,$arrayName,$paragraph,$wordMinimum,$wordMaximum)
- randommask($mask,$arrayName='')
- Other Features
- end
- omit
- File features
- arrayFromFolder()
- Script function destionationArray.copyfilefrom()=originArray.getvalue
- Database
- table($table, $conditions,$prefix='origin_')
- update($table,$indexcolumn,$indexvalue,$updatecolumn1,$updatevalue1...)
- insert($storeCache=false,$echoProgress=null,$continueOnError=false,$maxRetry=3)
- setInsert($continueOnError=false,$maxRetry=3)
- setArrayFromDBQuery($name,$query,$probability=[1],$queryParam=null)
- setArrayFromDBTable($name,$table,$column,$probability=[1])
- Examples:
- Connect to Sql Server
- Connect to Mysql
- Generate code based in a table
- Insert 1000 random values
- Update all rows of a table
- version
- License
What is the objective?
Sometimes we want to generate fake values for the database that are controlled and consistent. So, this library tries to create an ordered chaos.
While there are libraries to generate random values (such as fzaninotto/Faker), the objective of this library is to fill tables with random but credible/with a trend value.
Let's say the next exercise. We want to generate random values for a new system (sales)
If we generate random values, the chart would look like
->gen('when _index<200 then idtable.value=random(-10,10,0.2)')
Why is it so random? it is because they are random values (sic).
So, they are right, but they don't look real because there is not a trend or a natural flow of information, it is just static noise.
Then, let's generate the same value with a sine (for example, let's say that there is a cycle of sales)
->gen('when _index<200 then idtable.add=sin(0,0,10,30)')
The chart has a trend, but it is too predictable. So, let's add all factors.
->gen('when _index<200 then idtable.value=random(-10,10,0.2) and idtable.add=sin(0,0,10,30)')
While this chart is far from real, but it is not TOO RANDOM, and it has a trend.
fields
field($name,$type,$special='database',$initValue=0,$min=PHP_INT_MIN,$max=PHP_INT_MAX)
fields are our values. They could be numeric, date and string.
-
Fields could have speed and acceleration. However, if you change the value manually, then the speed and acceleration is ignored.
- "field.value=X" it sets the value of the field.
- "field.speed=X" it sets the speed of the field.
- "field.accel=X" it sets the acceleration of the field. If the speed is set manually, then the acceleration is ignored.
-
"field.stop=X" it sets the speed and acceleration to zero, and sets the value to X.
- The speed and acceleration is evaluated when it is executed (method run() )
Examples:
gen
It generates a value using the Minilang syntaxis.
The syntaxis is as follows:
minilang
Variable | Explanation | Example |
---|---|---|
$var | it is php global variable. | $var="hi" |
field | it is an minilang variable. | field |
field.value it is the value of a column (if any) | field.value="hi" | |
_index | indicates the current index (current number of row) | _index<200 |
20 , 20.50 | its a fixed value | 20,50.3 |
"text",'text' | it is fixed string value | "text",'text' |
fn() | its a function | myfunction(value), some.myfunction() |
Reserved variables
Reserved word | Explanation |
---|---|
null | null value |
false | false value |
true | true value |
on | 1 |
off | 0 |
undef | -1 (for undefined) |
flip | (special value). It inverts a value ON<->OFF Used as value.flip |
now | returns the current timestamp (integer) |
timer | returns the current timestamp (integer) |
interval | returns the interval (in seconds) between the last change and now |
fullinterval | returns the interval (in seconds) between the start of the process and now |
_index | returns the current index (the current counter of row) |
always | reserved word. It is used as "when always" |
Examples:
myvar = is a variable
when always set myvar.value=null
when _index<20 set myvar.value=false
Limits.
- Parenthesis are now allowed (unless it is defines a function).
-
-
a1=(20+30) is not allowed however a1=20 , a1+30 is allowed
-
- The syntax is divided in two parts, one of the logic (when) and other is the set (then)
- Conditions are evaluated in order. If one condition is meet, then it stop other evaluations.
-
-
Logic
It sets the condition(s) separated by "and" or "or"
It is not allowed to use more than operator for logic. a1=20+30 is not allowed.
Range Functions (numbers)
Functions that generates a range of values
ramp($fromX, $toX, $fromY, $toY)
It generates a ramp values (lineal values)
log($startX,$startY,$scale=1)
It generates log values
->gen('when _index<200 then idtable.value=log(0,0,100)')
exp($startX,$startY,$scale=1)
It generates exponential values. The scale is for division of Y
->gen('when _index<200 then idtable.value=exp(0,0,10)')
sin($startX,$startY,$speed=1,$scale=1,$angle=null)
It generates a sinusoid values. The angle
- startX is the position of the X
- startY is the position of the Y (you can move up and down the values)
- Speed is the speed of the angle.
- For example sin(0,0,1,1), every _index value counts as a degree.
- For example sin(0,0,2,1), every _index value counts as x2 degree.
- scale is the scale (Y) of the chart.
- angle, if it is not set then it is calculated with the current index x the speed (in degree). If not, then it is used to determine the angle of the sine value
atan($centerX,$startY,$speed=1,$scale=1)
It generates arc-tangent values
parabola($centerX,$startY,$scaleA=1,$scaleB=1,$scale=1)
It generates a parabola. It is possible to invert the parabola by changing the scaleA by negative
bell($centerX, $startY, $sigma=1, $scaleY=1)
It generates a bell values, sigma is the "wide" of the bell.
Fixed functions (numbers)
Functions that generates a single value
randomprop(...$args)
It generates a random value by using different proportions or probabilities.
randomprop(1,2,3,30,50,20)
- there is 30% for 1
- there is 50% for 2
- there is 20% for 3
Example:
random($from,$to,$jump=1,...$probs)
It generates a random value from $from to $to.
Optionally, you could add a probability for each segment.
random(0,100,1,10,20,70)
- There is a 10% probability the random value is between 0 and 33
- There is a 20% probability the random value is between 34 and 66
- There is a 70% probability the random value is between 67 and 100
idtable.value=random(0,200,1,80,10,10) // values trends to stay at the bottom
idtable.value=random(0,200,1,10,80,10) // values trends to stay at the center
idtable.value=random(0,200,1,10,10,80) // values trends to stay at the top
You can also use the next name of values
name | values equivalents |
---|---|
fakebell | [10, 25, 30, 25, 10] |
fakebell2 | [15, 22, 26, 22, 15] |
fakebell3 | [5, 15, 60, 15, 5] |
rightbias | [5, 10, 20, 35, 30] |
leftbias | [30, 35, 20, 10, 5] |
flat | [20, 20, 20, 20, 20] |
up | [10, 20, 30, 40, 50] |
down | [50, 40, 30, 20, 10] |
sine | [10, 30, 10, 30, 10] |
Example:
field.speed=xxxx
It sets the speed of the field. If the field has speed then it's value it's increased for each interaction.
Example:
field.accel=xxxx
It sets the acceleration of a field. If the field has acceleration then it's speed it's increased for each iteration.
Example:
field.value=xxxx
It sets the value of a field
Example:
field.getvalue
It returns the value of a field
Example:
field.valueabs
It transforms the value of a field to absolute (always positive)
Example:
field.day , field.month , field.year, field.hour, field.minute, field.weekday
It returns the current part of the date (day,month,year,hour,minute and weekday).
It is used for field of the type datetime. It is not for set the value, it is only for get the value, no matter the value. Weekday: 1= monday, 7=sunday
Month: 1 = january, 12= december
Example:
field.stop=xxxx
It sets or returns the value of a field, it also marks the speed and acceleration to zero.
field.add=xxxx
It adds a value to a field. If the field is of the type datetime, then you could add an "hour","minute" and "day"
field.concat=xxxx
It concatenates a value to a field.
field.skip=xxx
It skips a value to the next value. It is used for date.
value |
---|
hour |
day |
month |
monday |
tuesday |
wednesday |
thursday |
friday |
saturday |
sunday |
Arrays and texts
->setArray('arrayname',[])
It sets an array. If the array is associative, then the value is the probability of selection.
Note: arrays and variables share the same space of memory, so if we have a variable and array with the same value, then one of them will be overridden.
randomarray("arrayname",'field'=null)
it returns a random row inside the array declared with setArray(). If the array is associative then it returns a value according its probability.
If the array is a list of objects, then it returns the value of the field.
Example:
->setFormat('formatName',[])
It sets a format (template) to merge different arrays.
This function is used in tandem with randomformat()
The arrays are marked as {{name-of-the-array}}. If the array is not defined then it returns the value of a field.
If the array is associative then it returns a value according its probability.
Example:
randomformat($nameFormat)
It generates a random text using a mixes of format and different arrays.
Example:
randomtext($starting,$arrayName,$paragraph,$wordMinimum,$wordMaximum)
It generates a random text by using the array with name $arrayName. The text could start with a default text.
If $paragraph is not 0, then it could generate paragraph (line carriage)
If arrayName is empty then it uses an array with "lorem ipsum" words.
randommask($mask,$arrayName='')
It generates a text based on a mask
- # = a (optional) random digit
- 0 = a random digit.
- u = An uppercase letter
- l = A lowercase letter
- v = An upper or lowercase letter.
- w = An random letter (upper or lowercase) or a digit.
- X = An optional letter (uppercase)
- x = An optional letter (lowercase)
- ? = A random word from an array or a format.
- \ = Escape character, (the next character will be returned without processing)
randommask("##-00 uu ll \0 - oo (a)","lastName") // where lastName is an array
Example:
Other Features
end
It ends the current run. It is useful when you want to stop inserting values.
Example:
omit
It omits the current loop. It is useful when you want to skip some values.
Example:
See skip to omit dates
File features
arrayFromFolder()
It reads a folder and returns an array of files. The reading is not recursive, and it could be filtered by extension.
$filesWithoutExtension=$chaos->arrayFromFolder($localfolder,'jpg',false);
Script function destionationArray.copyfilefrom()=originArray.getvalue
It copies a file from a location to another.
Database
This library allows to interact directly with the database, reading and inserting information.
For example, let's say the next exercise:
table($table, $conditions,$prefix='origin_')
It sets the working table and the number of values to process.
If you are using a database, then table is used to determine where the values will be inserted.
If you are not using a database, then table is only for reference.
- $table = it is the name of the table
- $conditions (int) = It could indicate the number of rows to generate.
- $conditions (array) = It indicates the values to iterates.
- $conditions (string) = it indicates the table (or query) to use as "origin" of values. You can use an inner join of queries but the query must returns unique columns.
- $prefix (string) = it sets a prefix value.
The value of the query is stored in the fields called : origin_name of the column
update($table,$indexcolumn,$indexvalue,$updatecolumn1,$updatevalue1...)
Update a simple row of a table. You can update up to 3 values.
Example:
insert($storeCache=false,$echoProgress=null,$continueOnError=false,$maxRetry=3)
Insert random values into the database.
Note: This method is deprecated. Use instead setInsert()
- $storecache : if true then it inserts a value, and it stores its value into memory.
- $echoProgress : (printf format) if it is not empty then it shows the progress (echo)
- $continueOnError = if true then it continues if insert fails.
- $maxRetry = number of retries (if insert fails)
setInsert($continueOnError=false,$maxRetry=3)
It sets to insert the values into the database. This operation is executed when the command run() is executed.
- $continueOnError = if true then it continues if insert fails.
- $maxRetry = number of retries (if insert fails)
setArrayFromDBQuery($name,$query,$probability=[1],$queryParam=null)
It sets an array using a query
- $name = name of the array
- $query = source query of the original values. It must returns a single column.
- $probability = array of probability. If [1] it means 100% for each value, [80,20] means 80% for the first half values of the array and 20% for the second half.
- $queryParam = (optional) parameters of the query.
Note: arrays and variables share the same space of memory.
setArrayFromDBTable($name,$table,$column,$probability=[1])
It sets an array using a table
- $name = name of the array
- $query = source query of the original values. It must returns a single column.
- $probability = array of probability. If [1] it means 100% for each value, [80,20] means 80% for the first half values of the array and 20% for the second half.
- $queryParam = (optional) parameters of the query.
Note: arrays and variables share the same space of memory, so if you use the same name, then you can override one of them.
Examples:
Connect to Sql Server
Connect to Mysql
Generate code based in a table
Let's say that we have a table in the database called table1.
The later code will show the next values:
So you won't need to start from scratch.
This generation of code also understands foreign keys.
Insert 1000 random values
Note: Part of this code could be generated automatically.
Update all rows of a table
version
- 1.13 2022-09-17
- Added type hinting for the code.
- Cleaned the code.
- Updated requirements. Now, this library requires PHP 7.2 and higher.
- Updated library dependencies.
- 1.12 2022-01-03
- added random seed to the constructor. By default, the random seed is generated using the microseconds of the processor.
- fixed the precision of some operations
- random now allows any array of values.
- 1.11 2021-12-01
- added the method end(), omit() and update()
- updated the method sin(). Now it allows to specify the current angle.
- fixed some bugs with the process of date.
- 1.10 2021-11-22
- Some cleanups.
- 1.9 2020-08-12
- Updated a problem with the database (forever loop)
- Updated code
- Updated dependencies.
- 1.8 2020-04-09
- method insert() is deprecated. Use instead setInsert()
- new method setInsert()
- 1.7 2020-04-09 Updated Dependency eftec/pdoone (1.19 => 1.28.1)
- 1.6 Added function showTable() and show()
- 1.5 Updated MiniLang 2.12 -> 2.14
- 1.4 We could run ->insert(true)->show() at the same time. Insert(true) will keep the values (so we could show it without recalculating)
- 1.3 Now it could copy files.
New method arrayFromFolder() reads all files of a folder.
Script method field.copyfilefrom=origin copies from an origin (array of files)
And now randommask (?) wildcard works with array and formats. - 1.2 Some cleanup.
- 1.1 Now Minilib is an external library
- 1.0 First open source version
License
Dual License
LGPL-V3 and Commercial License.
Copyright Jorge Patricio Castro Castillo (2018)
All versions of chaosmachineone with dependencies
composer/installers Version ^2.2.0
eftec/minilang Version ^2.27
ext-ctype Version *
eftec/pdoone Version 3.12.2
ext-pdo Version *