1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SmartCNAB\Services; |
4
|
|
|
|
5
|
|
|
use InvalidArgumentException; |
6
|
|
|
|
7
|
|
|
use SmartCNAB\Contracts\FactoryInterface; |
8
|
|
|
use SmartCNAB\Support\Bank; |
9
|
|
|
use SmartCNAB\Support\File\Inspector; |
10
|
|
|
use SmartCNAB\Support\Picture; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* SmartCNAB files factory |
14
|
|
|
*/ |
15
|
|
|
class Factory implements FactoryInterface |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* Return an instance of a remittance. |
19
|
|
|
* |
20
|
|
|
* @param integer $bank |
21
|
|
|
* @param integer $version |
22
|
|
|
* @return \SmartCNAB\Contracts\File\RemittanceInterface |
23
|
|
|
*/ |
24
|
|
|
public function remittance($bank, $version) |
25
|
|
|
{ |
26
|
|
|
$bankNs = $this->discoverBankNamespace($bank); |
27
|
|
|
$file = "File{$version}"; |
28
|
|
|
$class = $bankNs . $file; |
29
|
|
|
|
30
|
|
|
return new $class(new Picture()); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Return an instance of a returning. |
35
|
|
|
* |
36
|
|
|
* @param string $path |
37
|
|
|
* @param integer $bank |
38
|
|
|
* @return \SmartCNAB\Contracts\File\Returning |
39
|
|
|
*/ |
40
|
|
|
public function returning($path, $bank = null) |
41
|
|
|
{ |
42
|
|
|
$bank = $bank ?: Inspector::bankNumberOf($path); |
43
|
|
|
$bankNs = $this->discoverBankNamespace($bank, 'Returning'); |
44
|
|
|
$version = Inspector::fileVersionOf($path); |
45
|
|
|
$file = "File{$version}"; |
46
|
|
|
$class = $bankNs . $file; |
47
|
|
|
|
48
|
|
|
return new $class($path, new Picture()); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Discover and return a bank namespace with received bank code. |
53
|
|
|
* |
54
|
|
|
* @param integer $bank |
55
|
|
|
* @param string $type If remittance or returning |
56
|
|
|
* @return string |
57
|
|
|
* @throws \InvalidArgumentException |
58
|
|
|
*/ |
59
|
|
|
protected function discoverBankNamespace($bank, $type = 'Remittances') |
60
|
|
|
{ |
61
|
|
|
if ( ! Bank::isHandled($bank)) { |
62
|
|
|
throw new InvalidArgumentException('Unable to handle bank ' . $bank); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
$name = Bank::nameOfNumber($bank); |
66
|
|
|
|
67
|
|
|
return "\SmartCNAB\Services\\{$type}\Banks\\{$name}\\"; |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|