99
1010class JsonToDataTransformer
1111{
12+ private const RegisterPriority = ['rzp ' , 'res ' , 'vr ' ];
13+
1214
1315 public function transform (stdClass $ json ): Data
1416 {
@@ -24,22 +26,7 @@ public function transform(stdClass $json): Data
2426 $ data ->vat_payer = $ data ->sources [Sources::SER_NO_DPH ] === true ;
2527 $ data ->company = Strings::trimNull ($ json ->obchodniJmeno ?? null );
2628
27- $ addressExists = isset ($ json ->sidlo ) && self ::updateAddress ($ data , $ json ->sidlo );
28-
29- if ($ addressExists === false && isset ($ json ->dalsiUdaje [0 ]->sidlo [0 ]->sidlo )) {
30- $ addressExists = self ::updateAddress ($ data , $ json ->dalsiUdaje [0 ]->sidlo [0 ]->sidlo );
31- }
32-
33- if ($ addressExists === false && isset ($ json ->sidlo ->textovaAdresa )) {
34- [
35- 'zip ' => $ data ->zip ,
36- 'street ' => $ data ->street ,
37- 'house_number ' => $ data ->house_number ,
38- 'city ' => $ data ->city ,
39- 'country ' => $ country ,
40- ] = Helper::parseAddress ($ json ->sidlo ->textovaAdresa );
41- $ data ->country ??= $ country ;
42- }
29+ self ::resolveAddress ($ data , $ json );
4330
4431 $ data ->nace = (array ) ($ json ->czNace ?? []);
4532 $ data ->legal_form_code = (int ) $ json ->pravniForma ;
@@ -69,10 +56,45 @@ private static function updateAddress(Data $data, stdClass $sidlo): bool
6956 }
7057
7158
59+ private static function resolveAddress (Data $ data , stdClass $ json ): void
60+ {
61+ $ addressExists = isset ($ json ->sidlo ) && self ::updateAddress ($ data , $ json ->sidlo );
62+
63+ if ($ addressExists === false ) {
64+ $ additionalData = isset ($ json ->dalsiUdaje ) ? self ::prepareForAddress ($ json ->dalsiUdaje ) : [];
65+ if ($ additionalData !== []) {
66+ foreach (self ::RegisterPriority as $ register ) {
67+ $ key = self ::keyForAddress ($ register , $ json ->pravniForma );
68+ if (isset ($ additionalData [$ key ])) {
69+ $ addressExists = self ::updateAddress ($ data , $ additionalData [$ key ]);
70+ if ($ addressExists === true ) {
71+ break ;
72+ }
73+ }
74+ }
75+ }
76+ }
77+
78+ if ($ addressExists === false && isset ($ json ->sidlo ->textovaAdresa )) {
79+ [
80+ 'zip ' => $ data ->zip ,
81+ 'street ' => $ data ->street ,
82+ 'house_number ' => $ data ->house_number ,
83+ 'city ' => $ data ->city ,
84+ 'country ' => $ country ,
85+ ] = Helper::parseAddress ($ json ->sidlo ->textovaAdresa );
86+ $ data ->country ??= $ country ;
87+ }
88+ }
89+
90+
7291 private static function isAddressFilled (Data $ data ): bool
7392 {
74- return $ data ->zip !== null
75- || $ data ->street !== null
93+ if ($ data ->zip === null ) {
94+ return false ;
95+ }
96+
97+ return $ data ->street !== null
7698 || $ data ->country !== null
7799 || $ data ->country_code !== null
78100 || $ data ->city !== null
@@ -82,4 +104,31 @@ private static function isAddressFilled(Data $data): bool
82104 || $ data ->house_number !== null ;
83105 }
84106
107+
108+ /**
109+ * @param array<stdClass> $dalsiUdaje
110+ * @return array<stdClass>
111+ */
112+ private static function prepareForAddress (array $ dalsiUdaje ): array
113+ {
114+ $ out = [];
115+ foreach ($ dalsiUdaje as $ record ) {
116+ $ x = self ::keyForAddress ($ record ->datovyZdroj , $ record ->pravniForma );
117+ foreach ($ record ->sidlo ?? [] as $ sidlo ) {
118+ if ($ sidlo ?->primarniZaznam === true && isset ($ sidlo ->sidlo )) {
119+ $ out [$ x ] = $ sidlo ->sidlo ;
120+ break ;
121+ }
122+ }
123+ }
124+
125+ return $ out ;
126+ }
127+
128+
129+ private static function keyForAddress (string $ datovyZdroj , string $ pravniForma ): string
130+ {
131+ return "$ datovyZdroj| $ pravniForma " ;
132+ }
133+
85134}
0 commit comments