'string is empty', 'unexpected_end' => 'unexpected end of string' ]; /** * Try to parse the string starting from the given position. * * @param string $source string to parse * @param int $pos position to start from * * @return int */ abstract public function parse($source, $pos = 0); /** * Returns length of the parsed element. * * @return int */ public function getLength() { return $this->length; } /** * Returns parsed element. * * @return string */ public function getMatch() { return $this->match; } /** * Returns the error message if string is invalid. * * @return string */ public function getError(): string { if ($this->error_source === false) { return ''; } if (!isset($this->error_source[$this->error_pos])) { return ($this->error_pos == 0) ? $this->error_msgs['empty'] : $this->error_msgs['unexpected_end']; } // The error message is prepared here to avoid extra calculations if the error message is not used. return $this->errorPosMessage($this->error_source, $this->error_pos); } /** * Save error source string and position for later use, when error will be retrieved. * * @param string $source * @param int $pos */ protected function errorPos($source, $pos): void { $this->error_source = $source; $this->error_pos = $pos; } /** * Clears error, when parse is used multiple times with same parser. */ protected function errorClear(): void { $this->error_source = false; $this->error_pos = 0; } /** * Prepares error message for incorrect syntax at position. * * @param string $source * @param int $pos * * @return string */ protected function errorPosMessage($source, $pos): string { $maxChunkSize = 50; $chunk = substr($source, $pos); if (mb_strlen($chunk) > $maxChunkSize) { $chunk = mb_substr($chunk, 0, $maxChunkSize) . ' ...'; } return _s('incorrect syntax near "%1$s"', $chunk); } }