container = new Container($serviceRegistry); } else { if (! is_object($container)) { throw new \InvalidArgumentException('Error: Passed container parameter is not an object'); } $interfaces = class_implements($container); if (! in_array(ContainerInterface::class, $interfaces)) { throw new \InvalidArgumentException('Error: Passed container does not implement ContainerInterface'); } $this->container = $container; } $this->symbolContainer = $this->container->get('stringcalc_symbolcontainer'); } /** * Calculates a term and returns the result. * Will return 0 if there is nothing to calculate. * * @param string $term * @return float|int * @throws Exceptions\ContainerException * @throws Exceptions\NotFoundException */ public function calculate($term) { $tokens = $this->tokenize($term); if (sizeof($tokens) == 0) { return 0; } $rootNode = $this->parse($tokens); if ($rootNode->isEmpty()) { return 0; } $calculator = $this->container->get('stringcalc_calculator'); return $calculator->calculate($rootNode); } /** * Tokenize the term. Returns an array with the tokens. * * @param string $term * @return array * @throws Exceptions\ContainerException * @throws Exceptions\NotFoundException */ public function tokenize($term) { $stringHelper = $this->container->get('stringcalc_stringhelper'); $stringHelper->validate($term); $term = strtolower($term); /** @var InputStreamInterface $inputStream */ $inputStream = $this->container->get('stringcalc_inputstream'); $inputStream->setInput($term); /** @var StringHelperInterface $stringHelper */ $stringHelper = $this->container->get('stringcalc_stringhelper'); $tokenizer = new Tokenizer($inputStream, $stringHelper); return $tokenizer->tokenize(); } /** * Parses an array of tokens. Returns a single node that is a container node. * * @param Token[] $tokens * * @return ContainerNode */ public function parse(array $tokens) { $parser = new Parser($this->symbolContainer); if ($this->customGrammarChecker !== null) { $parser->setCustomGrammarChecker($this->customGrammarChecker); } return $parser->parse($tokens); } /** * Adds a symbol to the list of symbols. * This is just a shortcut method. Call getSymbolContainer() if you want to * call more methods directly on the symbol container. * * @param AbstractSymbol $symbol The new symbol object * @param string|null $replaceSymbol Class name of a known symbol that you want to replace * @return void */ public function addSymbol(AbstractSymbol $symbol, $replaceSymbol = null) { $this->symbolContainer->add($symbol, $replaceSymbol); } /** * Setter for the custom grammar checker property. * * @see Parser::setCustomGrammarChecker() * * @param \Closure $customGrammarChecker */ public function setCustomGrammarChecker(\Closure $customGrammarChecker) { $this->customGrammarChecker = $customGrammarChecker; } /** * Removes the custom grammar checker. */ public function unsetCustomGrammarChecker() { $this->customGrammarChecker = null; } /** * Getter for the symbol container * * @return SymbolContainerInterface */ public function getSymbolContainer() { return $this->symbolContainer; } /** * Getter for the service container * * @return ContainerInterface */ public function getContainer() { return $this->container; } }