public function process() { $returnValue = new QtismInteger(0); $operands = $this->getOperands(); if (count($operands) >= 2) { $points = $operands[0]; $equation = $operands[1]; if (($points instanceof MultipleContainer || $points instanceof OrderedContainer) && ($points->getBaseType() === BaseType::POINT || $points->getBaseType() === BaseType::STRING) && $equation instanceof QtismString) { // Check every Point X,Y against the equation... $math = new \oat\beeme\Parser(); $math->setConstant('#pi', M_PI); try { foreach ($points as $point) { if ($point instanceof QtiPoint) { $x = floatval($point->getX()); $y = floatval($point->getY()); } else { $strs = explode(" ", $point->getValue()); if (count($strs) !== 2) { // Parsing error, the NULL value is returned. return null; } else { $x = floatval($strs[0]); $y = floatval($strs[1]); } } $result = $math->evaluate($equation->getValue(), array('x' => $x, 'y' => $y)); if ($result === true) { // The Point X,Y satisfies the equation... $returnValue->setValue($returnValue->getValue() + 1); } } } catch (\Exception $e) { // If an error occurs e.g. invalid expression, the NULL value is returned. return null; } } else { // Not supported operands, return the NULL value. return null; } } return $returnValue; }
<?php /** * Everything is relative to the application root now. */ chdir(dirname(__DIR__)); require 'vendor/autoload.php'; $parser = new \oat\beeme\Parser(); $expression = '1 + 2 * 3 * ( 7 * 8 ) - ( 45 - 10 )'; $result = $parser->evaluate($expression); printf('%s => %f;%s', $expression, $result, PHP_EOL); $expression = '11 - -2 * -3 * ( 17 * 81 ) - ( -45 - 10 )'; $result = $parser->evaluate($expression); printf('%s => %f;%s', $expression, $result, PHP_EOL); $expression = '-1 + -2 * 13 * ( 7 * 8 ) - ( 415 - 0.1 )'; $result = $parser->evaluate($expression); printf('%s => %f;%s', $expression, $result, PHP_EOL); $expression = '3 + x'; $result = $parser->evaluate($expression, ['x' => 3]); printf('%s => %f;%s', $expression, $result, PHP_EOL); $expression = '1 + abs(x)'; $result = $parser->evaluate($expression, ['x' => -10]); printf('%s => %f;%s', $expression, $result, PHP_EOL);