Chris@0: Chris@0: * Chris@0: * For the full copyright and license information, please view the LICENSE Chris@0: * file that was distributed with this source code. Chris@0: */ Chris@0: Chris@0: namespace Symfony\Component\CssSelector\Parser; Chris@0: Chris@0: /** Chris@0: * CSS selector reader. Chris@0: * Chris@0: * This component is a port of the Python cssselect library, Chris@0: * which is copyright Ian Bicking, @see https://github.com/SimonSapin/cssselect. Chris@0: * Chris@0: * @author Jean-François Simon Chris@0: * Chris@0: * @internal Chris@0: */ Chris@0: class Reader Chris@0: { Chris@0: private $source; Chris@0: private $length; Chris@0: private $position = 0; Chris@0: Chris@0: /** Chris@0: * @param string $source Chris@0: */ Chris@0: public function __construct($source) Chris@0: { Chris@0: $this->source = $source; Chris@17: $this->length = \strlen($source); Chris@0: } Chris@0: Chris@0: /** Chris@0: * @return bool Chris@0: */ Chris@0: public function isEOF() Chris@0: { Chris@0: return $this->position >= $this->length; Chris@0: } Chris@0: Chris@0: /** Chris@0: * @return int Chris@0: */ Chris@0: public function getPosition() Chris@0: { Chris@0: return $this->position; Chris@0: } Chris@0: Chris@0: /** Chris@0: * @return int Chris@0: */ Chris@0: public function getRemainingLength() Chris@0: { Chris@0: return $this->length - $this->position; Chris@0: } Chris@0: Chris@0: /** Chris@0: * @param int $length Chris@0: * @param int $offset Chris@0: * Chris@0: * @return string Chris@0: */ Chris@0: public function getSubstring($length, $offset = 0) Chris@0: { Chris@0: return substr($this->source, $this->position + $offset, $length); Chris@0: } Chris@0: Chris@0: /** Chris@0: * @param string $string Chris@0: * Chris@0: * @return int Chris@0: */ Chris@0: public function getOffset($string) Chris@0: { Chris@0: $position = strpos($this->source, $string, $this->position); Chris@0: Chris@0: return false === $position ? false : $position - $this->position; Chris@0: } Chris@0: Chris@0: /** Chris@0: * @param string $pattern Chris@0: * Chris@0: * @return array|false Chris@0: */ Chris@0: public function findPattern($pattern) Chris@0: { Chris@0: $source = substr($this->source, $this->position); Chris@0: Chris@0: if (preg_match($pattern, $source, $matches)) { Chris@0: return $matches; Chris@0: } Chris@0: Chris@0: return false; Chris@0: } Chris@0: Chris@0: /** Chris@0: * @param int $length Chris@0: */ Chris@0: public function moveForward($length) Chris@0: { Chris@0: $this->position += $length; Chris@0: } Chris@0: Chris@0: public function moveToEnd() Chris@0: { Chris@0: $this->position = $this->length; Chris@0: } Chris@0: }