Merge pull request #26 from phpfui/getters_for_function_and_operators
Getters for function and operators
This commit is contained in:
commit
aa1a092a9e
4 changed files with 64 additions and 3 deletions
10
README.md
10
README.md
|
@ -7,7 +7,15 @@ Simple math expressions calculator
|
||||||
|
|
||||||
## Install via Composer
|
## Install via Composer
|
||||||
|
|
||||||
All instructions to install here: https://packagist.org/packages/nxp/math-executor
|
Stable branch
|
||||||
|
```
|
||||||
|
composer require "nxp/math-executor" "dev-master"
|
||||||
|
```
|
||||||
|
|
||||||
|
Dev branch
|
||||||
|
```
|
||||||
|
composer require "nxp/math-executor" "dev-dev"
|
||||||
|
```
|
||||||
|
|
||||||
## Sample usage:
|
## Sample usage:
|
||||||
|
|
||||||
|
|
|
@ -51,6 +51,18 @@ class TokenFactory
|
||||||
$this->functions[$name] = array($places, $function);
|
$this->functions[$name] = array($places, $function);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* get functions
|
||||||
|
*
|
||||||
|
* @return array containing callback and places indexed by
|
||||||
|
* function name
|
||||||
|
*/
|
||||||
|
public function getFunctions()
|
||||||
|
{
|
||||||
|
return $this->functions;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Add operator
|
* Add operator
|
||||||
* @param string $operatorClass
|
* @param string $operatorClass
|
||||||
|
@ -68,6 +80,16 @@ class TokenFactory
|
||||||
$this->operators = array_unique($this->operators);
|
$this->operators = array_unique($this->operators);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get registered operators
|
||||||
|
*
|
||||||
|
* @return array of operator class names
|
||||||
|
*/
|
||||||
|
public function getOperators()
|
||||||
|
{
|
||||||
|
return $this->operators;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Add variable
|
* Add variable
|
||||||
* @param string $name
|
* @param string $name
|
||||||
|
|
|
@ -152,6 +152,16 @@ class MathExecutor
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get all registered operators to executor
|
||||||
|
*
|
||||||
|
* @return array of operator class names
|
||||||
|
*/
|
||||||
|
public function getOperators()
|
||||||
|
{
|
||||||
|
return $this->tokenFactory->getOperators();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Add function to executor
|
* Add function to executor
|
||||||
*
|
*
|
||||||
|
@ -160,13 +170,24 @@ class MathExecutor
|
||||||
* @param int $places Count of arguments
|
* @param int $places Count of arguments
|
||||||
* @return MathExecutor
|
* @return MathExecutor
|
||||||
*/
|
*/
|
||||||
public function addFunction($name, callable $function = null, $places = 1)
|
public function addFunction($name, $function = null, $places = 1)
|
||||||
{
|
{
|
||||||
$this->tokenFactory->addFunction($name, $function, $places);
|
$this->tokenFactory->addFunction($name, $function, $places);
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get all registered functions
|
||||||
|
*
|
||||||
|
* @return array containing callback and places indexed by
|
||||||
|
* function name
|
||||||
|
*/
|
||||||
|
public function getFunctions()
|
||||||
|
{
|
||||||
|
return $this->tokenFactory->getFunctions();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Execute expression
|
* Execute expression
|
||||||
*
|
*
|
||||||
|
|
|
@ -74,4 +74,14 @@ class MathTest extends \PHPUnit_Framework_TestCase
|
||||||
array('100500 * 3.5E-5')
|
array('100500 * 3.5E-5')
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
public function testFunction()
|
||||||
|
{
|
||||||
|
$calculator = new MathExecutor();
|
||||||
|
|
||||||
|
$calculator->addFunction('round', function ($arg) { return round($arg); }, 1);
|
||||||
|
/** @var float $phpResult */
|
||||||
|
eval('$phpResult = round(100/30);');
|
||||||
|
$this->assertEquals($calculator->execute('round(100/30)'), $phpResult);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue