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; |
15
|
|
|
|
16
|
|
|
use ArrayIterator; |
17
|
|
|
use Graze\CsvToken\Tokeniser\Token\Token; |
18
|
|
|
use Graze\CsvToken\ValueParser\Value; |
19
|
|
|
use Graze\CsvToken\ValueParser\ValueParserInterface; |
20
|
|
|
use Iterator; |
21
|
|
|
use RuntimeException; |
22
|
|
|
|
23
|
|
|
class Parser implements ParserInterface |
24
|
|
|
{ |
25
|
|
|
/** @var ValueParserInterface[] */ |
26
|
|
|
private $valueParsers = []; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Parser constructor. |
30
|
|
|
* |
31
|
|
|
* @param ValueParserInterface[] $valueParsers |
32
|
|
|
*/ |
33
|
18 |
|
public function __construct(array $valueParsers = []) |
34
|
|
|
{ |
35
|
18 |
|
array_map([$this, 'addValueParser'], $valueParsers); |
36
|
18 |
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param Iterator $tokens |
40
|
|
|
* |
41
|
|
|
* @return Iterator Iterator of csv line arrays |
42
|
|
|
*/ |
43
|
14 |
|
public function parse(Iterator $tokens) |
44
|
|
|
{ |
45
|
14 |
|
$value = new Value($this->valueParsers); |
46
|
14 |
|
$row = []; |
47
|
|
|
|
48
|
14 |
|
foreach ($tokens as $token) { |
49
|
13 |
|
switch ($token[0]) { |
50
|
13 |
|
case Token::T_QUOTE: |
51
|
11 |
|
$value->setInQuotes(!$value->isInQuotes()); |
52
|
11 |
|
break; |
53
|
13 |
|
case Token::T_CONTENT: |
54
|
13 |
|
$value->addContent($token[1]); |
55
|
13 |
|
break; |
56
|
11 |
|
case Token::T_DOUBLE_QUOTE: |
57
|
3 |
|
$value->addContent(substr($token[1], 0, $token[3] / 2)); |
58
|
3 |
|
break; |
59
|
11 |
|
case Token::T_NULL: |
60
|
6 |
|
if ($value->isEmpty() && !$value->isInQuotes() && !$value->wasQuoted()) { |
61
|
6 |
|
$value->addContent($token[1]); |
62
|
6 |
|
$value->setIsNull(); |
63
|
|
|
} else { |
64
|
1 |
|
$value->addContent($token[1]); |
65
|
|
|
} |
66
|
6 |
|
break; |
67
|
11 |
|
case Token::T_DELIMITER: |
68
|
11 |
|
$row[] = $value->getValue(); |
69
|
11 |
|
$value->reset(); |
70
|
11 |
|
break; |
71
|
8 |
|
case Token::T_NEW_LINE: |
72
|
3 |
|
$row[] = $value->getValue(); |
73
|
3 |
|
$value->reset(); |
74
|
3 |
|
yield $row; |
75
|
3 |
|
$row = []; |
76
|
3 |
|
break; |
77
|
|
|
default: |
78
|
13 |
|
break; |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
82
|
13 |
|
if (!$value->isEmpty()) { |
83
|
11 |
|
if ($value->isInQuotes()) { |
84
|
1 |
|
throw new RuntimeException("Unmatched quote at the end of the csv data"); |
85
|
|
|
} |
86
|
10 |
|
$row[] = $value->getValue(); |
87
|
|
|
} |
88
|
|
|
|
89
|
12 |
|
if (count($row) > 0) { |
90
|
10 |
|
yield $row; |
91
|
|
|
} |
92
|
12 |
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @param ValueParserInterface $valueParser |
96
|
|
|
*/ |
97
|
4 |
|
public function addValueParser(ValueParserInterface $valueParser) |
98
|
|
|
{ |
99
|
4 |
|
$this->valueParsers[] = $valueParser; |
100
|
4 |
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @param ValueParserInterface $valueParser |
104
|
|
|
*/ |
105
|
1 |
|
public function removeValueParser(ValueParserInterface $valueParser) |
106
|
|
|
{ |
107
|
1 |
|
$index = array_search($valueParser, $this->valueParsers, true); |
108
|
1 |
|
if ($index !== false) { |
109
|
1 |
|
unset($this->valueParsers[$index]); |
110
|
|
|
} |
111
|
1 |
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @param ValueParserInterface $valueParser |
115
|
|
|
* |
116
|
|
|
* @return bool |
117
|
|
|
*/ |
118
|
3 |
|
public function hasValueParser(ValueParserInterface $valueParser) |
119
|
|
|
{ |
120
|
3 |
|
return in_array($valueParser, $this->valueParsers); |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|