timo-lehnertz / formula
An open source PHP formula parser based on https://github.com/socialist/formula
Requires
- php: >=8.1
Requires (Dev)
- phpunit/phpunit: 9.5.*
- psalm/phar: 5.23
- dev-master
- dev-dev-master
- v2.0.53
- v2.0.52
- v2.0.51
- v2.0.50
- v2.0.49
- v2.0.48
- v2.0.47
- v2.0.46
- v2.0.45
- v2.0.44
- v2.0.43
- v2.0.42
- v2.0.41
- v2.0.40
- v2.0.39
- v2.0.38
- v2.0.37
- v2.0.36
- v2.0.35
- v2.0.34
- v2.0.33
- v2.0.32
- v2.0.31
- v2.0.30
- v2.0.29
- v2.0.28
- v2.0.27
- v2.0.26
- v2.0.25
- v2.0.24
- v2.0.23
- v2.0.22
- 2.0.21
- 2.0.20
- v2.0.19
- v2.0.18
- v2.0.17
- v2.0.16
- v2.0.15
- v2.0.14
- v2.0.13
- v2.0.12
- v2.0.11
- v2.0.10
- v2.0.9
- v2.0.8
- v2.0.7
- v2.0.6
- v2.0.5
- v2.0.4
- v2.0.3
- v2.0.2
- 1.0.46
- 1.0.45
- 1.0.44
- 1.0.43
- 1.0.42
- 1.0.41
- 1.0.39
- 1.0.38
- 1.0.37
- 1.0.36
- 1.0.35
- 1.0.34
- 1.0.33
- 1.0.32
- 1.0.31
- 1.0.30
- 1.0.29
- 1.0.28
- 1.0.27
- 1.0.26
- 1.0.25
- 1.0.24
- 1.0.23
- 1.0.22
- 1.0.20
- 1.0.19
- 1.0.18
- 1.0.17
- 1.0.15
- 1.0.14
- 1.0.13
- 1.0.12
- 1.0.11
- 1.0.10
- 1.0.8
- 1.0.7
- 1.0.6
- 1.0.5
- 1.0.4
- 1.0.3
- 1.0.2
- 1.0.1
- 1.0.0
- dev-formula2
This package is auto-updated.
Last update: 2024-12-22 15:25:59 UTC
README
An open source PHP formula parser based on https://github.com/socialist/formula
Features
- Variables
- User defined functions
- Vectors
- Vectormath
- PHP DateTime
- PHP DateInterval
Installation
For an installation using composer add this to you composer.json
"require": { "timo-lehnertz/formula": "^2.0" }
Usage
$formula = new Formula("10*a+func(b,5)"); $formula->setVariable("a", 1); $formula->setVariable("b", 2); $formula->setMethod("max", [$this, "func"]); $result = formula->calculate();
Table of Contents
[TOCM]
[TOC]
Operators
All standart php operators are supported. +-*/
and all logical operators.
Operators respect the mathematical rules. The multiplication operator can be left out between distinct expressions.
Ternary operator
Ternary operator is supported in the form of
a ? b : c
Truthy values
Everything that is not equal to 0 is considered a truthy value
Methods
You can call methods inside of a formula string and give them a definition in the in the formula object. All user defined methodsshould return something. The returned values get parsed into either a number, or a string.
Strings
Strings are supported in the form of "a string"
or 'a string'
. Strings are all truthy by default and dont really serve any purpose other than beeing a parameter for functions.
Dates
Formula supportes DateTimeImmutable and DateInterval. To define those use a string containing a Date or string Example:
- DateTime:
"2022-11-25T23:05:47+0100"
- DateInterval:
P1M
(interval of one month) All php date formats are supported. Check out this site for a list of DateTime formats: https://www.php.net/manual/en/class.datetime.php Here's a list for DateIntervals: https://www.php.net/manual/en/class.dateinterval.php Internally all dates and intervals get parsed to the UNIX timestamp for easier calculation. So if a date gets passed as parameter to a method the method will receive the timestamp and should also return a timestamp is thats what its purpose is.
Vectors
Vectors are defined like {1,2,a,b,someMethod()}
.
All math operations work on vectors of the same size or a vector and a number. All Vectors are truthy.
array indices
Array indices can be accsessed like {1,2,3}[0]
arrays and functions
Arrays passed to a method will be received by the php function just like a normal array.
Arrays Returned from a php function will be translated to a formula array.
The inbuild methods sizeof(<Vector>)
and asVector(...args)
can halp working with vectors. Also the min
and max
methods are designed to be used with Vectors. They will search recursivly for the min or max value.
Pre defined methods
Those methods are predefined and ready to use in any formula script
php functions
min
max
sqrt
pow
floor
ceil
round
sin
cos
tan
is_nan
abs
All these are linked to their PHP counterpart and act just like php functions
Additional methods
asVector(...element)
sizeof(<Vector>)
Other methods
inRange(value, min, max)
returns true if value is between min and max (including)reduce(valueArray, filterArray)
returns the an array containing all elements that occour both in valueArray and filterArrayfirstOrNull(array)
returns the first element of the array or null