11<?php
22
3+ declare(strict_types=1);
4+
35/**
46 * RaiffeisenBank - Statements handler class
57 *
1214namespace VitexSoftware\Raiffeisenbank;
1315
1416/**
15- * Description of Statementor
17+ * Description of Statementor.
1618 *
1719 * @author vitex
1820 */
1921class Statementor extends \Ease\Sand
2022{
2123 use \Ease\Logger\Logging;
22-
2324 public \DateTime $since ;
24-
2525 public \DateTime $until ;
2626
2727 /**
28- * DateTime Formating eg. 2021-08-01T10:00:00.0Z
29- * @var string
28+ * DateTime Formating eg. 2021-08-01T10:00:00.0Z.
3029 */
31- public static $dateTimeFormat = ' Y-m-d\\ TH:i:s.0\\ Z' ;
30+ public static string $dateTimeFormat = ' Y-m-d\\ TH:i:s.0\\ Z' ;
3231
3332 /**
34- * DateTime Formating eg. 2021-08-01T10:00:00.0Z
35- * @var string
33+ * DateTime Formating eg. 2021-08-01T10:00:00.0Z.
3634 */
37- public static $dateFormat = ' Y-m-d' ;
35+ public static string $dateFormat = ' Y-m-d' ;
3836 private string $accountNumber = ' ' ;
37+ private string $statementLine = ' MAIN' ;
3938
4039 public function __construct(string $accountNumber = ' ' , string $scope = ' ' )
4140 {
@@ -46,33 +45,56 @@ class Statementor extends \Ease\Sand
4645 if ($scope) {
4746 $this -> setScope ($scope );
4847 }
49-
5048 }
5149
5250 /**
53- * Set AccountNumber for further operations
51+ * Set AccountNumber for further operations.
5452 *
5553 * @param string $accountNumber
5654 *
5755 * @return Statementor
5856 */
59- public function setAccountNumber($accountNumber)
57+ public function setAccountNumber($accountNumber): self
6058 {
6159 $this -> accountNumber = $accountNumber ;
62- $this -> setObjectName ($accountNumber . ' @' . get_class($this ));
60+ $this -> setObjectName ($accountNumber .' @' .\get_class($this ));
61+
62+ return $this ;
63+ }
64+
65+ /**
66+ * Set "Statement Line"
67+ *
68+ * @param string $line MAIN or ADDITINAL
69+ *
70+ * @return self
71+ *
72+ * @throws \InvalidArgumentException
73+ */
74+ public function setStatementLine(string $line): self
75+ {
76+ switch ($line ) {
77+ case ' MAIN' :
78+ case ' ADDITIONAL' :
79+ $this -> statementLine = $line ;
80+
81+ break ;
82+
83+ default :
84+ throw new \InvalidArgumentException(' Wrong statement line: ' .$line );
85+ }
6386 return $this;
6487 }
6588
6689 /**
67- * Obtain Statements from RB
90+ * Obtain Statements from RB.
6891 *
69- * @param string $currencyCode CZK,USD etc
70- * @param string $statementLine
71- *
72- * @return array
92+ * @param string $currencyCode CZK,USD etc
93+ * @param string $statementLine default statement line override
7394 */
74- public function getStatements($currencyCode = 'CZK', $statementLine = 'MAIN '): array
95+ public function getStatements($currencyCode = 'CZK', string $statementLine = ''): array
7596 {
97+ $statementLineFinal = empty($statementLine ) ? $this -> statementLine : $statementLine ;
7698 $apiInstance = new PremiumAPI\GetStatementListApi();
7799 $page = 0;
78100 $statements = [];
@@ -85,30 +107,36 @@ class Statementor extends \Ease\Sand
85107 ' page' => ++$page ,
86108 ' size' => 60,
87109 ' currency' => $currencyCode ,
88- ' statementLine' => $statementLine ,
110+ ' statementLine' => $statementLineFinal ,
89111 ' dateFrom' => $this -> since -> format (self::$dateFormat ),
90112 ' dateTo' => $this -> until -> format (self::$dateFormat )]);
91113
92114 $result = $apiInstance -> getStatements (ApiClient::getxRequestId(), $requestBody , $page );
115+
93116 if (empty($result )) {
94117 $this -> addStatusMessage (sprintf(_(' No transactions from %s to %s' ), $this -> since -> format (self::$dateFormat ), $this -> until -> format (self::$dateFormat )));
95118 $result [' lastPage' ] = true ;
96119 $result [' last' ] = true ;
97120 }
98- if (array_key_exists('statements', $result)) {
121+
122+ if (\array_key_exists('statements', $result)) {
99123 $statements = array_merge($statements , $result [' statements' ]);
100124 }
125+
101126 sleep(1);
102127 } while ($result['last'] === false);
103128 } catch (\Ease\Exception $e) {
104- echo ' Exception when calling GetTransactionListApi->getTransactionList: ' , $e -> getMessage (), PHP_EOL;
129+ echo ' Exception when calling GetTransactionListApi->getTransactionList: ' , $e -> getMessage (), \ PHP_EOL;
105130 }
131+
106132 return $statements;
107133 }
108134
109135 /**
110136 * Prepare processing interval.
111137 *
138+ * @param mixed $scope
139+ *
112140 * @throws \Exception
113141 */
114142 public function setScope($scope): \DatePeriod
@@ -193,7 +221,6 @@ class Statementor extends \Ease\Sand
193221 if (preg_match(' /^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$/' , $scope )) {
194222 $this -> since = new \DateTime($scope );
195223 $this -> until = (new \DateTime($scope ))-> setTime (23, 59, 59, 999);
196-
197224 }
198225
199226 throw new \InvalidArgumentException('Unknown scope '.$scope);
@@ -206,61 +233,58 @@ class Statementor extends \Ease\Sand
206233 $this -> since = $this -> since -> setTime (0, 0);
207234 $this -> until = $this -> until -> setTime (23, 59, 59, 999);
208235 }
236+
209237 return new \DatePeriod($this->since, new \DateInterval('P1D'), $this->until);
210238 }
211239
212240 /**
213- * Save Statement PDF files
241+ * Save Statement PDF files.
214242 *
215- * @param string $saveTo
216- * @param array<mixed > $statements - produced by getStatements() function
217- * @param string $format pdf|xml
218- * @param string $currencyCode
219- *
220- * @return array
243+ * @param array<mixed > $statements - produced by getStatements() function
244+ * @param string $format pdf|xml
221245 */
222246 public function download(string $saveTo, array $statements, string $format = 'pdf', string $currencyCode = 'CZK'): array
223247 {
224248 $saved = [];
225249 $apiInstance = new PremiumAPI\DownloadStatementApi();
226250 $success = 0;
251+
227252 foreach ($statements as $statement ) {
228- $statementFilename = str_replace(' /' , ' _' , $statement -> statementNumber ) . ' _' .
229- $statement -> accountNumber . ' _' .
230- $statement -> accountId . ' _' .
231- $statement -> currency . ' _' . $statement -> dateFrom . ' .' . $format ;
253+ $statementFilename = str_replace(' /' , ' _' , $statement -> statementNumber ). ' _' .
254+ $statement -> accountNumber . ' _' .
255+ $statement -> accountId . ' _' .
256+ $statement -> currency . ' _' . $statement -> dateFrom . ' .' . $format ;
232257 $requestBody = new \VitexSoftware\Raiffeisenbank\Model\DownloadStatementRequest([
233258 ' accountNumber' => $this -> accountNumber ,
234259 ' currency' => $currencyCode ,
235260 ' statementId' => $statement -> statementId ,
236261 ' statementFormat' => $format ]);
237262 $pdfStatementRaw = $apiInstance -> downloadStatement (ApiClient::getxRequestId(), ' cs' , $requestBody );
238263 sleep(1);
239- if (file_put_contents($saveTo . ' /' . $statementFilename , $pdfStatementRaw -> fread ($pdfStatementRaw -> getSize ()))) {
240- $saved [$statementFilename ] = $saveTo . ' /' . $statementFilename ;
241- $this -> addStatusMessage ($statementFilename . ' saved' , ' success' );
264+
265+ if (file_put_contents($saveTo .' /' .$statementFilename , $pdfStatementRaw -> fread ($pdfStatementRaw -> getSize ()))) {
266+ $saved [$statementFilename ] = $saveTo .' /' .$statementFilename ;
267+ $this -> addStatusMessage ($statementFilename .' saved' , ' success' );
242268 unset($pdfStatementRaw );
243- $ success ++ ;
269+ ++ $ success ;
244270 }
245271 }
246- $this->addStatusMessage('Download done. ' . $success . ' of ' . count($statements) . ' saved');
272+
273+ $this->addStatusMessage('Download done. '.$success.' of '.\count($statements).' saved');
274+
247275 return $saved;
248276 }
249277
250278 /**
251- * Since time getter
252- *
253- * @return \DateTime
279+ * Since time getter.
254280 */
255281 public function getSince(): \DateTime
256282 {
257283 return $this -> since ;
258284 }
259285
260286 /**
261- * Until time getter
262- *
263- * @return \DateTime
287+ * Until time getter.
264288 */
265289 public function getUntil(): \DateTime
266290 {
0 commit comments