|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace h4kuna\Fio\Request\Read\Files; |
|
4
|
|
|
|
|
5
|
|
|
use h4kuna\Fio\Request, |
|
6
|
|
|
h4kuna\Fio\Response\Read, |
|
7
|
|
|
Nette; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* @author Milan Matějček |
|
11
|
|
|
*/ |
|
12
|
|
|
class Json implements Request\Read\IReader |
|
13
|
|
|
{ |
|
14
|
|
|
|
|
15
|
|
|
/** @var Read\ITransactionListFactory */ |
|
16
|
|
|
private $transactionListFactory; |
|
17
|
|
|
|
|
18
|
|
|
public function __construct(Read\ITransactionListFactory $transactionListFactory) |
|
19
|
|
|
{ |
|
20
|
|
|
$this->transactionListFactory = $transactionListFactory; |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
/** @return string */ |
|
24
|
|
|
public function getExtension() |
|
25
|
|
|
{ |
|
26
|
|
|
return self::JSON; |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @param string $data |
|
31
|
|
|
* @return Read\TransactionList |
|
32
|
|
|
*/ |
|
33
|
|
|
public function create($data) |
|
34
|
|
|
{ |
|
35
|
|
|
if (!$data) { |
|
36
|
|
|
$data = '{}'; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
if (self::isJsonBug()) { |
|
40
|
|
|
// all float values are transform to string |
|
41
|
|
|
// bug for php7.1 https://bugs.php.net/bug.php?id=72567 |
|
42
|
|
|
$data = preg_replace('~: ?(-?\d+\.\d+),~', ':"$1",', $data); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
$dateFormat = 'Y-m-dO'; |
|
46
|
|
|
$json = Nette\Utils\Json::decode($data); |
|
47
|
|
|
if (isset($json->accountStatement->info)) { |
|
48
|
|
|
$info = $this->transactionListFactory->createInfo($json->accountStatement->info, $dateFormat); |
|
49
|
|
|
} else { |
|
50
|
|
|
$info = new \stdClass(); |
|
51
|
|
|
} |
|
52
|
|
|
$transactionList = $this->transactionListFactory->createTransactionList($info); |
|
53
|
|
|
if (!isset($json->accountStatement->transactionList)) { |
|
54
|
|
|
return $transactionList; |
|
55
|
|
|
} |
|
56
|
|
|
foreach ($json->accountStatement->transactionList->transaction as $transactionData) { |
|
57
|
|
|
$row = $this->transactionListFactory->createTransaction($transactionData, $dateFormat); |
|
58
|
|
|
$transactionList->append($row); |
|
59
|
|
|
$row->clearTemporaryValues(); |
|
60
|
|
|
} |
|
61
|
|
|
return $transactionList; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @internal |
|
66
|
|
|
* @return bool |
|
67
|
|
|
*/ |
|
68
|
|
|
public static function isJsonBug() |
|
69
|
|
|
{ |
|
70
|
|
|
return PHP_VERSION_ID >= 70100; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
} |
|
74
|
|
|
|