|
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; |
|
15
|
|
|
|
|
16
|
|
|
use Graze\CsvToken\Csv\CsvConfigurationInterface; |
|
17
|
|
|
|
|
18
|
|
|
trait TypeBuilder |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* @param CsvConfigurationInterface $config |
|
22
|
|
|
* |
|
23
|
|
|
* @return int[] Sorted in order of precedence |
|
24
|
|
|
*/ |
|
25
|
24 |
|
protected function getTypes(CsvConfigurationInterface $config) |
|
26
|
|
|
{ |
|
27
|
|
|
$types = [ |
|
28
|
24 |
|
$config->getDelimiter() => Token::T_DELIMITER, |
|
29
|
24 |
|
]; |
|
30
|
|
|
|
|
31
|
24 |
|
if ($config->getQuote() != '') { |
|
32
|
22 |
|
$types[$config->getQuote()] = Token::T_QUOTE; |
|
33
|
22 |
|
if ($config->useDoubleQuotes()) { |
|
34
|
4 |
|
$types[str_repeat($config->getQuote(), 2)] = Token::T_DOUBLE_QUOTE; |
|
35
|
4 |
|
} |
|
36
|
22 |
|
} |
|
37
|
24 |
|
if ($config->getEscape() != '') { |
|
38
|
22 |
|
$types[$config->getEscape()] = Token::T_ESCAPE; |
|
39
|
22 |
|
} |
|
40
|
|
|
|
|
41
|
24 |
|
$newLines = $config->getNewLine(); |
|
42
|
24 |
|
if (!is_array($newLines)) { |
|
43
|
24 |
|
$newLines = [$newLines]; |
|
44
|
24 |
|
} |
|
45
|
24 |
|
foreach ($newLines as $newLine) { |
|
46
|
24 |
|
$types[$newLine] = Token::T_NEW_LINE; |
|
47
|
24 |
|
} |
|
48
|
24 |
|
if (!is_null($config->getNullValue())) { |
|
49
|
24 |
|
$types[$config->getNullValue()] = Token::T_NULL; |
|
50
|
24 |
|
} |
|
51
|
|
|
|
|
52
|
|
|
// sort by reverse key length |
|
53
|
24 |
|
uksort($types, function ($first, $second) { |
|
54
|
24 |
|
return strlen($second) - strlen($first); |
|
55
|
24 |
|
}); |
|
56
|
|
|
|
|
57
|
24 |
|
return $types; |
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
|
|
|