076cb711c041a559488ad47a2409bd1c6d7dc896
[yaffs-website] / vendor / symfony / css-selector / Parser / Reader.php
1 <?php
2
3 /*
4  * This file is part of the Symfony package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.com>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 namespace Symfony\Component\CssSelector\Parser;
13
14 /**
15  * CSS selector reader.
16  *
17  * This component is a port of the Python cssselect library,
18  * which is copyright Ian Bicking, @see https://github.com/SimonSapin/cssselect.
19  *
20  * @author Jean-François Simon <jeanfrancois.simon@sensiolabs.com>
21  *
22  * @internal
23  */
24 class Reader
25 {
26     private $source;
27     private $length;
28     private $position = 0;
29
30     /**
31      * @param string $source
32      */
33     public function __construct($source)
34     {
35         $this->source = $source;
36         $this->length = \strlen($source);
37     }
38
39     /**
40      * @return bool
41      */
42     public function isEOF()
43     {
44         return $this->position >= $this->length;
45     }
46
47     /**
48      * @return int
49      */
50     public function getPosition()
51     {
52         return $this->position;
53     }
54
55     /**
56      * @return int
57      */
58     public function getRemainingLength()
59     {
60         return $this->length - $this->position;
61     }
62
63     /**
64      * @param int $length
65      * @param int $offset
66      *
67      * @return string
68      */
69     public function getSubstring($length, $offset = 0)
70     {
71         return substr($this->source, $this->position + $offset, $length);
72     }
73
74     /**
75      * @param string $string
76      *
77      * @return int
78      */
79     public function getOffset($string)
80     {
81         $position = strpos($this->source, $string, $this->position);
82
83         return false === $position ? false : $position - $this->position;
84     }
85
86     /**
87      * @param string $pattern
88      *
89      * @return array|false
90      */
91     public function findPattern($pattern)
92     {
93         $source = substr($this->source, $this->position);
94
95         if (preg_match($pattern, $source, $matches)) {
96             return $matches;
97         }
98
99         return false;
100     }
101
102     /**
103      * @param int $length
104      */
105     public function moveForward($length)
106     {
107         $this->position += $length;
108     }
109
110     public function moveToEnd()
111     {
112         $this->position = $this->length;
113     }
114 }