Beispiel #1
0
 /**
  * Calculate the expression.
  *
  * @param string $expression
  *
  * @throws InvalidArgumentException
  * @throws LogicException
  *
  * @return int
  */
 public function calculate($expression)
 {
     // Check for invalid expression.
     if (!preg_match('/^([0-9]+[' . $this->operatorsRegexp . '])*[0-9]+$/', $expression)) {
         throw new InvalidArgumentException('Invalid expression! Bad characters used.');
     }
     // If its only a number, then it is the result.
     if (is_numeric($expression)) {
         return (int) $expression;
     }
     // Complex expression:
     // - split at every operator,
     // - store the first part,
     // - rerun the method for the second part,
     // - do the operation with the two parts
     $matches = array();
     if (preg_match('/^([0-9]+)([' . $this->operatorsRegexp . '])(.*)/', $expression, $matches)) {
         return $this->math->runOperation($matches[2], $matches[1], $this->calculate($matches[3]));
     } else {
         throw new LogicException('There is no way this could happen! (At least in theory.)');
     }
 }
Beispiel #2
0
 /**
  * @param string $operator
  *
  * @dataProvider invalidOperatorDataProvider
  * @expectedException \InvalidArgumentException
  */
 public function testOperationWithInvalidOperator($operator)
 {
     $this->math->runOperation($operator, 1, 1);
 }