TokenStore   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 125
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 17
c 2
b 0
f 0
lcom 1
cbo 1
dl 0
loc 125
ccs 48
cts 48
cp 1
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A getTokens() 0 10 2
C buildTokens() 0 29 7
B setEncoding() 0 26 5
A sort() 0 7 1
A hasChanged() 0 4 1
1
<?php
2
/**
3
 * This file is part of graze/csv-token
4
 *
5
 * Copyright (c) 2016 Nature Delivered Ltd. <https://www.graze.com>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 *
10
 * @license https://github.com/graze/csv-token/blob/master/LICENSE.md
11
 * @link    https://github.com/graze/csv-token
12
 */
13
14
namespace Graze\CsvToken\Tokeniser\Token;
15
16
use Graze\CsvToken\Csv\CsvConfigurationInterface;
17
18
class TokenStore implements TokenStoreInterface
19
{
20
    /** @var int[][] */
21
    private $maskStore = [];
22
    /** @var int[] */
23
    private $tokens = [];
24
    /** @var string|null */
25
    private $lastEncoding = null;
26
27
    /**
28
     * TokenStore constructor.
29
     *
30
     * @param CsvConfigurationInterface $config
31
     */
32 30
    public function __construct(CsvConfigurationInterface $config)
33
    {
34 30
        $this->tokens = $this->buildTokens($config);
35 30
        $this->setEncoding($config->getEncoding());
36 30
        $this->sort();
37 30
    }
38
39
    /**
40
     * @param int $mask
41
     *
42
     * @return int[]
43
     */
44 30
    public function getTokens($mask = Token::T_ANY)
45
    {
46 30
        if (!array_key_exists($mask, $this->maskStore)) {
47
            $this->maskStore[$mask] = array_filter($this->tokens, function ($type) use ($mask) {
48 30
                return $type & $mask;
49 30
            });
50
        }
51
52 30
        return $this->maskStore[$mask];
53
    }
54
55
    /**
56
     * @param CsvConfigurationInterface $config
57
     *
58
     * @return int[]
59
     */
60 30
    private function buildTokens(CsvConfigurationInterface $config)
61
    {
62
        $tokens = [
63 30
            $config->getDelimiter() => Token::T_DELIMITER,
64
        ];
65
66 30
        if ($config->getQuote() != '') {
67 28
            $tokens[$config->getQuote()] = Token::T_QUOTE;
68 28
            if ($config->useDoubleQuotes()) {
69 5
                $tokens[str_repeat($config->getQuote(), 2)] = Token::T_DOUBLE_QUOTE;
70
            }
71
        }
72 30
        if ($config->getEscape() != '') {
73 28
            $tokens[$config->getEscape()] = Token::T_ESCAPE;
74
        }
75
76 30
        foreach ($config->getNewLines() as $newLine) {
77 30
            $tokens[$newLine] = Token::T_NEW_LINE;
78
        }
79 30
        if (!is_null($config->getNullValue())) {
80 30
            $tokens[$config->getNullValue()] = Token::T_NULL;
81
        }
82
83 30
        foreach ($config->getBoms() as $bom) {
84 30
            $tokens[$bom] = Token::T_BOM;
85
        }
86
87 30
        return $tokens;
88
    }
89
90
    /**
91
     * @param string $encoding
92
     */
93 30
    public function setEncoding($encoding)
94
    {
95 30
        if ($encoding != $this->lastEncoding) {
96 30
            if (!is_null($this->lastEncoding)) {
97
                $changeEncoding = function ($string) use ($encoding) {
98 2
                    return mb_convert_encoding($string, $encoding, $this->lastEncoding);
99 2
                };
100
            } else {
101
                $changeEncoding = function ($string) use ($encoding) {
102 30
                    return mb_convert_encoding($string, $encoding);
103 30
                };
104
            }
105 30
            $tokens = [];
106 30
            foreach ($this->tokens as $string => $token) {
107 30
                if ($token != Token::T_BOM) {
108 30
                    $string = $changeEncoding($string);
109
                }
110 30
                $tokens[$string] = $token;
111
            }
112 30
            $this->tokens = $tokens;
113 30
            $this->sort();
114
115 30
            $this->lastEncoding = $encoding;
116 30
            $this->maskStore = [];
117
        }
118 30
    }
119
120
    /**
121
     * Sort the tokens into reverse key length order
122
     */
123
    private function sort()
124
    {
125
        // sort by reverse key length
126 30
        uksort($this->tokens, function ($first, $second) {
127 30
            return strlen($second) - strlen($first);
128 30
        });
129 30
    }
130
131
    /**
132
     * Determine if a mask set of tokens has changed
133
     *
134
     * @param int $mask
135
     *
136
     * @return bool
137
     */
138 27
    public function hasChanged($mask = Token::T_ANY)
139
    {
140 27
        return !isset($this->maskStore[$mask]);
141
    }
142
}
143