1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace h4kuna\Fio\Response\Read; |
4
|
|
|
|
5
|
|
|
use h4kuna\Fio, |
6
|
|
|
h4kuna\Fio\Utils; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* @author Milan Matějček |
10
|
|
|
*/ |
11
|
|
|
class JsonTransactionFactory implements ITransactionListFactory |
12
|
|
|
{ |
13
|
|
|
|
14
|
|
|
/** @var string[] */ |
15
|
|
|
private static $property; |
16
|
|
|
|
17
|
|
|
/** @var string */ |
18
|
|
|
private $transactionClass; |
19
|
|
|
|
20
|
|
|
/** @var bool */ |
21
|
|
|
protected $transactionClassCheck = false; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @param string $transactionClass |
25
|
|
|
*/ |
26
|
|
|
public function __construct($transactionClass = null) |
27
|
|
|
{ |
28
|
|
|
if ($transactionClass === null) { |
29
|
|
|
$transactionClass = __NAMESPACE__ . '\Transaction'; |
30
|
|
|
} |
31
|
|
|
$this->transactionClass = $transactionClass; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function createInfo($data, $dateFormat) |
35
|
|
|
{ |
36
|
|
|
$data->dateStart = Utils\Strings::createFromFormat($data->dateStart, $dateFormat); |
37
|
|
|
$data->dateEnd = Utils\Strings::createFromFormat($data->dateEnd, $dateFormat); |
38
|
|
|
return $data; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function createTransaction($data, $dateFormat) |
42
|
|
|
{ |
43
|
|
|
$transaction = $this->createTransactionObject($dateFormat); |
|
|
|
|
44
|
|
|
foreach (self::metaProperty($transaction) as $id => $meta) { |
45
|
|
|
$value = isset($data->{'column' . $id}) ? $data->{'column' . $id}->value : null; |
46
|
|
|
$transaction->bindProperty($meta['name'], $meta['type'], $value); |
47
|
|
|
} |
48
|
|
|
return $transaction; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** @return TransactionList */ |
52
|
|
|
public function createTransactionList($info) |
53
|
|
|
{ |
54
|
|
|
return new TransactionList($info); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
protected function createTransactionObject($dateFormat) |
58
|
|
|
{ |
59
|
|
|
if ($this->transactionClassCheck === false) { |
60
|
|
|
if (is_string($this->transactionClass)) { |
61
|
|
|
$class = $this->transactionClass; |
62
|
|
|
$this->transactionClass = new $class($dateFormat); |
|
|
|
|
63
|
|
|
} else { |
64
|
|
|
throw new Fio\InvalidArgumentException('Add you class as string.'); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
if (!$this->transactionClass instanceof TransactionAbstract) { |
68
|
|
|
throw new Fio\TransactionExtendException('Transaction class must extends TransationAbstract.'); |
69
|
|
|
} |
70
|
|
|
$this->transactionClassCheck = true; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
return clone $this->transactionClass; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
private static function metaProperty($class) |
77
|
|
|
{ |
78
|
|
|
if (self::$property !== null) { |
79
|
|
|
return self::$property; |
80
|
|
|
} |
81
|
|
|
$reflection = new \ReflectionClass($class); |
82
|
|
|
if (!preg_match_all('/@property-read (?P<type>[\w|]+) \$(?P<name>\w+).*\[(?P<id>\d+)\]/', $reflection->getDocComment(), $find)) { |
83
|
|
|
throw new Fio\TransactionPropertyException('Property not found you have bad syntax.'); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
self::$property = []; |
87
|
|
|
foreach ($find['name'] as $key => $property) { |
88
|
|
|
self::$property[$find['id'][$key]] = ['type' => strtolower($find['type'][$key]), 'name' => $property]; |
89
|
|
|
} |
90
|
|
|
return self::$property; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
} |
94
|
|
|
|