Completed
Push — master ( 589fec...539028 )
by Milan
07:58
created

TransactionAbstract::is32bitOS()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
rs 10
cc 1
nc 1
nop 0
1
<?php declare(strict_types=1);
2
3
namespace h4kuna\Fio\Response\Read;
4
5
use h4kuna\Fio\Exceptions;
6
use h4kuna\Fio\Utils;
7
8
abstract class TransactionAbstract implements \Iterator
9
{
10
11
	/** @var array */
12
	private $properties = [];
13
14
	/** @var string */
15
	protected $dateFormat;
16
17
18
	public function __construct(string $dateFormat)
19
	{
20
		$this->dateFormat = $dateFormat;
21
	}
22
23
24
	/**
25
	 * @return mixed
26
	 */
27
	public function __get(string $name)
28
	{
29
		if (array_key_exists($name, $this->properties)) {
30
			return $this->properties[$name];
31
		}
32
		throw new Exceptions\Runtime('Property does not exists. ' . $name);
33
	}
34
35
36
	public function clearTemporaryValues(): void
37
	{
38
		$this->dateFormat = '';
39
	}
40
41
42
	public function bindProperty(string $name, string $type, $value): void
43
	{
44
		$method = 'set' . ucfirst($name);
45
		if (method_exists($this, $method)) {
46
			$value = $this->{$method}($value);
47
		} elseif ($value !== null) {
48
			$value = $this->checkValue($value, $type);
49
		}
50
		$this->properties[$name] = $value;
51
	}
52
53
54
	public function current()
55
	{
56
		return current($this->properties);
57
	}
58
59
60
	public function key()
61
	{
62
		return key($this->properties);
63
	}
64
65
66
	public function next()
67
	{
68
		next($this->properties);
69
	}
70
71
72
	public function rewind()
73
	{
74
		reset($this->properties);
75
	}
76
77
78
	public function valid()
79
	{
80
		return array_key_exists($this->key(), $this->properties);
81
	}
82
83
84
	public function getProperties(): array
85
	{
86
		return $this->properties;
87
	}
88
89
90
	/**
91
	 * @return mixed
92
	 */
93
	protected function checkValue($value, string $type)
94
	{
95
		switch ($type) {
96
			case 'datetime':
97
				return Utils\Strings::createFromFormat($value, $this->dateFormat);
98
			case 'float':
99
				return floatval($value);
100
			case 'string':
101
				return trim($value);
102
			case 'int':
103
				return intval($value);
104
			case 'string|null':
105
				return trim($value) ?: null;
106
		}
107
		return $value;
108
	}
109
110
}
111