9
9
use MichalSpacekCz \Http \Client \HttpClientRequest ;
10
10
use MichalSpacekCz \Http \Exceptions \HttpClientRequestException ;
11
11
use Nette \Http \IResponse ;
12
+ use Nette \Schema \Elements \Structure ;
13
+ use Nette \Schema \Expect ;
14
+ use Nette \Schema \Processor ;
15
+ use Nette \Schema \ValidationException ;
12
16
use Nette \Utils \Json ;
13
17
use Nette \Utils \JsonException ;
14
18
use Override ;
28
32
29
33
30
34
public function __construct (
35
+ private Processor $ schemaProcessor ,
31
36
private HttpClient $ httpClient ,
32
37
) {
33
38
}
@@ -54,25 +59,54 @@ public function getDetails(string $companyId): CompanyInfoDetails
54
59
if (empty ($ units ->id )) {
55
60
throw new CompanyNotFoundException ();
56
61
}
57
- $ unit = $ this ->call ('uctovna-jednotka ' , ['id ' => reset ($ units ->id )]);
58
-
62
+ try {
63
+ /** @var Structure $expectArray */
64
+ $ expectArray = $ this ->schemaProcessor ->process (
65
+ Expect::type (Structure::class),
66
+ Expect::array ([
67
+ Expect::int ()->required (),
68
+ ]),
69
+ );
70
+ $ schema = Expect::structure ([
71
+ 'id ' => $ expectArray ->otherItems ()->required (),
72
+ ])->otherItems ();
73
+ /** @var object{id:array{0:int}} $data */
74
+ $ data = $ this ->schemaProcessor ->process ($ schema , $ units );
75
+ } catch (ValidationException $ e ) {
76
+ throw new CompanyInfoException ($ e ->getMessage (), previous: $ e );
77
+ }
78
+ $ unit = $ this ->call ('uctovna-jednotka ' , ['id ' => $ data ->id [0 ]]);
79
+ try {
80
+ $ schema = Expect::structure ([
81
+ 'ico ' => Expect::string ()->required (),
82
+ 'dic ' => Expect::string (),
83
+ 'nazovUJ ' => Expect::string ()->required (),
84
+ 'ulica ' => Expect::string ()->required (),
85
+ 'mesto ' => Expect::string ()->required (),
86
+ 'psc ' => Expect::string ()->required (),
87
+ ])->otherItems ();
88
+ /** @var object{ico:string, dic?:string, nazovUJ:string, ulica:string, mesto:string, psc:string} $data */
89
+ $ data = $ this ->schemaProcessor ->process ($ schema , $ unit );
90
+ } catch (ValidationException $ e ) {
91
+ throw new CompanyInfoException ($ e ->getMessage (), previous: $ e );
92
+ }
59
93
return new CompanyInfoDetails (
60
94
IResponse::S200_OK ,
61
95
'OK ' ,
62
- $ unit ->ico ,
63
- isset ($ unit ->dic ) && is_string ( $ unit -> dic ) ? strtoupper (self ::COUNTRY_CODE ) . $ unit ->dic : '' ,
64
- $ unit ->nazovUJ ,
65
- $ unit ->ulica ,
66
- $ unit ->mesto ,
67
- $ unit ->psc ,
96
+ $ data ->ico ,
97
+ isset ($ data ->dic ) ? strtoupper (self ::COUNTRY_CODE ) . $ data ->dic : '' ,
98
+ $ data ->nazovUJ ,
99
+ $ data ->ulica ,
100
+ $ data ->mesto ,
101
+ $ data ->psc ,
68
102
self ::COUNTRY_CODE ,
69
103
);
70
104
}
71
105
72
106
73
107
/**
74
108
* @param string $method
75
- * @param array<string, string> $parameters
109
+ * @param array<string, string|int > $parameters
76
110
* @return stdClass JSON object
77
111
* @throws CompanyInfoException
78
112
*/
0 commit comments