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
|
||||
|
||||
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:
|
||||
|
||||
|
|
|
@ -51,6 +51,18 @@ class TokenFactory
|
|||
$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
|
||||
* @param string $operatorClass
|
||||
|
@ -68,6 +80,16 @@ class TokenFactory
|
|||
$this->operators = array_unique($this->operators);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get registered operators
|
||||
*
|
||||
* @return array of operator class names
|
||||
*/
|
||||
public function getOperators()
|
||||
{
|
||||
return $this->operators;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add variable
|
||||
* @param string $name
|
||||
|
|
|
@ -152,6 +152,16 @@ class MathExecutor
|
|||
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
|
||||
*
|
||||
|
@ -160,13 +170,24 @@ class MathExecutor
|
|||
* @param int $places Count of arguments
|
||||
* @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);
|
||||
|
||||
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
|
||||
*
|
||||
|
|
|
@ -74,4 +74,14 @@ class MathTest extends \PHPUnit_Framework_TestCase
|
|||
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