| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * Calc.php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * @date 28.03.2015 0:54:24 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * @copyright Sklyarov Alexey <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | namespace Sufir\Calc; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use Closure; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use InvalidArgumentException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use ReflectionObject; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use RuntimeException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use SplStack; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use Sufir\Calc\Token; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | use Sufir\Calc\Token\FunctionToken; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | use Sufir\Calc\Token\NumberToken; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | use Sufir\Calc\Token\VariableToken; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  * Calc | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  * Description of Calc | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  * @author Sklyarov Alexey <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  * @package Sufir\Calc | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  | final class Calc | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     private $functions = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     private $variables = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |      * @param integer $scale | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 38 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 39 | 17 |  |     public function __construct($scale = 5) | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 41 | 17 |  |         bcscale($scale); | 
            
                                                                        
                            
            
                                    
            
            
                | 42 | 17 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |      * @param Token $tokens | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |      * @param array $variables | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |      * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |      * @throws RuntimeException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 | 14 |  |     public function evaluate(array $tokens, array $variables = []) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 | 14 |  |         $stack = new SplStack; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 | 14 |  |         $this->defineVars($variables); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 | 14 |  |         foreach ($tokens as $token) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 | 14 |  |             if ($token->isNumber()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 | 14 |  |                 $stack->push($token); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 | 14 |  |             } elseif ($token->isVariable()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 | 4 |  |                 if (!isset($this->variables[$token->getValue()])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 | 1 |  |                     throw new RuntimeException( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 | 1 |  |                         "Undefined variable: «{$token}»" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 | 1 |  |                     ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 | 3 |  |                 $stack->push(new NumberToken($this->variables[$token->getValue()])); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 | 14 |  |             } elseif ($token->isOperator()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 | 12 |  |                 if ($stack->count() > 1) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 | 12 |  |                     $second = $stack->pop(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 | 12 |  |                     $first = $stack->pop(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 | 12 |  |                     $result = self::math($token->getValue(), $first->getValue(), $second->getValue()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 | 12 |  |                     $stack->push(new NumberToken($result)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 | 12 |  |                 } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 | 1 |  |                     throw new RuntimeException( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 | 1 |  |                         "Not enough operands for a binary operator: «{$token}»" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 | 1 |  |                     ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 | 14 |  |             } elseif ($token->isFunction()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 | 9 |  |                 if (!isset($this->functions[$token->getValue()])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 | 1 |  |                     throw new RuntimeException("Undefined function: «{$token}()»"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 | 8 |  |                 $countOfArguments = $this->functions[$token->getValue()]['args']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 | 8 |  |                 if ($stack->count() < $countOfArguments) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 | 1 |  |                     throw new RuntimeException( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 | 1 |  |                         "Wrong argenents for function «{$token}()»:" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 | 1 |  |                         . "defined {$stack->count()} expected {$countOfArguments}" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 | 1 |  |                     ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 | 7 |  |                 $arguments = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 | 7 |  |                 for ($idx = 0; $idx < $countOfArguments; $idx++) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 | 4 |  |                     $arguments[] = $stack->pop()->getValue(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 | 4 |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 | 7 |  |                 $arguments = array_reverse($arguments); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 | 7 |  |                 $result = call_user_func_array($this->functions[$token->getValue()]['func'], $arguments); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 | 7 |  |                 if (!is_numeric($result)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 | 1 |  |                     throw new RuntimeException( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 | 1 |  |                         "Wrong result type of «{$token->getValue()}()» function, expected integer or float" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 | 1 |  |                     ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 | 6 |  |                 $stack->push(new NumberToken($result)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 | 6 |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 | 14 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 | 9 |  |         return $stack->top()->getValue(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |      * Register new function. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |      * @param string $name | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |      * @param Closure $callable | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |      * @return \Sufir\Calc\Calc | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 | 9 |  |     public function registerFunction($name, Closure $callable) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 | 9 |  |         $name = rtrim($name, " \t\n\r\0\x0B\(\)"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 | 9 |  |         if (!FunctionToken::validate($name)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 | 1 |  |             throw new InvalidArgumentException("Wrong function name «{$name}»"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 | 8 |  |         $objReflector = new ReflectionObject($callable); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 | 8 |  |         $reflector = $objReflector->getMethod('__invoke'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 | 8 |  |         $parameters = $reflector->getParameters(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 | 8 |  |         $this->functions[$name] = array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 | 8 |  |             'args' => count($parameters), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 | 8 |  |             'func' => $callable, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 | 8 |  |         return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |      * Define new variable. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |      * @param string $name | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |      * @param integer $value | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |      * @return \Sufir\Calc\Calc | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |      * @throws RuntimeException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 | 5 |  |     public function defineVar($name, $value) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 | 5 |  |         $name = '$' . ltrim($name, " \t\n\r\0\x0B\$"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 | 5 |  |         if (!VariableToken::validate($name)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 | 1 |  |             throw new InvalidArgumentException("Wrong variable name «{$name}»"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 | 4 |  |         if (!is_numeric($value)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 | 1 |  |             throw new InvalidArgumentException( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 | 1 |  |                 "Wrong type of «{$name}», expected integer or float" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 | 1 |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 | 3 |  |         $this->variables[$name] = $value; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 | 3 |  |         return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  |      * Define new variables. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 |  |  |      * <br> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 |  |  |      * Array format ['varName' => 'varValue', ...] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 |  |  |      * @param array $variables | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 |  |  |      * @return \Sufir\Calc\Calc | 
            
                                                                                                            
                            
            
                                    
            
            
                | 178 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 179 | 14 |  |     public function defineVars(array $variables = []) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 180 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 181 | 14 |  |         foreach ($variables as $name => $value) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 182 | 3 |  |             $this->defineVar($name, $value); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 183 | 14 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 184 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 185 | 14 |  |         return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 186 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 187 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 188 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 189 |  |  |      * @param string $operator | 
            
                                                                                                            
                            
            
                                    
            
            
                | 190 |  |  |      * @param integer|float $firstOperand | 
            
                                                                                                            
                            
            
                                    
            
            
                | 191 |  |  |      * @param integer|float $secondOperand | 
            
                                                                                                            
                            
            
                                    
            
            
                | 192 |  |  |      * @return string|null | 
            
                                                                                                            
                            
            
                                    
            
            
                | 193 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 194 | 12 |  |     private static function math($operator, $firstOperand, $secondOperand) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 195 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 196 |  |  |         switch ($operator) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 197 | 12 |  |             case '^': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 198 | 4 |  |                 return bcpow($firstOperand, $secondOperand); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 199 | 12 |  |             case '*': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 200 | 9 |  |                 return bcmul($firstOperand, $secondOperand); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 201 | 5 |  |             case '/': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 202 | 3 |  |                 return bcdiv($firstOperand, $secondOperand); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 203 | 5 |  |             case '+': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 204 | 5 |  |                 return bcadd($firstOperand, $secondOperand); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 205 | 4 |  |             case '-': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 206 | 4 |  |                 return bcsub($firstOperand, $secondOperand); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 207 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 208 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 209 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 210 |  |  |  |