1<?php
2/**
3* Scan attribute selectors
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\Selector {
10
11  use PhpCss\Scanner;
12
13  /**
14   * Scan attribute selectors
15   */
16  class Attribute extends Scanner\Status {
17
18    /**
19    * single char tokens
20    * @var array
21    */
22    private $_tokenChars = array(
23      Scanner\Token::ATTRIBUTE_SELECTOR_END => ']',
24      Scanner\Token::SINGLEQUOTE_STRING_START => "'",
25      Scanner\Token::DOUBLEQUOTE_STRING_START => '"'
26    );
27
28    /**
29    * patterns for more complex tokens
30    * @var array
31    */
32    private $_tokenPatterns = array(
33      Scanner\Token::ATTRIBUTE_OPERATOR => Scanner\Patterns::ATTRIBUTE_OPERATOR,
34      Scanner\Token::WHITESPACE => Scanner\Patterns::WHITESPACE,
35      Scanner\Token::NUMBER => Scanner\Patterns::NUMBER,
36      Scanner\Token::IDENTIFIER => Scanner\Patterns::IDENTIFIER
37    );
38
39    /**
40    * Try to get token in buffer at offset position.
41    *
42    * @param string $buffer
43    * @param integer $offset
44    * @return Scanner\Token
45    */
46    public function getToken(string $buffer, int $offset): ?Scanner\Token {
47      if ($token = $this->matchCharacters($buffer, $offset, $this->_tokenChars)) {
48        return $token;
49      }
50      if ($token = $this->matchPatterns($buffer, $offset, $this->_tokenPatterns)) {
51        return $token;
52      }
53      return NULL;
54    }
55
56    /**
57    * Check if token ends status
58    *
59    * @param Scanner\Token $token
60    * @return boolean
61    */
62    public function isEndToken(Scanner\Token $token): bool {
63      return $token->type === Scanner\Token::ATTRIBUTE_SELECTOR_END;
64    }
65
66    /**
67    * Get new (sub)status if needed.
68    *
69    * @param Scanner\Token $token
70    * @return Scanner\Status|NULL
71    */
72    public function getNewStatus(Scanner\Token $token): ?Scanner\Status {
73      switch ($token->type) {
74      case Scanner\Token::SINGLEQUOTE_STRING_START :
75        return new Scanner\Status\Text\Single();
76      case Scanner\Token::DOUBLEQUOTE_STRING_START :
77        return new Scanner\Status\Text\Double();
78      }
79      return NULL;
80    }
81  }
82}
83