1 | <?php |
||
10 | class Reader |
||
11 | { |
||
12 | /** |
||
13 | * @param string $filename |
||
14 | * |
||
15 | * @return Document |
||
16 | */ |
||
17 | 1 | public function readFile($filename) |
|
27 | |||
28 | /** |
||
29 | * @param string $line |
||
30 | * |
||
31 | * @return string|null |
||
32 | */ |
||
33 | protected function parseName($line) |
||
41 | |||
42 | /** |
||
43 | * @param Document $document |
||
44 | * @param string[] $lines |
||
45 | */ |
||
46 | protected function parseColumns(Document $document, array $lines) |
||
47 | { |
||
48 | foreach ($lines as $line) { |
||
49 | if (preg_match('/ATTRIBUTE\s([a-zA-Z0-9_-]+)\s(.*)/i', $line, $matches)) { |
||
50 | $type = $matches[2]; |
||
51 | $column = null; |
||
52 | if (strcasecmp($type, 'string') === 0) { |
||
53 | $column = new StringColumn($matches[1]); |
||
54 | } else if (strcasecmp($type, 'numeric') === 0) { |
||
55 | $column = new NumericColumn($matches[1]); |
||
56 | } else if (preg_match('/^\{(.*)\}$/', $matches[2], $classMatches)) { |
||
57 | $column = new NominalColumn($matches[1], array_map(function ($value) { |
||
58 | return trim($value, "'"); |
||
59 | }, preg_split( |
||
60 | "/,(?=(?:[^\']*\'[^\']*\')*(?![^\']*\'))/", |
||
61 | $classMatches[1] |
||
62 | ) |
||
63 | )); |
||
64 | } else if (preg_match('/date\s\"/', $matches[2])) { |
||
65 | preg_match('/date\s"([A-Za-z0-9-: ]+)"/', $line, $dateMatches); |
||
66 | $column = new DateColumn($matches[1], $dateMatches[1]); |
||
67 | } |
||
68 | if ($column) { |
||
69 | $document->addColumn($column); |
||
70 | } |
||
71 | } |
||
72 | } |
||
73 | } |
||
74 | |||
75 | /** |
||
76 | * @param Document $document |
||
77 | * @param string[] $lines |
||
78 | */ |
||
79 | protected function parseData(Document $document, array $lines) |
||
108 | } |
||
109 |
If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration: