Completed
Push — master ( fb9739...3e52b5 )
by Milan
01:47 queued 11s
created

DataProvider::createDateTime()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php declare(strict_types=1);
2
3
namespace h4kuna\Ares;
4
5
use DateTime;
6
use DateTimeZone;
7
8
class DataProvider
9
{
10
11
	/** @var IFactory */
12
	private $factory;
13
14
	/** @var array */
15
	private $data;
16
17
	/** @var Data */
18
	private $dataMessenger;
19
20
21
	public function __construct(IFactory $dataFactory)
22
	{
23
		$this->factory = $dataFactory;
24
	}
25
26
27
	public function getData(): Data
28
	{
29
		if (is_array($this->data)) {
30
			$this->setFileNumberAndCourt();
31
			$this->dataMessenger = $this->factory->createData($this->data);
32
		}
33
		return $this->dataMessenger;
34
	}
35
36
37
	public function prepareData()
38
	{
39
		$this->data = [];
40
		return $this;
41
	}
42
43
44
	public function setActive(bool $active)
45
	{
46
		$this->data['active'] = $active;
47
		return $this;
48
	}
49
50
51
	public function setCity(string $city)
52
	{
53
		$this->data['city'] = self::toNull($city);
54
		return $this;
55
	}
56
57
58
	public function setCompany(string $company)
59
	{
60
		$this->data['company'] = self::toNull($company);
61
		return $this;
62
	}
63
64
65
	public function setCourt(string $court)
66
	{
67
		$this->data['court'] = self::toNull($court);
68
		return $this;
69
	}
70
71
72
	public function setCreated(string $date)
73
	{
74
		$this->data['created'] = self::createDateTime($date);
75
		return $this;
76
	}
77
78
79
	public function setDissolved(?string $date)
80
	{
81
		if ($date === null) {
82
			$this->setActive(true);
83
			$this->data['dissolved'] = null;
84
		} else {
85
			$this->data['dissolved'] = self::createDateTime($date);
86
			$this->setActive(false);
87
		}
88
89
		return $this;
90
	}
91
92
93
	public function setFileNumber(string $fileNumber)
94
	{
95
		$this->data['file_number'] = self::toNull($fileNumber);
96
		return $this;
97
	}
98
99
100
	public function setIN(string $in)
101
	{
102
		$this->data['in'] = self::toNull($in);
103
		return $this;
104
	}
105
106
107
	public function setIsPerson(string $s)
108
	{
109
		$this->data['is_person'] = $s <= '108' || $s === '424' || $s === '425';
110
		$this->data['legal_form_code'] = (int) $s;
111
		return $this;
112
	}
113
114
115
	private function setFileNumberAndCourt(): void
116
	{
117
		$this->data['court_all'] = null;
118
		if ($this->data['file_number'] && $this->data['court']) {
119
			$this->data['court_all'] = $this->data['file_number'] . ', ' . $this->data['court'];
120
		}
121
	}
122
123
124
	public function setCityDistrict(string $district)
125
	{
126
		$this->data['city_district'] = self::toNull($district);
127
		return $this;
128
	}
129
130
131
	public function setCityPost(string $district)
132
	{
133
		$this->data['city_post'] = self::toNull($district);
134
		return $this;
135
	}
136
137
138
	public function setStreet(string $street)
139
	{
140
		$this->data['street'] = self::toNull($street);
141
		return $this;
142
	}
143
144
145
	public function setHouseNumber(string $cd, string $co)
146
	{
147
		$this->data['house_number'] = self::toNull(trim($cd . '/' . $co, '/'));
148
		return $this;
149
	}
150
151
152
	public function setTIN(string $s)
153
	{
154
		$this->data['tin'] = self::toNull($s);
155
		$this->data['vat_payer'] = (bool) $s;
156
		return $this;
157
	}
158
159
160
	public function setZip(string $zip)
161
	{
162
		$this->data['zip'] = self::toNull($zip);
163
		return $this;
164
	}
165
166
167
	public function setNace(array $nace)
168
	{
169
		$this->data['nace'] = $nace;
170
		return $this;
171
	}
172
173
174
	private static function toNull(string $v): ?string
175
	{
176
		$string = trim($v);
177
		if ($string === '') {
178
			return null;
179
		}
180
		return $string;
181
	}
182
183
184
	private static function createDateTime(string $date): DateTime
185
	{
186
		return new DateTime($date, new DateTimeZone('Europe/Prague'));
187
	}
188
189
}
190