@@ -30,12 +30,12 @@ |
||
30 | 30 | $record = $parser->getCurrentLineRecord(); |
31 | 31 | $depth = (int)$record[0]; |
32 | 32 | if(isset($record[2])){ |
33 | - $phone = new \PhpGedcom\Record\Phon(); |
|
34 | - $phone->setPhon(trim($record[2])); |
|
33 | + $phone = new \PhpGedcom\Record\Phon(); |
|
34 | + $phone->setPhon(trim($record[2])); |
|
35 | 35 | } |
36 | 36 | else{ |
37 | - $parser->skipToNextLevel($depth); |
|
38 | - return null; |
|
37 | + $parser->skipToNextLevel($depth); |
|
38 | + return null; |
|
39 | 39 | } |
40 | 40 | |
41 | 41 |
@@ -30,12 +30,12 @@ |
||
30 | 30 | $record = $parser->getCurrentLineRecord(); |
31 | 31 | $depth = (int)$record[0]; |
32 | 32 | if(isset($record[2])){ |
33 | - $even = new \PhpGedcom\Record\SourRef\Even(); |
|
34 | - $even->setEven(trim($record[2])); |
|
33 | + $even = new \PhpGedcom\Record\SourRef\Even(); |
|
34 | + $even->setEven(trim($record[2])); |
|
35 | 35 | } |
36 | 36 | else{ |
37 | - $parser->skipToNextLevel($depth); |
|
38 | - return null; |
|
37 | + $parser->skipToNextLevel($depth); |
|
38 | + return null; |
|
39 | 39 | } |
40 | 40 | |
41 | 41 | $parser->forward(); |
@@ -29,7 +29,7 @@ |
||
29 | 29 | { |
30 | 30 | $data = new \PhpGedcom\Record\SourRef\Data(); |
31 | 31 | $record = $parser->getCurrentLineRecord(); |
32 | - $depth = (int) $record[0]; |
|
32 | + $depth = (int) $record[0]; |
|
33 | 33 | |
34 | 34 | $parser->forward(); |
35 | 35 |
@@ -26,89 +26,89 @@ |
||
26 | 26 | */ |
27 | 27 | public static function convert(\PhpGedcom\Record\Head &$head, $format = self::GEDCOM55) |
28 | 28 | { |
29 | - $level = 0; |
|
30 | - $output = $level." HEAD\n"; |
|
29 | + $level = 0; |
|
30 | + $output = $level." HEAD\n"; |
|
31 | 31 | |
32 | - // level up |
|
33 | - $level++; |
|
32 | + // level up |
|
33 | + $level++; |
|
34 | 34 | |
35 | - //SOUR |
|
36 | - $sour = $head->getSour(); |
|
37 | - if($sour){ |
|
35 | + //SOUR |
|
36 | + $sour = $head->getSour(); |
|
37 | + if($sour){ |
|
38 | 38 | $_convert = \PhpGedcom\Writer\Head\Sour::convert($sour, $level); |
39 | 39 | $output.=$_convert; |
40 | - } |
|
40 | + } |
|
41 | 41 | |
42 | - // DEST |
|
43 | - $dest = $head->getDest(); |
|
44 | - if($dest){ |
|
42 | + // DEST |
|
43 | + $dest = $head->getDest(); |
|
44 | + if($dest){ |
|
45 | 45 | $output.=$level." DEST ".$dest."\n"; |
46 | - } |
|
46 | + } |
|
47 | 47 | |
48 | - //Subm |
|
49 | - $subm = $head->getSubm(); |
|
50 | - if($subm){ |
|
48 | + //Subm |
|
49 | + $subm = $head->getSubm(); |
|
50 | + if($subm){ |
|
51 | 51 | $output.=$level." SUBM ".$subm."\n"; |
52 | - } |
|
52 | + } |
|
53 | 53 | |
54 | - // SUBN |
|
55 | - $subn = $head->getSubn(); |
|
56 | - if($subn){ |
|
54 | + // SUBN |
|
55 | + $subn = $head->getSubn(); |
|
56 | + if($subn){ |
|
57 | 57 | $output.=$level." SUBN ".$subn."\n"; |
58 | - } |
|
58 | + } |
|
59 | 59 | |
60 | - // FILE |
|
61 | - $file = $head->getFile(); |
|
62 | - if($file){ |
|
60 | + // FILE |
|
61 | + $file = $head->getFile(); |
|
62 | + if($file){ |
|
63 | 63 | $output.=$level." FILE ".$file."\n"; |
64 | - } |
|
64 | + } |
|
65 | 65 | |
66 | - // COPR |
|
67 | - $copr = $head->getCopr(); |
|
68 | - if($copr){ |
|
66 | + // COPR |
|
67 | + $copr = $head->getCopr(); |
|
68 | + if($copr){ |
|
69 | 69 | $output.=$level." COPR ".$copr."\n"; |
70 | - } |
|
70 | + } |
|
71 | 71 | |
72 | - // LANG |
|
73 | - $lang = $head->getLang(); |
|
74 | - if($lang){ |
|
72 | + // LANG |
|
73 | + $lang = $head->getLang(); |
|
74 | + if($lang){ |
|
75 | 75 | $output.=$level." LANG ".$lang."\n"; |
76 | - } |
|
77 | - // DATE |
|
78 | - $date = $head->getDate(); |
|
79 | - if($date){ |
|
76 | + } |
|
77 | + // DATE |
|
78 | + $date = $head->getDate(); |
|
79 | + if($date){ |
|
80 | 80 | $_convert = \PhpGedcom\Writer\Head\Date::convert($date, $level); |
81 | 81 | $output.=$_convert; |
82 | - } |
|
82 | + } |
|
83 | 83 | |
84 | 84 | |
85 | - // GEDC |
|
86 | - $gedc = $head->getGedc(); |
|
87 | - if($gedc){ |
|
85 | + // GEDC |
|
86 | + $gedc = $head->getGedc(); |
|
87 | + if($gedc){ |
|
88 | 88 | $_convert = \PhpGedcom\Writer\Head\Gedc::convert($gedc, $level); |
89 | 89 | $output.=$_convert; |
90 | - } |
|
90 | + } |
|
91 | 91 | |
92 | 92 | |
93 | - // CHAR |
|
94 | - $char = $head->getChar(); |
|
95 | - if($char){ |
|
93 | + // CHAR |
|
94 | + $char = $head->getChar(); |
|
95 | + if($char){ |
|
96 | 96 | $_convert = \PhpGedcom\Writer\Head\Char::convert($char, $level); |
97 | 97 | $output.=$_convert; |
98 | - } |
|
99 | - // PLAC |
|
100 | - $plac = $head->getPlac(); |
|
101 | - if($plac){ |
|
98 | + } |
|
99 | + // PLAC |
|
100 | + $plac = $head->getPlac(); |
|
101 | + if($plac){ |
|
102 | 102 | $_convert = \PhpGedcom\Writer\Head\Plac::convert($plac, $level); |
103 | 103 | $output.=$_convert; |
104 | - } |
|
104 | + } |
|
105 | 105 | |
106 | - // NOTE |
|
107 | - $note = $head->getNote(); |
|
108 | - if($note){ |
|
106 | + // NOTE |
|
107 | + $note = $head->getNote(); |
|
108 | + if($note){ |
|
109 | 109 | $output.=$level." NOTE ".$note."\n"; |
110 | - } |
|
111 | - // |
|
110 | + } |
|
111 | + // |
|
112 | 112 | /* |
113 | 113 | +1 SUBM @<XREF:SUBM>@ {1:1} |
114 | 114 | +1 SUBN @<XREF:SUBN>@ {0:1} |
@@ -18,301 +18,301 @@ |
||
18 | 18 | * |
19 | 19 | */ |
20 | 20 | class Parser { |
21 | - /** |
|
22 | - * |
|
23 | - */ |
|
24 | - protected $_file = null; |
|
25 | - |
|
26 | - /** |
|
27 | - * |
|
28 | - */ |
|
29 | - protected $_gedcom = null; |
|
30 | - |
|
31 | - /** |
|
32 | - * |
|
33 | - */ |
|
34 | - protected $_errorLog = array(); |
|
35 | - |
|
36 | - /** |
|
37 | - * |
|
38 | - */ |
|
39 | - protected $_linesParsed = 0; |
|
40 | - |
|
41 | - /** |
|
42 | - * |
|
43 | - */ |
|
44 | - protected $_line = ''; |
|
45 | - |
|
46 | - /** |
|
47 | - * |
|
48 | - */ |
|
49 | - protected $_lineRecord = null; |
|
50 | - |
|
51 | - /** |
|
52 | - * |
|
53 | - */ |
|
54 | - protected $_linePieces = 0; |
|
55 | - |
|
56 | - /** |
|
57 | - * |
|
58 | - */ |
|
59 | - protected $_returnedLine = ''; |
|
60 | - |
|
61 | - /** |
|
62 | - * |
|
63 | - */ |
|
64 | - public function __construct(\PhpGedcom\Gedcom $gedcom = null) { |
|
65 | - if (!is_null($gedcom)) { |
|
66 | - $this->_gedcom = $gedcom; |
|
67 | - } else { |
|
68 | - $this->_gedcom = new \PhpGedcom\Gedcom(); |
|
69 | - } |
|
70 | - } |
|
71 | - |
|
72 | - /** |
|
73 | - * |
|
74 | - */ |
|
75 | - public function forward() { |
|
76 | - // if there was a returned line by back(), set that as our current |
|
77 | - // line and blank out the returnedLine variable, otherwise grab |
|
78 | - // the next line from the file |
|
79 | - |
|
80 | - if (!empty($this->_returnedLine)) { |
|
81 | - $this->_line = $this->_returnedLine; |
|
82 | - $this->_returnedLine = ''; |
|
83 | - } else { |
|
84 | - $this->_line = fgets($this->_file); |
|
85 | - $this->_lineRecord = null; |
|
86 | - $this->_linesParsed++; |
|
87 | - } |
|
88 | - |
|
89 | - return $this; |
|
90 | - } |
|
91 | - |
|
92 | - /** |
|
93 | - * |
|
94 | - */ |
|
95 | - public function back() { |
|
96 | - // our parser object encountered a line it wasn't meant to parse |
|
97 | - // store this line for the previous parser to analyze |
|
98 | - |
|
99 | - $this->_returnedLine = $this->_line; |
|
100 | - |
|
101 | - return $this; |
|
102 | - } |
|
103 | - |
|
104 | - /** |
|
105 | - * Jump to the next level in the GEDCOM that is <= $level. This will leave the parser at the line above |
|
106 | - * this level, such that calling $parser->forward() will result in landing at the correct level. |
|
107 | - * |
|
108 | - * @param int $level |
|
109 | - */ |
|
110 | - public function skipToNextLevel($level) { |
|
111 | - $currentDepth = 999; |
|
112 | - |
|
113 | - while ($currentDepth > $level) { |
|
114 | - $this->forward(); |
|
115 | - $record = $this->getCurrentLineRecord(); |
|
116 | - $currentDepth = (int) $record[0]; |
|
117 | - } |
|
118 | - |
|
119 | - $this->back(); |
|
120 | - } |
|
121 | - |
|
122 | - /** |
|
123 | - * |
|
124 | - */ |
|
125 | - public function getGedcom() { |
|
126 | - return $this->_gedcom; |
|
127 | - } |
|
128 | - |
|
129 | - /** |
|
130 | - * |
|
131 | - */ |
|
132 | - public function eof() { |
|
133 | - return feof($this->_file); |
|
134 | - } |
|
135 | - |
|
136 | - /** |
|
137 | - * |
|
138 | - * @return string |
|
139 | - */ |
|
140 | - public function parseMultiLineRecord() { |
|
141 | - $record = $this->getCurrentLineRecord(); |
|
142 | - |
|
143 | - $depth = (int) $record[0]; |
|
144 | - $data = isset($record[2]) ? trim($record[2]) : ''; |
|
145 | - |
|
146 | - $this->forward(); |
|
147 | - |
|
148 | - while (!$this->eof()) { |
|
149 | - $record = $this->getCurrentLineRecord(); |
|
150 | - $recordType = strtoupper(trim($record[1])); |
|
151 | - $currentDepth = (int) $record[0]; |
|
152 | - |
|
153 | - if ($currentDepth <= $depth) { |
|
154 | - $this->back(); |
|
155 | - break; |
|
156 | - } |
|
157 | - |
|
158 | - switch ($recordType) { |
|
159 | - case 'CONT': |
|
160 | - $data .= "\n"; |
|
161 | - |
|
162 | - if (isset($record[2])) { |
|
163 | - $data .= trim($record[2]); |
|
164 | - } |
|
165 | - break; |
|
166 | - case 'CONC': |
|
167 | - if (isset($record[2])) { |
|
168 | - $data .= ' ' . trim($record[2]); |
|
169 | - } |
|
170 | - break; |
|
171 | - default: |
|
172 | - $this->back(); |
|
173 | - break 2; |
|
174 | - } |
|
175 | - |
|
176 | - $this->forward(); |
|
177 | - } |
|
178 | - |
|
179 | - return $data; |
|
180 | - } |
|
181 | - |
|
182 | - /** |
|
183 | - * |
|
184 | - * @return string The current line |
|
185 | - */ |
|
186 | - public function getCurrentLine() { |
|
187 | - return $this->_line; |
|
188 | - } |
|
189 | - |
|
190 | - /** |
|
191 | - * |
|
192 | - */ |
|
193 | - public function getCurrentLineRecord($pieces = 3) { |
|
194 | - if (!is_null($this->_lineRecord)) { |
|
195 | - if ($this->_linePieces == $pieces) { |
|
196 | - return $this->_lineRecord; |
|
197 | - } |
|
198 | - } |
|
199 | - |
|
200 | - if (empty($this->_line)) { |
|
201 | - return false; |
|
202 | - } |
|
203 | - |
|
204 | - $line = trim($this->_line); |
|
205 | - |
|
206 | - $this->_lineRecord = explode(' ', $line, $pieces); |
|
207 | - $this->_linePieces = $pieces; |
|
208 | - |
|
209 | - return $this->_lineRecord; |
|
210 | - } |
|
211 | - |
|
212 | - /** |
|
213 | - * |
|
214 | - */ |
|
215 | - protected function logError($error) { |
|
216 | - $this->_errorLog[] = $error; |
|
217 | - } |
|
218 | - |
|
219 | - /** |
|
220 | - * |
|
221 | - */ |
|
222 | - public function logUnhandledRecord($additionalInfo = '') { |
|
223 | - $this->logError( |
|
224 | - $this->_linesParsed . ': (Unhandled) ' . trim(implode('|', $this->getCurrentLineRecord())) . |
|
225 | - (!empty($additionalInfo) ? ' - ' . $additionalInfo : '') |
|
226 | - ); |
|
227 | - } |
|
228 | - |
|
229 | - public function logSkippedRecord($additionalInfo = '') { |
|
230 | - $this->logError( |
|
231 | - $this->_linesParsed . ': (Skipping) ' . trim(implode('|', $this->getCurrentLineRecord())) . |
|
232 | - (!empty($additionalInfo) ? ' - ' . $additionalInfo : '') |
|
233 | - ); |
|
234 | - } |
|
235 | - |
|
236 | - /** |
|
237 | - * |
|
238 | - */ |
|
239 | - public function getErrors() { |
|
240 | - return $this->_errorLog; |
|
241 | - } |
|
242 | - |
|
243 | - /** |
|
244 | - * |
|
245 | - */ |
|
246 | - public function normalizeIdentifier($identifier) { |
|
247 | - $identifier = trim($identifier); |
|
248 | - $identifier = trim($identifier, '@'); |
|
249 | - |
|
250 | - return $identifier; |
|
251 | - } |
|
252 | - |
|
253 | - /** |
|
254 | - * |
|
255 | - * @param string $fileName |
|
256 | - * @return Gedcom |
|
257 | - */ |
|
258 | - public function parse($fileName) { |
|
259 | - $this->_file = fopen($fileName, 'r'); #explode("\n", mb_convert_encoding($contents, 'UTF-8')); |
|
260 | - |
|
261 | - if (!$this->_file) { |
|
262 | - return null; |
|
263 | - } |
|
264 | - |
|
265 | - $this->forward(); |
|
266 | - |
|
267 | - while (!$this->eof()) { |
|
268 | - $record = $this->getCurrentLineRecord(); |
|
269 | - |
|
270 | - if ($record === false) { |
|
271 | - continue; |
|
272 | - } |
|
273 | - |
|
274 | - $depth = (int) $record[0]; |
|
275 | - |
|
276 | - // We only process 0 level records here. Sub levels are processed |
|
277 | - // in methods for those data types (individuals, sources, etc) |
|
278 | - |
|
279 | - if ($depth == 0) { |
|
280 | - // Although not always an identifier (HEAD,TRLR): |
|
281 | - if (isset($record[1])) { |
|
282 | - $identifier = $this->normalizeIdentifier($record[1]); |
|
283 | - } |
|
284 | - |
|
285 | - if (isset($record[1]) && trim($record[1]) == 'HEAD') { |
|
286 | - Parser\Head::parse($this); |
|
287 | - } else if (isset($record[2]) && trim($record[2]) == 'SUBN') { |
|
288 | - Parser\Subn::parse($this); |
|
289 | - } else if (isset($record[2]) && trim($record[2]) == 'SUBM') { |
|
290 | - Parser\Subm::parse($this); |
|
291 | - } else if (isset($record[2]) && $record[2] == 'SOUR') { |
|
292 | - Parser\Sour::parse($this); |
|
293 | - } else if (isset($record[2]) && $record[2] == 'INDI') { |
|
294 | - Parser\Indi::parse($this); |
|
295 | - } else if (isset($record[2]) && $record[2] == 'FAM') { |
|
296 | - Parser\Fam::parse($this); |
|
297 | - } else if (isset($record[2]) && substr(trim($record[2]), 0, 4) == 'NOTE') { |
|
298 | - Parser\Note::parse($this); |
|
299 | - } else if (isset($record[2]) && $record[2] == 'REPO') { |
|
300 | - Parser\Repo::parse($this); |
|
301 | - } else if (isset($record[2]) && $record[2] == 'OBJE') { |
|
302 | - Parser\Obje::parse($this); |
|
303 | - } else if (isset($record[1]) && trim($record[1]) == 'TRLR') { |
|
304 | - // EOF |
|
305 | - break; |
|
306 | - } else { |
|
307 | - $this->logUnhandledRecord(get_class() . ' @ ' . __LINE__); |
|
308 | - } |
|
309 | - } else { |
|
310 | - $this->logUnhandledRecord(get_class() . ' @ ' . __LINE__); |
|
311 | - } |
|
312 | - |
|
313 | - $this->forward(); |
|
314 | - } |
|
315 | - |
|
316 | - return $this->getGedcom(); |
|
317 | - } |
|
21 | + /** |
|
22 | + * |
|
23 | + */ |
|
24 | + protected $_file = null; |
|
25 | + |
|
26 | + /** |
|
27 | + * |
|
28 | + */ |
|
29 | + protected $_gedcom = null; |
|
30 | + |
|
31 | + /** |
|
32 | + * |
|
33 | + */ |
|
34 | + protected $_errorLog = array(); |
|
35 | + |
|
36 | + /** |
|
37 | + * |
|
38 | + */ |
|
39 | + protected $_linesParsed = 0; |
|
40 | + |
|
41 | + /** |
|
42 | + * |
|
43 | + */ |
|
44 | + protected $_line = ''; |
|
45 | + |
|
46 | + /** |
|
47 | + * |
|
48 | + */ |
|
49 | + protected $_lineRecord = null; |
|
50 | + |
|
51 | + /** |
|
52 | + * |
|
53 | + */ |
|
54 | + protected $_linePieces = 0; |
|
55 | + |
|
56 | + /** |
|
57 | + * |
|
58 | + */ |
|
59 | + protected $_returnedLine = ''; |
|
60 | + |
|
61 | + /** |
|
62 | + * |
|
63 | + */ |
|
64 | + public function __construct(\PhpGedcom\Gedcom $gedcom = null) { |
|
65 | + if (!is_null($gedcom)) { |
|
66 | + $this->_gedcom = $gedcom; |
|
67 | + } else { |
|
68 | + $this->_gedcom = new \PhpGedcom\Gedcom(); |
|
69 | + } |
|
70 | + } |
|
71 | + |
|
72 | + /** |
|
73 | + * |
|
74 | + */ |
|
75 | + public function forward() { |
|
76 | + // if there was a returned line by back(), set that as our current |
|
77 | + // line and blank out the returnedLine variable, otherwise grab |
|
78 | + // the next line from the file |
|
79 | + |
|
80 | + if (!empty($this->_returnedLine)) { |
|
81 | + $this->_line = $this->_returnedLine; |
|
82 | + $this->_returnedLine = ''; |
|
83 | + } else { |
|
84 | + $this->_line = fgets($this->_file); |
|
85 | + $this->_lineRecord = null; |
|
86 | + $this->_linesParsed++; |
|
87 | + } |
|
88 | + |
|
89 | + return $this; |
|
90 | + } |
|
91 | + |
|
92 | + /** |
|
93 | + * |
|
94 | + */ |
|
95 | + public function back() { |
|
96 | + // our parser object encountered a line it wasn't meant to parse |
|
97 | + // store this line for the previous parser to analyze |
|
98 | + |
|
99 | + $this->_returnedLine = $this->_line; |
|
100 | + |
|
101 | + return $this; |
|
102 | + } |
|
103 | + |
|
104 | + /** |
|
105 | + * Jump to the next level in the GEDCOM that is <= $level. This will leave the parser at the line above |
|
106 | + * this level, such that calling $parser->forward() will result in landing at the correct level. |
|
107 | + * |
|
108 | + * @param int $level |
|
109 | + */ |
|
110 | + public function skipToNextLevel($level) { |
|
111 | + $currentDepth = 999; |
|
112 | + |
|
113 | + while ($currentDepth > $level) { |
|
114 | + $this->forward(); |
|
115 | + $record = $this->getCurrentLineRecord(); |
|
116 | + $currentDepth = (int) $record[0]; |
|
117 | + } |
|
118 | + |
|
119 | + $this->back(); |
|
120 | + } |
|
121 | + |
|
122 | + /** |
|
123 | + * |
|
124 | + */ |
|
125 | + public function getGedcom() { |
|
126 | + return $this->_gedcom; |
|
127 | + } |
|
128 | + |
|
129 | + /** |
|
130 | + * |
|
131 | + */ |
|
132 | + public function eof() { |
|
133 | + return feof($this->_file); |
|
134 | + } |
|
135 | + |
|
136 | + /** |
|
137 | + * |
|
138 | + * @return string |
|
139 | + */ |
|
140 | + public function parseMultiLineRecord() { |
|
141 | + $record = $this->getCurrentLineRecord(); |
|
142 | + |
|
143 | + $depth = (int) $record[0]; |
|
144 | + $data = isset($record[2]) ? trim($record[2]) : ''; |
|
145 | + |
|
146 | + $this->forward(); |
|
147 | + |
|
148 | + while (!$this->eof()) { |
|
149 | + $record = $this->getCurrentLineRecord(); |
|
150 | + $recordType = strtoupper(trim($record[1])); |
|
151 | + $currentDepth = (int) $record[0]; |
|
152 | + |
|
153 | + if ($currentDepth <= $depth) { |
|
154 | + $this->back(); |
|
155 | + break; |
|
156 | + } |
|
157 | + |
|
158 | + switch ($recordType) { |
|
159 | + case 'CONT': |
|
160 | + $data .= "\n"; |
|
161 | + |
|
162 | + if (isset($record[2])) { |
|
163 | + $data .= trim($record[2]); |
|
164 | + } |
|
165 | + break; |
|
166 | + case 'CONC': |
|
167 | + if (isset($record[2])) { |
|
168 | + $data .= ' ' . trim($record[2]); |
|
169 | + } |
|
170 | + break; |
|
171 | + default: |
|
172 | + $this->back(); |
|
173 | + break 2; |
|
174 | + } |
|
175 | + |
|
176 | + $this->forward(); |
|
177 | + } |
|
178 | + |
|
179 | + return $data; |
|
180 | + } |
|
181 | + |
|
182 | + /** |
|
183 | + * |
|
184 | + * @return string The current line |
|
185 | + */ |
|
186 | + public function getCurrentLine() { |
|
187 | + return $this->_line; |
|
188 | + } |
|
189 | + |
|
190 | + /** |
|
191 | + * |
|
192 | + */ |
|
193 | + public function getCurrentLineRecord($pieces = 3) { |
|
194 | + if (!is_null($this->_lineRecord)) { |
|
195 | + if ($this->_linePieces == $pieces) { |
|
196 | + return $this->_lineRecord; |
|
197 | + } |
|
198 | + } |
|
199 | + |
|
200 | + if (empty($this->_line)) { |
|
201 | + return false; |
|
202 | + } |
|
203 | + |
|
204 | + $line = trim($this->_line); |
|
205 | + |
|
206 | + $this->_lineRecord = explode(' ', $line, $pieces); |
|
207 | + $this->_linePieces = $pieces; |
|
208 | + |
|
209 | + return $this->_lineRecord; |
|
210 | + } |
|
211 | + |
|
212 | + /** |
|
213 | + * |
|
214 | + */ |
|
215 | + protected function logError($error) { |
|
216 | + $this->_errorLog[] = $error; |
|
217 | + } |
|
218 | + |
|
219 | + /** |
|
220 | + * |
|
221 | + */ |
|
222 | + public function logUnhandledRecord($additionalInfo = '') { |
|
223 | + $this->logError( |
|
224 | + $this->_linesParsed . ': (Unhandled) ' . trim(implode('|', $this->getCurrentLineRecord())) . |
|
225 | + (!empty($additionalInfo) ? ' - ' . $additionalInfo : '') |
|
226 | + ); |
|
227 | + } |
|
228 | + |
|
229 | + public function logSkippedRecord($additionalInfo = '') { |
|
230 | + $this->logError( |
|
231 | + $this->_linesParsed . ': (Skipping) ' . trim(implode('|', $this->getCurrentLineRecord())) . |
|
232 | + (!empty($additionalInfo) ? ' - ' . $additionalInfo : '') |
|
233 | + ); |
|
234 | + } |
|
235 | + |
|
236 | + /** |
|
237 | + * |
|
238 | + */ |
|
239 | + public function getErrors() { |
|
240 | + return $this->_errorLog; |
|
241 | + } |
|
242 | + |
|
243 | + /** |
|
244 | + * |
|
245 | + */ |
|
246 | + public function normalizeIdentifier($identifier) { |
|
247 | + $identifier = trim($identifier); |
|
248 | + $identifier = trim($identifier, '@'); |
|
249 | + |
|
250 | + return $identifier; |
|
251 | + } |
|
252 | + |
|
253 | + /** |
|
254 | + * |
|
255 | + * @param string $fileName |
|
256 | + * @return Gedcom |
|
257 | + */ |
|
258 | + public function parse($fileName) { |
|
259 | + $this->_file = fopen($fileName, 'r'); #explode("\n", mb_convert_encoding($contents, 'UTF-8')); |
|
260 | + |
|
261 | + if (!$this->_file) { |
|
262 | + return null; |
|
263 | + } |
|
264 | + |
|
265 | + $this->forward(); |
|
266 | + |
|
267 | + while (!$this->eof()) { |
|
268 | + $record = $this->getCurrentLineRecord(); |
|
269 | + |
|
270 | + if ($record === false) { |
|
271 | + continue; |
|
272 | + } |
|
273 | + |
|
274 | + $depth = (int) $record[0]; |
|
275 | + |
|
276 | + // We only process 0 level records here. Sub levels are processed |
|
277 | + // in methods for those data types (individuals, sources, etc) |
|
278 | + |
|
279 | + if ($depth == 0) { |
|
280 | + // Although not always an identifier (HEAD,TRLR): |
|
281 | + if (isset($record[1])) { |
|
282 | + $identifier = $this->normalizeIdentifier($record[1]); |
|
283 | + } |
|
284 | + |
|
285 | + if (isset($record[1]) && trim($record[1]) == 'HEAD') { |
|
286 | + Parser\Head::parse($this); |
|
287 | + } else if (isset($record[2]) && trim($record[2]) == 'SUBN') { |
|
288 | + Parser\Subn::parse($this); |
|
289 | + } else if (isset($record[2]) && trim($record[2]) == 'SUBM') { |
|
290 | + Parser\Subm::parse($this); |
|
291 | + } else if (isset($record[2]) && $record[2] == 'SOUR') { |
|
292 | + Parser\Sour::parse($this); |
|
293 | + } else if (isset($record[2]) && $record[2] == 'INDI') { |
|
294 | + Parser\Indi::parse($this); |
|
295 | + } else if (isset($record[2]) && $record[2] == 'FAM') { |
|
296 | + Parser\Fam::parse($this); |
|
297 | + } else if (isset($record[2]) && substr(trim($record[2]), 0, 4) == 'NOTE') { |
|
298 | + Parser\Note::parse($this); |
|
299 | + } else if (isset($record[2]) && $record[2] == 'REPO') { |
|
300 | + Parser\Repo::parse($this); |
|
301 | + } else if (isset($record[2]) && $record[2] == 'OBJE') { |
|
302 | + Parser\Obje::parse($this); |
|
303 | + } else if (isset($record[1]) && trim($record[1]) == 'TRLR') { |
|
304 | + // EOF |
|
305 | + break; |
|
306 | + } else { |
|
307 | + $this->logUnhandledRecord(get_class() . ' @ ' . __LINE__); |
|
308 | + } |
|
309 | + } else { |
|
310 | + $this->logUnhandledRecord(get_class() . ' @ ' . __LINE__); |
|
311 | + } |
|
312 | + |
|
313 | + $this->forward(); |
|
314 | + } |
|
315 | + |
|
316 | + return $this->getGedcom(); |
|
317 | + } |
|
318 | 318 | } |
319 | 319 | \ No newline at end of file |