phpOMS/Math/Parser/Evaluator.php

56 lines
1.2 KiB
PHP

<?php
/**
* Orange Management
*
* PHP Version 7.2
*
* @package TBD
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link http://website.orange-management.de
*/
declare(strict_types=1);
namespace phpOMS\Math\Parser;
/**
* Shape interface.
*
* @package Framework
* @license OMS License 1.0
* @link http://website.orange-management.de
* @since 1.0.0
*/
class Evaluator
{
/**
* Evaluate function.
*
* Example: ('2*x^3-4x', ['x' => 99])
*
* @param string $formula Formula to differentiate
* @param array $vars Variables to evaluate
*
* @return float
*
* @throws \Exception
*
* @since 1.0.0
*/
public static function evaluate(string $formula, array $vars) : float
{
// todo: do i need array_values here?
$formula = \str_replace(array_keys($vars), array_values($vars), $formula);
// todo: this is horrible in case things like mod etc. need to be supported
if (\preg_match('#[^0-9\+\-\*\/\(\)]#', $formula)) {
throw new \Exception('Bad elements');
}
// todo: create parser
return 0;
}
}