1 | <?php |
||
7 | class TokenStore implements TokenStoreInterface |
||
8 | { |
||
9 | /** @var int[][] */ |
||
10 | private $maskStore = []; |
||
11 | /** @var int[] */ |
||
12 | private $tokens = []; |
||
13 | /** @var string|null */ |
||
14 | private $lastEncoding = null; |
||
15 | |||
16 | /** |
||
17 | * TokenStore constructor. |
||
18 | * |
||
19 | * @param CsvConfigurationInterface $config |
||
20 | */ |
||
21 | 29 | public function __construct(CsvConfigurationInterface $config) |
|
27 | |||
28 | /** |
||
29 | * @param int $mask |
||
30 | * |
||
31 | * @return int[] |
||
32 | */ |
||
33 | 29 | public function getTokens($mask = Token::T_ANY) |
|
43 | |||
44 | /** |
||
45 | * @param CsvConfigurationInterface $config |
||
46 | * |
||
47 | * @return int[] |
||
48 | */ |
||
49 | 29 | private function buildTokens(CsvConfigurationInterface $config) |
|
50 | { |
||
51 | $tokens = [ |
||
52 | 29 | $config->getDelimiter() => Token::T_DELIMITER, |
|
53 | 29 | ]; |
|
54 | |||
55 | 29 | if ($config->getQuote() != '') { |
|
56 | 27 | $tokens[$config->getQuote()] = Token::T_QUOTE; |
|
57 | 27 | if ($config->useDoubleQuotes()) { |
|
58 | 5 | $tokens[str_repeat($config->getQuote(), 2)] = Token::T_DOUBLE_QUOTE; |
|
59 | 5 | } |
|
60 | 27 | } |
|
61 | 29 | if ($config->getEscape() != '') { |
|
62 | 27 | $tokens[$config->getEscape()] = Token::T_ESCAPE; |
|
63 | 27 | } |
|
64 | |||
65 | 29 | foreach ($config->getNewLines() as $newLine) { |
|
66 | 29 | $tokens[$newLine] = Token::T_NEW_LINE; |
|
67 | 29 | } |
|
68 | 29 | if (!is_null($config->getNullValue())) { |
|
69 | 29 | $tokens[$config->getNullValue()] = Token::T_NULL; |
|
70 | 29 | } |
|
71 | |||
72 | 29 | foreach ($config->getBoms() as $bom) { |
|
73 | 29 | $tokens[$bom] = Token::T_BOM; |
|
74 | 29 | } |
|
75 | |||
76 | 29 | return $tokens; |
|
77 | } |
||
78 | |||
79 | /** |
||
80 | * @param string $encoding |
||
81 | */ |
||
82 | 29 | public function setEncoding($encoding) |
|
83 | { |
||
84 | 29 | if ($encoding != $this->lastEncoding) { |
|
85 | 29 | if (!is_null($this->lastEncoding)) { |
|
86 | $changeEncoding = function ($string) use ($encoding) { |
||
87 | 2 | return mb_convert_encoding($string, $encoding, $this->lastEncoding); |
|
88 | 2 | }; |
|
89 | 2 | } else { |
|
90 | $changeEncoding = function ($string) use ($encoding) { |
||
91 | 29 | return mb_convert_encoding($string, $encoding); |
|
92 | 29 | }; |
|
93 | } |
||
94 | 29 | $tokens = []; |
|
95 | 29 | foreach ($this->tokens as $string => $token) { |
|
96 | 29 | if ($token != Token::T_BOM) { |
|
97 | 29 | $string = $changeEncoding($string); |
|
98 | 29 | } |
|
99 | 29 | $tokens[$string] = $token; |
|
100 | 29 | } |
|
101 | 29 | $this->tokens = $tokens; |
|
102 | 29 | $this->sort(); |
|
103 | |||
104 | 29 | $this->lastEncoding = $encoding; |
|
105 | 29 | $this->maskStore = []; |
|
106 | 29 | } |
|
107 | 29 | } |
|
108 | |||
109 | /** |
||
110 | * Sort the tokens into reverse key length order |
||
111 | */ |
||
112 | private function sort() |
||
119 | |||
120 | /** |
||
121 | * Determine if a mask set of tokens has changed |
||
122 | * |
||
123 | * @param int $mask |
||
124 | * |
||
125 | * @return bool |
||
126 | */ |
||
127 | 26 | public function hasChanged($mask = Token::T_ANY) |
|
131 | } |
||
132 |