1<?php
2/**
3* Scanner Status "Selector"
4*
5* @license http://www.opensource.org/licenses/mit-license.php The MIT License
6* @copyright Copyright 2010-2014 PhpCss Team
7*/
8
9namespace PhpCss\Scanner\Status {
10
11  use PhpCss\Scanner;
12
13  /**
14  * Scanner Status Selector recognizes token of a css selector sequence.
15  */
16  class Selector extends Scanner\Status {
17
18    /**
19    * single char tokens
20    * @var array
21    */
22    private $_tokenChars = array(
23      Scanner\Token::SEPARATOR => ',',
24      Scanner\Token::ATTRIBUTE_SELECTOR_START => '[',
25      Scanner\Token::PARENTHESES_START => '(',
26      Scanner\Token::PARENTHESES_END => ')',
27      Scanner\Token::SINGLEQUOTE_STRING_START => "'",
28      Scanner\Token::DOUBLEQUOTE_STRING_START => '"'
29    );
30
31    /**
32    * patterns for more complex tokens
33    * @var array
34    */
35    private $_tokenPatterns = array(
36      Scanner\Token::CLASS_SELECTOR => Scanner\Patterns::CLASS_SELECTOR,
37      Scanner\Token::ID_SELECTOR => Scanner\Patterns::ID_SELECTOR,
38      Scanner\Token::PSEUDO_CLASS => Scanner\Patterns::PSEUDO_CLASS,
39      Scanner\Token::PSEUDO_CLASS_POSITION => Scanner\Patterns::PSEUDO_CLASS_POSITION,
40      Scanner\Token::PSEUDO_ELEMENT => Scanner\Patterns::PSEUDO_ELEMENT,
41      Scanner\Token::IDENTIFIER => Scanner\Patterns::IDENTIFIER,
42      Scanner\Token::COMBINATOR => Scanner\Patterns::COMBINATOR,
43      Scanner\Token::WHITESPACE => Scanner\Patterns::WHITESPACE,
44      Scanner\Token::NUMBER => Scanner\Patterns::NUMBER
45    );
46
47    /**
48    * Try to get token in buffer at offset position.
49    *
50    * @param string $buffer
51    * @param integer $offset
52    * @return Scanner\Token
53    */
54    public function getToken(string $buffer, int $offset): ?Scanner\Token {
55      if ($token = $this->matchCharacters($buffer, $offset, $this->_tokenChars)) {
56        return $token;
57      }
58      if ($token = $this->matchPatterns($buffer, $offset, $this->_tokenPatterns)) {
59        return $token;
60      }
61      return NULL;
62    }
63
64    /**
65    * Check if token ends status
66    *
67    * @param Scanner\Token $token
68    * @return boolean
69    */
70    public function isEndToken(Scanner\Token $token): bool {
71      return FALSE;
72    }
73
74    /**
75    * Get new (sub)status if needed.
76    *
77    * @param Scanner\Token $token
78    * @return Scanner\Status|NULL
79    */
80    public function getNewStatus(Scanner\Token $token): ?Scanner\Status {
81      switch ($token->type) {
82      case Scanner\Token::SINGLEQUOTE_STRING_START :
83        return new Scanner\Status\Text\Single();
84      case Scanner\Token::DOUBLEQUOTE_STRING_START :
85        return new Scanner\Status\Text\Double();
86      case Scanner\Token::ATTRIBUTE_SELECTOR_START :
87        return new Scanner\Status\Selector\Attribute();
88      }
89      return NULL;
90    }
91  }
92}
93