1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SmartCNAB\Support\File; |
4
|
|
|
|
5
|
|
|
use SmartCNAB\Contracts\File\RemittanceInterface; |
6
|
|
|
use SmartCNAB\Support\Picture; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Base remittances class. |
10
|
|
|
*/ |
11
|
|
|
class Remittance extends File implements RemittanceInterface |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* Picture instance. |
15
|
|
|
* |
16
|
|
|
* @var \SmartCNAB\Support\Picture |
17
|
|
|
*/ |
18
|
|
|
protected $picture; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Parsed schema. |
22
|
|
|
* |
23
|
|
|
* @var array |
24
|
|
|
*/ |
25
|
|
|
protected $schema; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Lines sequential. |
29
|
|
|
* |
30
|
|
|
* @var integer |
31
|
|
|
*/ |
32
|
|
|
protected $sequential = 0; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Initialize and return a new instance. |
36
|
|
|
* |
37
|
|
|
* @param \SmartCNAB\Support\Picture $picture |
38
|
|
|
*/ |
39
|
|
|
public function __construct(Picture $picture) |
40
|
|
|
{ |
41
|
|
|
$this->picture = $picture; |
42
|
|
|
$this->schema = $this->parseSchema(); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Add some detail data for file. |
47
|
|
|
* |
48
|
|
|
* @param array $data |
49
|
|
|
* @return \SmartCNAB\Support\File\Remittance |
50
|
|
|
*/ |
51
|
|
|
public function addDetail(array $data) |
52
|
|
|
{ |
53
|
|
|
$data = $this->increment($data); |
54
|
|
|
$data = $this->formatLine($data); |
55
|
|
|
$this->addLine($data); |
56
|
|
|
|
57
|
|
|
return $this; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Set data for file header build. |
62
|
|
|
* |
63
|
|
|
* @param array $data |
64
|
|
|
* @return \SmartCNAB\Support\File\Remittance |
65
|
|
|
*/ |
66
|
|
|
public function begin(array $data) |
67
|
|
|
{ |
68
|
|
|
$data = $this->increment($data); |
69
|
|
|
$data = $this->formatLine($data, 'header'); |
70
|
|
|
$this->addLine($data); |
71
|
|
|
|
72
|
|
|
return $this; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Ends a file with trailer. |
77
|
|
|
* |
78
|
|
|
* @return \SmartCNAB\Support\File\Remittance |
79
|
|
|
*/ |
80
|
|
|
public function end() |
81
|
|
|
{ |
82
|
|
|
$data = $this->increment([]); |
83
|
|
|
$data = $this->formatLine($data, 'trailer'); |
84
|
|
|
$this->addLine($data); |
85
|
|
|
$this->addLine(['']); |
86
|
|
|
|
87
|
|
|
return $this; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Return the parsed schema. |
92
|
|
|
* |
93
|
|
|
* @return array |
94
|
|
|
*/ |
95
|
|
|
public function getSchema() |
96
|
|
|
{ |
97
|
|
|
return $this->schema; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Add some detail data for file. |
102
|
|
|
* |
103
|
|
|
* @param array $data |
104
|
|
|
* @return \SmartCNAB\Support\File\Remittance |
105
|
|
|
*/ |
106
|
|
|
protected function addLine(array $data) |
107
|
|
|
{ |
108
|
|
|
$this->lines[] = $data; |
109
|
|
|
|
110
|
|
|
return $this; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Format the line data received using the schema. |
115
|
|
|
* |
116
|
|
|
* @param array $data |
117
|
|
|
* @param string $type |
118
|
|
|
* @return array |
119
|
|
|
*/ |
120
|
|
View Code Duplication |
protected function formatLine(array $data, $type = 'detail') |
121
|
|
|
{ |
122
|
|
|
$metas = $this->schema[$type]; |
123
|
|
|
$fields = array_keys($metas); |
124
|
|
|
|
125
|
|
|
$formatted = array_map( |
126
|
|
|
$this->getFormatMapper($data, $type), |
127
|
|
|
$metas, |
128
|
|
|
$fields |
129
|
|
|
); |
130
|
|
|
|
131
|
|
|
return array_combine($fields, $formatted); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Create and returns a new line format mapper using received parameters. |
136
|
|
|
* |
137
|
|
|
* @param array $data |
138
|
|
|
* @param string $type |
139
|
|
|
* @return Closure |
140
|
|
|
*/ |
141
|
|
|
protected function getFormatMapper(array $data, $type) |
142
|
|
|
{ |
143
|
|
|
return function ($meta, $field) use ($data, $type) { |
144
|
|
|
$value = empty($data[$field]) ? '' : $data[$field]; |
145
|
|
|
$method = 'mutate' . ucfirst($type) . ucfirst($field); |
146
|
|
|
|
147
|
|
View Code Duplication |
if (method_exists($this, $method)) { |
148
|
|
|
$value = call_user_func([$this, $method], $value, $data, $meta); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
return $this->picture->to($meta['pic'], $value, $meta); |
152
|
|
|
}; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* Increment and return the data. |
157
|
|
|
* |
158
|
|
|
* @param array $data |
159
|
|
|
* @return array |
160
|
|
|
*/ |
161
|
|
|
protected function increment(array $data) |
162
|
|
|
{ |
163
|
|
|
$data['sequential'] = ++$this->sequential; |
164
|
|
|
|
165
|
|
|
return $data; |
166
|
|
|
} |
167
|
|
|
} |
168
|
|
|
|