• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

h4kuna / ares / 6585053264

20 Oct 2023 08:04AM UTC coverage: 91.691%. Remained the same
6585053264

push

github

h4kuna
Add schedule tests every day

309 of 337 relevant lines covered (91.69%)

0.92 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

97.73
/src/Ares/Helper.php
1
<?php declare(strict_types=1);
2

3
namespace h4kuna\Ares\Ares;
4

5
use h4kuna\Ares\Ares\Core\SubjectType;
6
use h4kuna\Ares\Exceptions\InvalidStateException;
7
use h4kuna\Ares\Tools\Strings;
8
use Nette\Utils\Strings as NetteStrings;
9

10
final class Helper
11
{
12
        public static string $baseUrl = 'https://ares.gov.cz/ekonomicke-subjekty-v-be/rest';
13

14
        /**
15
         * @var array<string, string>
16
         */
17
        public static $endpoints = [
18
                Sources::SERVICE_VR => '/ekonomicke-subjekty-vr/{ico}',
19
                Sources::SERVICE_RES => '/ekonomicke-subjekty-res/{ico}',
20
                Sources::SERVICE_RZP => '/ekonomicke-subjekty-rzp/{ico}',
21
                Sources::SERVICE_NRPZS => '/ekonomicke-subjekty-nrpzs/{ico}',
22
                Sources::SERVICE_RCNS => '/ekonomicke-subjekty-rcns/{ico}',
23
                Sources::SERVICE_RPSH => '/ekonomicke-subjekty-rpsh/{ico}',
24
                Sources::SERVICE_RS => '/ekonomicke-subjekty-rs/{ico}',
25
                Sources::SERVICE_SZR => '/ekonomicke-subjekty-szr/{ico}',
26
                Sources::SERVICE_CEU => '/ekonomicke-subjekty-ceu/{ico}',
27
                Sources::CORE => '/ekonomicke-subjekty/{ico}',
28
                Sources::DIAL => '/ciselniky-nazevniky/{ico}',
29
        ];
30

31
        private const SERVICES = [
32
                Sources::SERVICE_VR => 'NEEXISTUJICI',
33
                Sources::SERVICE_RES => 'NEEXISTUJICI',
34
                Sources::SERVICE_RZP => 'NEEXISTUJICI',
35
                Sources::SERVICE_NRPZS => 'NEEXISTUJICI',
36
                Sources::SERVICE_RCNS => 'NEEXISTUJICI',
37
                Sources::SERVICE_RPSH => 'NEEXISTUJICI',
38
                Sources::SERVICE_RS => 'NEEXISTUJICI',
39
                Sources::SERVICE_SZR => 'NEEXISTUJICI',
40
                Sources::SERVICE_CEU => 'NEEXISTUJICI',
41
                Sources::SER_NO_DPH => 'NEEXISTUJICI',
42
                Sources::SER_NO_IR => 'NEEXISTUJICI',
43
                Sources::SER_NO_RED => 'NEEXISTUJICI',
44
                Sources::SER_NO_SD => 'NEEXISTUJICI',
45
        ];
46

47

48
        public static function endpointExists(string $source): bool
1✔
49
        {
50
                return isset(self::$endpoints[$source]);
1✔
51
        }
52

53

54
        /**
55
         * @param Sources::SERVICE_*|Sources::DIAL|Sources::CORE $source
56
         */
57
        public static function prepareUrlSearch(string $source): string
1✔
58
        {
59
                return self::prepareUrl($source, 'vyhledat');
1✔
60
        }
61

62

63
        /**
64
         * @param Sources::SERVICE_*|Sources::DIAL|Sources::CORE $source
65
         */
66
        public static function prepareUrl(string $source, string $in): string
1✔
67
        {
68
                if (self::endpointExists($source) === false) {
1✔
69
                        throw new InvalidStateException(sprintf('Endpoint %s does not exists.', $source));
×
70
                }
71

72
                return str_replace(
1✔
73
                        '{ico}',
1✔
74
                        self::normalizeIN($in),
1✔
75
                        self::$baseUrl . self::$endpoints[$source],
1✔
76
                );
77
        }
78

79

80
        /**
81
         * @param array<Sources::SER*, string> $registrations
82
         * @return array<Sources::SER*, true|string>
83
         */
84
        public static function services(array $registrations): array
1✔
85
        {
86
                $map = [];
1✔
87
                foreach ($registrations as $k => $v) {
1✔
88
                        $map[$k] = $v === 'AKTIVNI' ? true : $v;
1✔
89
                }
90

91
                return $map + self::SERVICES;
1✔
92
        }
93

94

95
        public static function houseNumber(
1✔
96
                string $cisloDomovni,
97
                string $cisloOrientacni,
98
                string $cisloOrientacniPismeno
99
        ): ?string
100
        {
101
                $houseNumber = Strings::trimNull(trim($cisloDomovni . '/' . $cisloOrientacni, '/'));
1✔
102
                $houseNumber = $houseNumber === '0' ? null : $houseNumber;
1✔
103

104
                $cisloOrientacniPismeno = Strings::trimNull($cisloOrientacniPismeno);
1✔
105
                if ($cisloOrientacniPismeno !== null) {
1✔
106
                        $houseNumber .= $cisloOrientacniPismeno;
1✔
107
                }
108

109
                return $houseNumber;
1✔
110
        }
111

112

113
        public static function isPerson(int $legalForm): bool
1✔
114
        {
115
                return match ($legalForm) {
116
                        SubjectType::OSVC, 105, 107, 424, 425 => true,
1✔
117
                        default => false,
1✔
118
                };
119
        }
120

121

122
        public static function normalizeIN(string $in): string
1✔
123
        {
124
                return str_pad($in, 8, '0', STR_PAD_LEFT);
1✔
125
        }
126

127

128
        /**
129
         * @return array{street: ?string, zip: ?string, city: ?string, house_number: ?string}
130
         */
131
        public static function parseAddress(string $address): array
1✔
132
        {
133
                $results = NetteStrings::match($address, '~^(?<street>.+) (?<house_number>\d+(?:/\d+)?(?:\w)?)(?:, (?<district>.+?))?, (?<zip>\d{5}) (?<city>.+)$~');
1✔
134

135
                if ($results !== null) {
1✔
136
                        return self::prepareAddressData($results);
1✔
137
                }
138

139
                $results = NetteStrings::match($address, '~^(?<city>.+), (?<zip>\d{5})(?:, (?<district>.+?))?, (?<street>.+), (?<house_number>\d+(?:/\d+)?(?:\w)?)$~');
1✔
140

141
                return self::prepareAddressData($results ?? []);
1✔
142
        }
143

144

145
        /**
146
         * @param array{street?: string, zip?: string, city?: string, house_number?: string} $results
147
         * @return array{street: ?string, zip: ?string, city: ?string, house_number: ?string}
148
         */
149
        private static function prepareAddressData(array $results): array
1✔
150
        {
151
                return [
152
                        'zip' => $results['zip'] ?? null,
1✔
153
                        'street' => $results['street'] ?? null,
1✔
154
                        'house_number' => $results['house_number'] ?? null,
1✔
155
                        'city' => $results['city'] ?? null,
1✔
156
                ];
157
        }
158

159

160
        public static function normalizeTIN(string $tin): string
1✔
161
        {
162
                $upper = strtoupper($tin);
1✔
163
                if (str_starts_with($upper, 'CZ') === false && is_numeric($upper)) {
1✔
164
                        return "CZ$upper";
1✔
165
                }
166

167
                return $upper;
1✔
168
        }
169
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc