-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOfcParser.php
87 lines (74 loc) · 2.31 KB
/
OfcParser.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
<?php
namespace App\Utility;
class OfcParser
{
/**
* Convertar o array com os dados em string;
*/
private function convert($url)
{
$string = '';
foreach (file($url) as $linha) {
$string .= $linha;
}
return $string;
}
private function formatArray($file)
{
$ofc = $this->convert($file);
$OFCArray = explode("<", $ofc);
$tags = array();
foreach ($OFCArray as $linha) {
$tag=explode(">", $linha);
if (isset($tag[1])) {
if ($tag[1]!=null) {
if (isset($tags[$tag[0]])) {
if (is_array($tags[$tag[0]])) {
$tags[$tag[0]][]= trim($tag[1]);
} else {
$temp=$tags[$tag[0]];
$tags[$tag[0]]= array();
$tags[$tag[0]][]= trim($temp);
$tags[$tag[0]][]= trim($tag[1]);
}
} else {
$tags[$tag[0]]= trim($tag[1]);
}
}
}
}
return $tags;
}
private function dateFormat($DTPOSTED)
{
$date = substr($DTPOSTED, 0, 4) .'-'. substr($DTPOSTED, 4, 2) .'-'. substr($DTPOSTED, 6, 2);
return date_create_from_format('Y-m-d', $date);
}
/**
* Realizar a formatação do array, retornando os itens com seus atributos
* [
*'TRNTYPE' => '0',
*'DTPOSTED' => '20200605',
*'TRNAMT' => '1030.27',
*'FITID' => '51855',
*'CHKNUM' => '51855',
*'MEMO' => 'DP DIN LOT'
*]
*/
public function build($arquivo)
{
$extrato = [];
$ofc = $this->formatArray($arquivo);
foreach ($ofc['STMTTRN'] as $index => $value) {
$extrato[] = [
'TRNTYPE' => $ofc['TRNTYPE'][$index],
'DTPOSTED' => $this->dateFormat($ofc['DTPOSTED'][$index]),
'TRNAMT' => $ofc['TRNAMT'][$index],
'FITID' => $ofc['FITID'][$index],
'CHKNUM'=> $ofc['CHKNUM'][$index],
'MEMO' => $ofc['MEMO'][$index]
];
}
return $extrato;
}
}