1 | <?php |
||
11 | abstract class AbstractBase implements BaseInterface |
||
12 | { |
||
13 | /** @var string */ |
||
14 | private $delimiter = ','; |
||
15 | |||
16 | /** @var string */ |
||
17 | private $enclosure = '"'; |
||
18 | |||
19 | /** @var string */ |
||
20 | private $escapeCharacter = '\\'; |
||
21 | |||
22 | /** @var false|array */ |
||
23 | private $headline = false; |
||
24 | |||
25 | /** @var SplFileObject */ |
||
26 | private $handler; |
||
27 | |||
28 | /** @var string */ |
||
29 | private $path; |
||
30 | |||
31 | /** |
||
32 | * @return bool |
||
33 | */ |
||
34 | public function hasHeadline() |
||
35 | { |
||
36 | return ($this->headline !== false); |
||
37 | } |
||
38 | |||
39 | /** |
||
40 | * @param string $delimiter |
||
41 | * @throws InvalidArgumentException |
||
42 | */ |
||
43 | public function setDelimiter($delimiter) |
||
44 | { |
||
45 | $this->assertIsASingleCharacterString($delimiter, 'delimiter'); |
||
46 | $this->delimiter = $delimiter; |
||
47 | $this->updateCsvControl(); |
||
48 | } |
||
49 | |||
50 | /** |
||
51 | * @param string $enclosure |
||
52 | * @throws InvalidArgumentException |
||
53 | */ |
||
54 | public function setEnclosure($enclosure) |
||
55 | { |
||
56 | $this->assertIsASingleCharacterString($enclosure, 'enclosure'); |
||
57 | $this->enclosure = $enclosure; |
||
58 | $this->updateCsvControl(); |
||
59 | } |
||
60 | |||
61 | /** |
||
62 | * @param string $escapeCharacter |
||
63 | * @throws InvalidArgumentException |
||
64 | */ |
||
65 | public function setEscapeCharacter($escapeCharacter) |
||
66 | { |
||
67 | $this->assertIsASingleCharacterString($escapeCharacter, 'escapeCharacter'); |
||
68 | $this->escapeCharacter = $escapeCharacter; |
||
69 | $this->updateCsvControl(); |
||
70 | } |
||
71 | |||
72 | /** |
||
73 | * @param string $path |
||
74 | * @return $this |
||
75 | * @throws InvalidArgumentException |
||
76 | * @todo implement validation |
||
77 | */ |
||
78 | public function setPath($path) |
||
79 | { |
||
80 | $this->path = $path; |
||
81 | $this->handler = $this->open($path); |
||
82 | |||
83 | return $this; |
||
84 | } |
||
85 | |||
86 | /** |
||
87 | * @return string |
||
88 | */ |
||
89 | protected function getDelimiter() |
||
90 | { |
||
91 | return $this->delimiter; |
||
92 | } |
||
93 | |||
94 | /** |
||
95 | * @return string |
||
96 | */ |
||
97 | protected function getEnclosure() |
||
98 | { |
||
99 | return $this->enclosure; |
||
100 | } |
||
101 | |||
102 | /** |
||
103 | * @return string |
||
104 | */ |
||
105 | protected function getEscapeCharacter() |
||
106 | { |
||
107 | return $this->escapeCharacter; |
||
108 | } |
||
109 | |||
110 | /** |
||
111 | * @return SplFileObject|resource |
||
112 | */ |
||
113 | protected function getFileHandler() |
||
114 | { |
||
115 | return $this->handler; |
||
116 | } |
||
117 | |||
118 | /** |
||
119 | * @return string |
||
120 | */ |
||
121 | abstract protected function getFileHandlerOpenMode(); |
||
122 | |||
123 | /** |
||
124 | * @return array|false |
||
125 | */ |
||
126 | protected function getHeadline() |
||
127 | { |
||
128 | return $this->headline; |
||
129 | } |
||
130 | |||
131 | /** |
||
132 | * @return string |
||
133 | */ |
||
134 | protected function getPath() |
||
138 | |||
139 | /** |
||
140 | * @return $this |
||
141 | */ |
||
142 | protected function resetHeadline() |
||
148 | |||
149 | /** |
||
150 | * @param array $headline |
||
151 | * @return $this |
||
152 | */ |
||
153 | protected function setHeadline(array $headline) |
||
159 | |||
160 | protected function close() |
||
166 | |||
167 | /** |
||
168 | * @param string $path |
||
169 | * @return SplFileObject |
||
170 | * @todo inject or inject factory |
||
171 | */ |
||
172 | protected function open($path) |
||
179 | |||
180 | /** |
||
181 | * @param string $variable |
||
182 | * @param string $name |
||
183 | * @throws InvalidArgumentException |
||
184 | */ |
||
185 | private function assertIsASingleCharacterString($variable, $name) |
||
198 | |||
199 | private function updateCsvControl() |
||
211 | } |