Download the PHP package chriskonnertz/string-calc without Composer

On this page you can find all versions of the php package chriskonnertz/string-calc. 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 string-calc

StringCalc 2

Build Status GitHub license Source Version Version

StringCalc is a zero-dependency PHP calculator library for mathematical terms (expressions) passed as strings.

Installation

Through Composer:

This library requires PHP 8.0 or higher. For PHP 5.6 support, use StringCalc 1.

Usage example

Here is a minimalistic example of PHP code that calculates a term. It assumes that there is an autoloader.

There is an interactive PHP demo script included. It is located at dev/demo.php.

Motivation

Imagine you are building a web application that allows users to enter mathematical terms in a text field. How would you calculate the results of such terms? PHP's eval function is not the answer. The official documentation recommends not to use this function: The eval() language construct is very dangerous because it allows execution of arbitrary PHP code. Its use thus is discouraged. StringCalc has its own parser implementation and therefore is a better and save alternative. StringCalc follows modern coding principles, is extensible and well documented. If you have any suggestions how to improve this library, feel free to discuss them in the issue section.

The term

Example, we need an example! Here we go: 2*(pi-abs(-0.4))

This is a mathematical term following syntactical and grammatical rules that StringCalc understands. Syntax and grammar of these terms are very similar to what you would write in PHP code. To be more precise, there is an intersecting set of syntactical and grammatical rules. There are some exceptions, but usually you will be able to write terms for StringCalc by pretending that you are writing PHP code.

Term examples

Here are some unspectacular examples:

Here is a list that shows examples with more exotic syntax:

To see a list of all available types of mathematical symbols (parts of a term) follow this link: Symbols/Concrete classes

List of symbols

Operators: `

Functions: `

Constants: `

Others: `

The StringCalc class

The StringCalc is the is the API frontend of the StringCalc library. This section describes the public methods of this class.

Constructor

The constructor has one optional parameter named $container that implements the Container\ContainerInterface. This is the service container used by StringCalc. If no argument is passed, the constructor will create a new container object with the type Container\Container on its own. The container interface ensures that the container implements the PSR-11 standard. Therefore, you can replace the default container with every other container that implements the PSR-11 standard, but you have to wrap it in a wrapper class that makes it compatible to the Container\Container class. We recommend avoiding the extra afford and to extend the Container\Container class instead.

calculate

The calculate() method is the most important method of this class. It expects one parameter with the type string called $term. It returns a number with the type float or int. We strongly recommend wrapping any calls of this method in a try-catch block and to write a catch statement that catches all exceptions with the type Exceptions\StringCalcException.

In the example an exception with class StringCalcException will be thrown, because the min method has to be called with one parameter at least. Exceptions with the type StringCalcException are usually thrown when grammar or syntax of a given term are invalid. They have two additional properties: position is an integer telling you where the problem occurred in the term, and subject is a string that might contain additional data, especially raw user input that you should never print to a website without encoding it via htmlentities()!

tokenize

The tokenize($term) method tokenizes the passed term. It returns an array with the tokens. Tokens are the parts of a term or to be more precise the mathematical symbols of a term. A token is an object that has the Tokenizer\Token class as its parent. It implements the __toString() method, so you can do something like this:

This will print the tokens of the term aka a string representation the whole term. A token consists of three properties: The value, the type and the position. The value is returned by the __toString() method. The type is a constant that represents one of these types: characters, words or numbers. The position is the index of the value string in the term string. Tokens do not have a semantic meaning.

parse

The parse(array $tokens) method parses an array of tokens. It returns an array of nodes. Internally it uses the parser aka Parser\Parser to parse the tokens. It transforms the tokens to nodes of the syntax tree. These nodes have a semantic meaning, for example they are numbers or operators (take a look at the Types of symbols section for a full list of symbol types). Also they have a hierarchy, also known as the "tree" in the "syntax tree". Brackets in a term create a node in the syntax tree.

Usage example:

This example code will visualize the syntax tree. It uses the traverse(Closure $callback) method to go through all nodes of the tree. The level of the node is visualized by indentation and the name of the class of the node object is printed to display the type of the node. A node implements the abstract Parser\Nodes\AbstractNode class. There are three types of nodes: Container nodes (representing what is inside brackets), function nodes (representing a mathematical function and its arguments) and symbol nodes that represent mathematical symbols of certain types (numbers, operators, ...). These classes live in the Parser\Nodes namespace.

addSymbol

Call the addSymbol() method if you want to add custom symbols to the list of symbols. It has two parameters. The first one named $symbol is the symbol object. Therefore the object has to extend the abstract class Symbol\AbstractSymbol. The second parameter named $replaceSymbol is optional and allows you to replace a symbol in the symbol list. If you want to use this parameter you have to pass the full name of the class that you want to replace.

Example:

The addSymbol() method is just a shortcut, you can call this method on the symbol container object as well. This object also has a remove method that removes a symbol from the container.

If you want to add a new symbol, it cannot directly extend from the Symbol\AbstractSymbol class but has to extend one of the abstract symbol type classes that extend the Symbol\AbstractSymbol class. The reason for this constraint is that these classes have a semantic meaning that is not implemented in the classes themselves but in other classes such as the tokenizer and the parser. Take a look at the Types of symbols section to familiarize with the symbol type classes.

getSymbolContainer

The getSymbolContainer() method is a getter method that returns the symbol container. The symbol container implements the Symbols\SymbolContainerInterface and contains instances of all registered symbols. It has several methods such as add(), remove(), size() and getAll().

getContainer

The getContainer() method is a getter method that returns the service container. Take a look at the notes about the constructor for more details. There is no setter method for the container, you can only set it via the constructor.

Types of symbols

A term consists of symbols that have a specific type. This section lists all available symbol types.

Numbers

Numbers in a term always consist of digits and may include exactly one period. Good usage examples:

Bad usage examples:

Just for your information: From the tokenizer's point of view, numbers in a term are always positive. This means that the tokenizer will split the term -1 in two parts: - and 1.

💡 Notice: The fractional part of a PHP float can only have a limited length. If a number in a term has a longer fractional part, the fractional part will be cut somewhere.

Number implementation

There is only one concrete number class: Symbols\Concrete\Number. It extends the abstract class Symbols\AbstractNumber. It does not implement any behaviour. It is basically a placeholder for concrete numbers in the term.

Brackets

There are two types of brackets in a term: Opening and closing brackets. There is no other typification. For example there can be classes that implement support for parentheses () and square brackets [] but they will be treated equally. Therefore, this is a valid term even though it might not be valid from a mathematical point of view: [1+)

For every opening bracket there must be a closing bracket and vice versa. Good usage examples:

Bad usage examples:

Bracket implementation

The Symbols\AbstractBracket class is the base class for all brackets. It is extended by the abstract classes Symbols\AbstractOpeningBracket and Symbols\AbstractClosingBracket. These are extended by concrete classes: Symbols\Concrete\OpeningBracket and Symbols\Concrete\ClosingBracket. These classes do not implement behaviour.

Constants

Constants in a term typically represent mathematical constants, for example pi.

Usage examples:

Constant implementation

The Symbols\AbstractConstant class is the base class for all constants. There are several concrete constants that extend this class.

Constants classes have a property called value that stores the value of the constant. It is possible to overwrite this value in a concrete constant class or to overwrite the getter method getValue().

Operators

Operators in a term can be unary or binary or even both. However, if they are unary, they have to follow the prefix notation (example: -1).

Unary operator example: -1 Binary operator example: 2-1

Operator implementation

The Symbols\AbstractOperator class is the base class for all operators. There are several concrete operators that extend this class.

Please be aware that operators are closely related to functions. Functions are at least as powerful as operators are. If an operator does not seem suitable for a purpose, a function might be an appropriate alternative.

Operator classes implement the operate($leftNumber, $rightNumber) method. Its parameters represent the operands. It might be confusing that even if the operator is a unary operator its operate method needs to offer both parameters. The $rightNumber argument will be the operand of the unary operation while the left will be 0.

Functions

Functions in a term represent mathematical functions. Typically, the textual representation of a function consists of two or more letters, for example: min

Good usage examples of using functions:

Bad usage examples:

Function implementation

The Symbols\AbstractFunction class is the base class for all functions. There are several concrete functions that extend this class.

Please be aware that operators are closely related to functions. Functions are at least as powerful as operators are. If an operator does not seem suitable for a purpose, a function should be an appropriate alternative.

Function classes implement the execute(array $arguments) method. The arguments are passed as an array to this method. The size of the arguments array can be 0-n. The implementation of this method is responsible to validate the number of arguments. If the number of arguments is improper, throw an Exceptions\NumberOfArgumentsException. Example:

The items of the $arguments array will always have the type int or float. They will never be null.

Separators

A separator is used to separate the arguments of a (mathematical) function. Out-of-the-box there is one separator symbol with one identifier: ,

Good usage examples:

Bad usage examples:

Separator implementation

The Symbols\AbstractSeparator class is the base class for all separators. There is only one concrete class that extends this class: Symbols\Concrete\Separator

Grammar

This section deals with the grammar for terms that StringCalc can process.

Grammar vs Implementation

It is important to notice that the implementations of the parser (Parser\Parser class) and the calculator (Calculator\Calculator class) do not mimic the production rules defined below exactly. So don't be irritated if you compare the actual implementation with the grammar rules.

Grammar Definition

`

Remember that numbers are always positive! The term "-1" consists of a unary operator followed by a number.

Tests

Run composer install from the StringCalc directory, then run the tests:

General notes


All versions of string-calc with dependencies

PHP Build Version
Package Version
Requires php Version >=8.0.0
ext-mbstring Version *
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 chriskonnertz/string-calc contains the following files

Loading the files please wait ....