1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Graze\CsvToken\ValueParser; |
4
|
|
|
|
5
|
|
|
use RuntimeException; |
6
|
|
|
|
7
|
|
|
class Value |
8
|
|
|
{ |
9
|
|
|
/** @var bool */ |
10
|
|
|
private $inQuotes = false; |
11
|
|
|
/** @var bool */ |
12
|
|
|
private $wasQuoted = false; |
13
|
|
|
/** @var ValueParserInterface[] */ |
14
|
|
|
private $valueParsers = []; |
15
|
|
|
/** @var string */ |
16
|
|
|
private $content = ''; |
17
|
|
|
/** @var bool */ |
18
|
|
|
private $isNull = false; |
19
|
|
|
/** @var bool */ |
20
|
|
|
private $hasContent = false; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Value constructor. |
24
|
|
|
* |
25
|
|
|
* @param array $valueParsers |
26
|
|
|
*/ |
27
|
9 |
|
public function __construct(array $valueParsers = []) |
28
|
|
|
{ |
29
|
9 |
|
$this->valueParsers = $valueParsers; |
30
|
9 |
|
$this->reset(); |
31
|
9 |
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param bool $quoted |
35
|
|
|
* |
36
|
|
|
* @return static |
37
|
|
|
*/ |
38
|
7 |
|
public function setInQuotes($quoted) |
39
|
|
|
{ |
40
|
7 |
|
$this->inQuotes = $quoted; |
41
|
7 |
|
if ($quoted) { |
42
|
7 |
|
$this->wasQuoted = true; |
43
|
7 |
|
} |
44
|
7 |
|
return $this; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param string $content |
49
|
|
|
* |
50
|
|
|
* @return static |
51
|
|
|
*/ |
52
|
8 |
|
public function addContent($content) |
53
|
|
|
{ |
54
|
8 |
|
if (!$this->inQuotes && $this->wasQuoted) { |
55
|
2 |
|
throw new RuntimeException( |
56
|
2 |
|
"Invalid CSV: Attempting to add a string to a field that was in quotes: " . $this->content . $content |
57
|
2 |
|
); |
58
|
|
|
} |
59
|
8 |
|
$this->content .= $content; |
60
|
8 |
|
$this->isNull = false; |
61
|
8 |
|
$this->hasContent = true; |
62
|
8 |
|
return $this; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Indicated that this value is null |
67
|
|
|
*/ |
68
|
4 |
|
public function setIsNull() |
69
|
|
|
{ |
70
|
4 |
|
$this->isNull = true; |
71
|
4 |
|
$this->hasContent = true; |
72
|
4 |
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @return mixed |
76
|
|
|
*/ |
77
|
7 |
|
public function getValue() |
78
|
|
|
{ |
79
|
7 |
|
if ($this->wasQuoted) { |
80
|
6 |
|
return $this->content; |
81
|
5 |
|
} elseif ($this->isNull) { |
82
|
4 |
|
return null; |
83
|
|
|
} else { |
84
|
5 |
|
foreach ($this->valueParsers as $valueParser) { |
85
|
1 |
|
if ($valueParser->canParseValue($this->content)) { |
86
|
1 |
|
return $valueParser->parseValue($this->content); |
87
|
|
|
} |
88
|
5 |
|
} |
89
|
|
|
} |
90
|
|
|
|
91
|
5 |
|
return $this->content; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Reset the state |
96
|
|
|
*/ |
97
|
9 |
|
public function reset() |
98
|
|
|
{ |
99
|
9 |
|
$this->inQuotes = false; |
100
|
9 |
|
$this->wasQuoted = false; |
101
|
9 |
|
$this->content = ''; |
102
|
9 |
|
$this->isNull = false; |
103
|
9 |
|
$this->hasContent = false; |
104
|
9 |
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @return bool |
108
|
|
|
*/ |
109
|
7 |
|
public function isInQuotes() |
110
|
|
|
{ |
111
|
7 |
|
return $this->inQuotes; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @return bool |
116
|
|
|
*/ |
117
|
5 |
|
public function wasQuoted() |
118
|
|
|
{ |
119
|
5 |
|
return $this->wasQuoted; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @return bool |
124
|
|
|
*/ |
125
|
8 |
|
public function isEmpty() |
126
|
|
|
{ |
127
|
8 |
|
return !$this->hasContent; |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|