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

Data::formatDate()   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 h4kuna\DataType\Immutable;
6
7
/**
8
 * @property-read bool $active
9
 * @property-read string $city
10
 * @property-read string $city_district
11
 * @property-read string $city_post
12
 * @property-read string $company
13
 * @property-read string $court
14
 * @property-read string $court_all
15
 * @property-read \DateTime $created
16
 * @property-read \DateTime $dissolved
17
 * @property-read string $file_number
18
 * @property-read string $in
19
 * @property-read bool $is_person
20
 * @property-read int $legal_form_code
21
 * @property-read string $house_number
22
 * @property-read string $street
23
 * @property-read string $tin
24
 * @property-read string $vat_payer
25
 * @property-read string $zip
26
 * @property-read array $nace
27
 */
28
class Data extends Immutable\Messenger
29
{
30
31
	public function toArray(array $map = []): array
32
	{
33
		if ($map === []) {
34
			return $this->getData();
35
		}
36
		$out = [];
37
		foreach ($map as $k => $v) {
38
			if ($this->offsetExists($k)) {
39
				if (!$v) {
40
					$v = $k;
41
				}
42
				$out[$v] = $this[$k];
43
			}
44
		}
45
		return $out;
46
	}
47
48
49
	public function jsonSerialize()
50
	{
51
		$data = $this->getData();
52
		if ($this->created instanceof \DateTimeInterface) {
53
			$data['created'] = self::formatDate($this->created);
54
		}
55
56
		if ($this->dissolved instanceof \DateTimeInterface) {
57
		    $data['dissolved'] = self::formatDate($this->dissolved);
58
		}
59
		return $data;
60
	}
61
62
63
	public function __toString()
64
	{
65
		return (string) json_encode($this->jsonSerialize());
66
	}
67
68
69
	private static function formatDate(\DateTimeInterface $date)
70
	{
71
		return $date->format(\DateTime::ISO8601);
72
	}
73
74
}
75