1 | <?php |
||
8 | class Parser |
||
9 | { |
||
10 | |||
11 | public $error = []; |
||
12 | protected $blocks; |
||
13 | protected $info = []; |
||
14 | |||
15 | /** |
||
16 | * Constructor |
||
17 | * @param string $version |
||
18 | * @return void |
||
|
|||
19 | */ |
||
20 | public function __construct($version = '310') |
||
26 | |||
27 | /** |
||
28 | * Read EFD content file and return as array |
||
29 | * @param string $contentfile |
||
30 | * @return array |
||
31 | * @throws \Exception |
||
32 | */ |
||
33 | public function read($contentfile) |
||
34 | { |
||
35 | //cleaning |
||
36 | $contentfile = str_replace('__', '_', $contentfile); |
||
37 | $contentfile = str_replace(['| ', ' |'], '|', $contentfile); |
||
38 | $contentfile = str_replace(['- ', ' -'], '-', $contentfile); |
||
39 | $contentfile = str_replace('\r', '', $contentfile); |
||
40 | $contentfile = strtoupper($contentfile); |
||
41 | $contentfile = Encoding::fixUTF8($contentfile); |
||
42 | $contentfile = Strings::squashCharacters($contentfile); |
||
43 | |||
44 | $datas = $this->block($contentfile); |
||
45 | $i = 1; |
||
46 | foreach ($datas as $data) { |
||
47 | foreach ($data as $key => $d) { |
||
48 | $node = $this->blocks[$key]; |
||
49 | $vars = []; |
||
50 | if (empty($d)) { |
||
51 | continue; |
||
52 | } |
||
53 | if (count($node) !== count($d)) { |
||
54 | $this->error[] = "Erro de conteúdo da chave $key"; |
||
55 | } |
||
56 | foreach ($d as $n => $value) { |
||
57 | $name = $node[$n]; |
||
58 | $value = str_replace(',', '.', $value); |
||
59 | $value = str_replace(["\r","\t","\n"], "", $value); |
||
60 | //$value = preg_replace('/(?:\s\s+)/', ' ', $value); |
||
61 | $value = preg_replace("/[^a-zA-Z0-9 @,-_.;:\/]/", "", $value); |
||
62 | $vars[$name] = trim($value); |
||
63 | } |
||
64 | } |
||
65 | if (substr($key, 1, 2) === '99') { |
||
66 | $i++; |
||
67 | continue; |
||
68 | } |
||
69 | $this->info[$i] = [$key => $vars]; |
||
70 | $i++; |
||
71 | } |
||
72 | return $this->info; |
||
73 | } |
||
74 | |||
75 | protected function block($fields, $unique = false) |
||
101 | } |
||
102 |
Adding a
@return
annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.