Download the PHP package freezewarp/php-shunting-yard without Composer
On this page you can find all versions of the php package freezewarp/php-shunting-yard. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download freezewarp/php-shunting-yard
More information about freezewarp/php-shunting-yard
Files in freezewarp/php-shunting-yard
Package php-shunting-yard
Short Description Flexible shunting yard parser, based on https://github.com/andig/php-shunting-yard
License MIT
Homepage https://github.com/FreezeWarp/php-shunting-yard
Informations about the package php-shunting-yard
PHP Shunting Yard Implementation
Fork Changes
The general goal of this fork is to take the existing framework for safe formula evaluation and add the needed functionality for more general inputs (whereas the original is fairly heavily focused on numbers alone).
To this end, the following changes are made:
-
Constants can be any PHP value, not just numeric values. (Internally, they are represented as
T_NATIVE
, and subsumeT_NULL
.) -
Support for string literals has been added.
- The
||
operator was added for concatenation. (This operator is given precedence above equality but below addition and subtraction. Thus,2 + 3 || 3 + 4 = "57"
.) - The
+
operator acts as concatenation if either side is non-numeric. (Thus,"2" + "3" = 5
, but"2 " + "3" = "2 3"
) - String literals are supported -- either as
"string"
or'string'
. (Escaping is not supported.)
- The
-
Support for array literals has been added.
- The
||
operator will perform array merging if either operand is an array. - The
in
operator can be used to check the existence of an array member. Anot in
operator still needs to be added. - Both lists and associative arrays are implemented. Lists use the syntax
[1, 2, 3]
, while associative arrays use the syntax[1 -> 2, 3 -> 4, 5 -> 6]
. - Array keys and values support all operations otherwise supported by shunting expressions -- for instance,
[1 + 2 -> 3 + 4, 5 + 6 -> 7 + 8]
is equivalent to[3 -> 7, 11 -> 15]
- The
-
Variable names have been expanded:
- Now, any string matching
/\p{L}\p{N}\.]+/
(that is, containing only unicode letters, numbers, and the symbol.
) will be treated as a variable name. This means thatlittérature + 手紙
is a valid formula that is adding two variables. - A special syntax,
${xyz}
is allotted for more complex variable references -- this supports any character in the variable name other than}
. - Unregistered variables evaluate to 0 instead of causing an exception. A strict mode flag on Context can re-enable the old behaviour.
- Now, any string matching
-
End-of-line comments are supported using #
- A handful of more opinionated changes were made to make the overall syntax more user-friendly:
- No default constants are set. At construction time, an array can be passed to set all constants at once.
and
,or
, andnot
are now synonyms for&
,|
, and!
respectively.if
,coalesce
,min
, andmax
functions are registered by default.- The xor operator was removed to avoid possible confusion (it was
><
).
Unit tests have been written for all of these changes. All existing and new tests pass.
Finally, while not included, I personally recommend using this package by overloading the Context like so:
While the default class only performs normal array lookups, there are many opportunities for more advanced array lookups. Laravel's array_get provides basic dot lookups, or https://github.com/Galbar/JsonPath-PHP could be used for full JSONPath lookups.
Example
Simple equation parsing
Equation with constants and functions
Test a condition
Re-run parsed expression on multiple inputs
Installation
Define the following requirement in your composer.json file:
Authors
This is a fork of https://github.com/andig/php-shunting-yard, which in turn used code from: