-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile_reader.class.php
280 lines (246 loc) · 9.54 KB
/
file_reader.class.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
<?php
namespace andmaengine\bank_engine;
/**
* @description-pt-BR Leitor de arquivo de extrato bancário eletrônico
* @description-en-US File reader of eletronic bank statement
* @Category andmaengine
* @Package andmaengine\bank_engine
* @Copyright Copyright (c) 2017-2030
* @Language PHP
* @Updated To 7.2.0
* @license
* @author Anderson Matheus Arruda < andmarruda at gmail dot com >
* @link
**/
use \SplFileObject;
use \Exception;
use andmaengine\bank_engine\utilities as utilities;
require_once(__DIR__. '/utilities.class.php');
class file_reader{
// ------------ PRIVATE PARAMETERS ------------ //
/**
* @name file_pattern
* @description-pt-BR Padrão de dados do arquivo
* @description-en-US File's data pattern
* @var array
**/
private $file_pattern=array(
'text/plan' => 'reader_text_plan'
);
/**
* @name file_inforamtion
* @description-pt-BR Informações contidas no arquivo
* @description-en-US File's contained information
**/
private $file_information;
/**
* @name lang
* @description-pt-BR Lingua utilizada nas Exception e retornos
* @description-en-US Utilized language on Exception and return
**/
private $lang;
/**
* @name reader_configuration
* @description-pt-BR Configurações do leitor
* @description-en-US Reader's configuration
**/
private $reader_configuration;
/**
* @name configuration_class
* @description-pt-BR Classe que contém as configurações de leitura
* @description-en-US Class that contents the reader's configuration
**/
private $configuration_class;
/**
* @name remove_whitespace
* @description-pt-BR Seta para remover todos os espaços em branco do início e fim da string automáticamente
* @description-en-US Set to remove all the white space of begin and end of string automatically
**/
private $remove_whitespace;
// ------------ PRIVATE FUNCTIONS ------------ //
/**
* @description-pt-BR Lê a linha e converte para array
* @description-en-US Read the line and converts to array
* @version 1.0.0
* @access private
* @name read_line_default
* @author Anderson Matheus Arruda < andmarruda at gmail dot com >
* @modifyBy Anderson Matheus Arruda < andmarruda at gmail dot com >
* @modifyDate 2018-01-21 01:32 GMT -03:00
* @modifyDescription-pt-BR Adicionado recurso para remover todos espaço em branco do início e fim da string automáticamente
* @modifyDescription-en-US Added resource to remove all the white space of begin and end of string automatically
* @param array[(string) column_name => 'initial_position' => int, 'length' => int, 'type' => string, 'convert' => string, 'decimalLength' => int] $reader_configuration
* @param string $line_content
* @return void
**/
private function read_line_default(array $reader_configuration, string $line_content, array $file_information_key) : void
{
$line_information = array();
array_walk($reader_configuration, function($config, $fieldname) use(&$line_information, $line_content){
$info=substr($line_content, $config['initial_position'] -1, $config['length']);
if($this->remove_whitespace)
$info=trim($info);
if(!isset($config['convert'])){
$info=utilities::convert_to_type($config['type'], $info);
} else{
if(isset($config['date_format']))
utilities::$date_format=$config['date_format'];
if(isset($config['decimal_length']))
utilities::$decimal_length=$config['decimal_length'];
$func='andmaengine\bank_engine\utilities::convert_'. $config['convert'];
$info=$func($info);
}
$line_information[$fieldname]=$info;
});
return $line_information;
}
/**
* @description-pt-BR Converte a linha do arquivo por posição de substr
* @description-en-US Converts the file's line by substr's position
* @version 1.0.0
* @access private
* @name reader_default
* @author Anderson Matheus Arruda < andmarruda at gmail dot com >
* @modifyBy
* @modifyDescription
* @param string $line_content
* @return void
**/
private function reader_default(string $line_content){
$configuration_dimensions=$this->configuration_class->get_configuration_dimensions();
$reader_configuration = $this->reader_configuration;
$file_information_key = array();
if(is_array($configuration_dimensions) && sizeof($configuration_dimensions) > 0){
$continue=true;
$arrayDim='$actual_file_information';
$actual_file_information=$this->file_information;
array_walk($configuration_dimensions, function($dimension, $func) use(&$continue, $line_content, &$reader_configuration, &$file_information_key, &$arrayDim){
$func = 'andmaengine\bank_engine\utilities::'. $func;
$dim = $func($dimension, $line_content);
if($continue && array_key_exists($dim, $reader_configuration)){
$reader_configuration = $reader_configuration[$dim];
$file_information_key[]=$dim;
$arrayDim.='[\''. $dim. '\']';
}
else
$continue = false;
});
if($continue)
{
$row_information = $this->read_line_default($reader_configuration, $line_content, $file_information_key);
eval($arrayDim. '[]=$row_information;');
$this->file_information=$actual_file_information;
}
} else{
$this->file_information[]=$this->read_line_default($reader_configuration, $line_content, $file_information_key);
}
}
/**
* @description-pt-BR Converte a linha do arquivo para array em modo text/plan
* @description-en-US Converts the file's line to an array on text/plan mode
* @version 1.0.0
* @access private
* @name reader_text_plan
* @author Anderson Matheus Arruda < andmarruda at gmail dot com >
* @modifyBy
* @modifyDescription
* @param string $line_content
* @return void
**/
private function reader_text_plan(string $line_content) : void
{
$this->reader_configuration=$this->configuration_class->get_configuration();
reset($this->reader_configuration);
$condition=key($this->reader_configuration);
switch(strtolower($condition)){
case 'separator':
$this->reader_with_separator($line_content);
break;
default:
$this->reader_default($line_content);
}
}
// ------------ PUBLIC FUNCTIONS ------------ //
/**
* @description-pt-BR Recebe informações do idioma utilizada e classe de configurações
* @description-en-US Receive information of utilized language and configuration's class
* @version 1.0.0
* @access public
* @name ` __construct
* @author Anderson Matheus Arruda < andmarruda at gmail dot com >
* @param $lang Instance Of Language
* @param $configuration Class with reader's configuration
* @modifyBy
* @modifyDescription
* @return void
**/
public function __construct(language $lang, $configuration){
$this->lang = $lang;
$class_interface = class_implements($configuration);
if(in_array('andmaengine\bank_engine\configuration', $class_interface)){
$this->configuration_class = $configuration;
} else{
$errText = $this->lang->get_text('configuration_exception');
throw new Exception($errText);
}
}
/**
* @description-pt-BR Retorna o valor da array com o conteúdo do arquivo
* @description-en-US Returns the array's value with file's content
* @version 1.0.0
* @access public
* @name ` get_file_information
* @author Anderson Matheus Arruda < andmarruda at gmail dot com >
* @modifyBy
* @modifyDescription
* @param
* @param
* @return array[mixed]
**/
public function get_file_information(bool $as_json=false)
{
return (!$as_json) ? $this->file_information : json_encode($this->file_information);
}
/**
* @description-pt-BR Recebe dados de entrada para validar tipos
* @description-en-US Receive incoming data for type validation
* @version 1.0.1
* @access public
* @name ` reader
* @author Anderson Matheus Arruda < andmarruda at gmail dot com >
* @modifyBy Anderson Matheus Arruda < andmarruda at gmail dot com >
* @modifyDate 2018-01-21 01:31 GMT -03:00
* @modifyDescription-pt-BR Adicionado recurso para remover todos espaço em branco do início e fim da string automáticamente
* @modifyDescription-en-US Added resource to remove all the white space of begin and end of string automatically
* @param string $file_pattern
* @param string $file_path
* @param bool $remove_whitespace "default true"
* @return void
**/
public function reader(string $file_pattern, string $file_path, bool $remove_whitespace=true) : void
{
$error=NULL;
if(is_null($error) && !array_key_exists($file_pattern, $this->file_pattern)){
$error = 'reader_pattern_exception';
}
if(is_null($error) && !file_exists($file_path)){
$error = 'reader_file_path_expcetion';
}
$file_control = new SplFileObject($file_path);
if(is_null($error) && !$file_control->isFile()){
$error = 'splfileobject_not_file';
}
if(is_null($error) && !$file_control->isReadable()){
$error = 'splfileobject_file_not_readable';
}
if(!is_null($error)){
$errText = $this->lang->get_text($error);
throw new Exception($errText);
}
$this->remove_whitespace = $remove_whitespace;
$funcname=$this->file_pattern[$file_pattern];
while(!$file_control->eof())
$this->$funcname($file_control->fgets());
}
}
?>