forked from nemiah/phpFinTS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGetBalance.php
More file actions
127 lines (113 loc) · 3.95 KB
/
GetBalance.php
File metadata and controls
127 lines (113 loc) · 3.95 KB
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
<?php
namespace Fhp\Action;
use Fhp\Model\SEPAAccount;
use Fhp\PaginateableAction;
use Fhp\Protocol\BPD;
use Fhp\Protocol\Message;
use Fhp\Protocol\UnexpectedResponseException;
use Fhp\Protocol\UPD;
use Fhp\Segment\BaseSegment;
use Fhp\Segment\Common\Kti;
use Fhp\Segment\Common\Kto;
use Fhp\Segment\Common\KtvV3;
use Fhp\Segment\SAL\HISAL;
use Fhp\Segment\SAL\HKSALv4;
use Fhp\Segment\SAL\HKSALv5;
use Fhp\Segment\SAL\HKSALv6;
use Fhp\Segment\SAL\HKSALv7;
use Fhp\UnsupportedException;
/**
* Runs an HKSAL request the current balance of the given account.
*/
class GetBalance extends PaginateableAction
{
// Request (if you add a field here, update __serialize() and __unserialize() as well).
/** @var SEPAAccount */
private $account;
/** @var bool */
private $allAccounts;
// Response
/** @var HISAL[] */
private $response = [];
/**
* @param SEPAAccount $account The account to get the balance for. This can be constructed based on information
* that the user entered, or it can be {@link SEPAAccount} instance retrieved from {@link GetSEPAAccounts}.
* @param bool $allAccounts If set to true, will return balances for all accounts of the user. You still need to
* pass one of the accounts into $account, though.
*/
public static function create(SEPAAccount $account, bool $allAccounts = false): GetBalance
{
$result = new GetBalance();
$result->account = $account;
$result->allAccounts = $allAccounts;
return $result;
}
/**
* @deprecated Beginning from PHP7.4 __unserialize is used for new generated strings, then this method is only used for previously generated strings - remove after May 2023
*/
public function serialize(): string
{
return serialize($this->__serialize());
}
public function __serialize(): array
{
return [
parent::__serialize(),
$this->account, $this->allAccounts,
];
}
/**
* @deprecated Beginning from PHP7.4 __unserialize is used for new generated strings, then this method is only used for previously generated strings - remove after May 2023
*
* @param string $serialized
* @return void
*/
public function unserialize($serialized)
{
self::__unserialize(unserialize($serialized));
}
public function __unserialize(array $serialized): void
{
list(
$parentSerialized,
$this->account, $this->allAccounts,
) = $serialized;
is_array($parentSerialized) ?
parent::__unserialize($parentSerialized) :
parent::unserialize($parentSerialized);
}
/**
* @return HISAL[]
*/
public function getBalances()
{
$this->ensureDone();
return $this->response;
}
protected function createRequest(BPD $bpd, ?UPD $upd)
{
/** @var BaseSegment $hisals */
$hisals = $bpd->requireLatestSupportedParameters('HISALS');
switch ($hisals->getVersion()) {
case 4:
return HKSALv4::create(Kto::fromAccount($this->account));
case 5:
return HKSALv5::create(KtvV3::fromAccount($this->account), $this->allAccounts);
case 6:
return HKSALv6::create(KtvV3::fromAccount($this->account), $this->allAccounts);
case 7:
return HKSALv7::create(Kti::fromAccount($this->account), $this->allAccounts);
default:
throw new UnsupportedException('Unsupported HKSAL version: ' . $hisals->getVersion());
}
}
public function processResponse(Message $response)
{
parent::processResponse($response);
$responseSegments = $response->findSegments(HISAL::class);
if (count($responseSegments) === 0) {
throw new UnexpectedResponseException('No HISAL segments received!');
}
$this->response = array_merge($this->response, $responseSegments);
}
}