@@ -120,32 +120,31 @@ function ($reason) {
120120 private function validateResponse (array $ response ): void
121121 {
122122 try {
123- $ data = $ response ['entity ' ];
124-
125-
126- // Ensure $data is an array
127- if (! is_array ($ data )) {
128- throw new ResponseException ('Response data is not an array ' );
123+ // Check if entity exists in the response
124+ if (! isset ($ response ['entity ' ]) || ! is_array ($ response ['entity ' ])) {
125+ throw new ResponseException ('Entity data not found in response ' );
129126 }
130127
128+ $ entity = $ response ['entity ' ];
129+
131130 // Check if entity.page exists in the response
132- if (! isset ($ data ['page ' ])) {
131+ if (! isset ($ entity ['page ' ])) {
133132 throw new ResponseException ('Page data not found in response: entity.page is missing ' );
134133 }
135134
136- // Check if layout exists in the response
137- if (! isset ($ data ['layout ' ]) || empty ($ data ['layout ' ])) {
135+ // Check if entity. layout exists in the response
136+ if (! isset ($ entity ['layout ' ]) || empty ($ entity ['layout ' ])) {
138137 throw new ResponseException ("This page don't have a layout, maybe because you're using an advanced template " );
139138 }
140139
141- // Check if template exists in the response
142- if (! isset ($ data ['template ' ]) || empty ($ data ['template ' ])) {
143- throw new ResponseException ('Template data not found in response: template is missing ' );
140+ // Check if entity. template exists in the response
141+ if (! isset ($ entity ['template ' ]) || empty ($ entity ['template ' ])) {
142+ throw new ResponseException ('Template data not found in response: entity. template is missing ' );
144143 }
145144
146- // Check if site exists in the response
147- if (! isset ($ data ['site ' ]) || empty ($ data ['site ' ])) {
148- throw new ResponseException ('Site data not found in response: site is missing ' );
145+ // Check if entity. site exists in the response
146+ if (! isset ($ entity ['site ' ]) || empty ($ entity ['site ' ])) {
147+ throw new ResponseException ('Site data not found in response: entity. site is missing ' );
149148 }
150149 } catch (ResponseException $ e ) {
151150 throw $ e ;
@@ -168,14 +167,12 @@ private function validateResponse(array $response): void
168167 private function mapResponseToPage (array $ response ): Page
169168 {
170169 try {
171- $ data = $ response ['entity ' ];
172-
173- // Ensure $data is an array and entity is an array
174- if (! is_array ($ data )) {
175- throw new ResponseException ('Can \'t map response to Page: Response data is not an array ' );
170+ // Ensure entity is an array
171+ if (! isset ($ response ['entity ' ]) || ! is_array ($ response ['entity ' ])) {
172+ throw new ResponseException ('Can \'t map response to Page: Response entity is not an array ' );
176173 }
177174
178- $ pageData = $ data ['page ' ] ?? [];
175+ $ pageData = $ response [ ' entity ' ] ['page ' ] ?? [];
179176
180177 if (! is_array ($ pageData )) {
181178 $ pageData = [];
@@ -214,26 +211,26 @@ private function mapResponseToPage(array $response): Page
214211 private function mapResponseToPageAsset (array $ response ): PageAsset
215212 {
216213 try {
217- $ data = $ response ['entity ' ] ?? [];
218-
219- // Ensure $data is an array
220- if (! is_array ($ data )) {
221- $ data = [];
214+ // Ensure entity exists and is an array
215+ if (! isset ($ response ['entity ' ]) || ! is_array ($ response ['entity ' ])) {
216+ throw new ResponseException ('Entity data not found in response ' );
222217 }
223218
219+ $ entity = $ response ['entity ' ];
220+
224221 // Extract page data
225222 $ page = $ this ->mapResponseToPage ($ response );
226223
227224 // Extract layout data
228- $ layoutData = isset ($ data ['layout ' ]) && is_array ($ data ['layout ' ]) ? $ data ['layout ' ] : [];
225+ $ layoutData = isset ($ entity ['layout ' ]) && is_array ($ entity ['layout ' ]) ? $ entity ['layout ' ] : [];
229226
230227 $ layout = new Layout (
231228 width: $ layoutData ['width ' ] ?? null ,
232229 title: $ layoutData ['title ' ] ?? '' ,
233- header: $ layoutData ['header ' ] ?? true ,
234- footer: $ layoutData ['footer ' ] ?? true ,
235- body: is_array ( $ layoutData ['body ' ] ?? []) ? $ layoutData [ ' body ' ] : ['rows ' => []],
236- sidebar: is_array ( $ layoutData ['sidebar ' ] ?? []) ? $ layoutData [ ' sidebar ' ] : [
230+ header: $ layoutData ['header ' ] ?? false ,
231+ footer: $ layoutData ['footer ' ] ?? false ,
232+ body: $ layoutData ['body ' ] ?? ['rows ' => []],
233+ sidebar: $ layoutData ['sidebar ' ] ?? [
237234 'containers ' => [],
238235 'location ' => '' ,
239236 'width ' => 'small ' ,
@@ -244,7 +241,7 @@ private function mapResponseToPageAsset(array $response): PageAsset
244241 );
245242
246243 // Extract template data
247- $ templateData = isset ($ data ['template ' ]) && is_array ($ data ['template ' ]) ? $ data ['template ' ] : [];
244+ $ templateData = isset ($ entity ['template ' ]) && is_array ($ entity ['template ' ]) ? $ entity ['template ' ] : [];
248245
249246 $ template = new Template (
250247 identifier: $ templateData ['identifier ' ] ?? '' ,
@@ -262,7 +259,7 @@ private function mapResponseToPageAsset(array $response): PageAsset
262259 );
263260
264261 // Extract site data
265- $ siteData = isset ($ data ['site ' ]) && is_array ($ data ['site ' ]) ? $ data ['site ' ] : [];
262+ $ siteData = isset ($ entity ['site ' ]) && is_array ($ entity ['site ' ]) ? $ entity ['site ' ] : [];
266263
267264 $ site = new Site (
268265 identifier: $ siteData ['identifier ' ] ?? '' ,
@@ -279,11 +276,11 @@ private function mapResponseToPageAsset(array $response): PageAsset
279276 );
280277
281278 // Extract containers
282- $ containers = isset ($ data ['containers ' ]) && is_array ($ data ['containers ' ]) ? $ data ['containers ' ] : [];
279+ $ containers = isset ($ entity ['containers ' ]) && is_array ($ entity ['containers ' ]) ? $ entity ['containers ' ] : [];
283280
284281 // Extract urlContentMap if available
285282 $ urlContentMap = null ;
286- $ urlContentMapData = $ data ['urlContentMap ' ] ?? null ;
283+ $ urlContentMapData = $ entity ['urlContentMap ' ] ?? null ;
287284 if (is_array ($ urlContentMapData )) {
288285 $ urlContentMap = new Contentlet (
289286 identifier: $ urlContentMapData ['identifier ' ] ?? '' ,
@@ -297,18 +294,14 @@ private function mapResponseToPageAsset(array $response): PageAsset
297294 }
298295
299296 // Extract viewAs data
300- $ viewAsData = isset ($ data ['viewAs ' ]) && is_array ($ data ['viewAs ' ]) ? $ data ['viewAs ' ] : [];
297+ $ viewAsData = isset ($ entity ['viewAs ' ]) && is_array ($ entity ['viewAs ' ]) ? $ entity ['viewAs ' ] : [];
301298
302- $ visitorData = $ viewAsData ['visitor ' ] ?? [];
303- if (! is_array ($ visitorData )) {
304- $ visitorData = [];
305- }
299+ $ visitorData = isset ($ viewAsData ['visitor ' ]) && is_array ($ viewAsData ['visitor ' ]) ? $ viewAsData ['visitor ' ] : [];
306300
307301 // Create UserAgent
308- $ userAgentData = $ visitorData ['userAgent ' ] ?? [];
309- if (! is_array ($ userAgentData )) {
310- $ userAgentData = [];
311- }
302+ $ userAgentData = isset ($ visitorData ['userAgent ' ]) && is_array ($ visitorData ['userAgent ' ])
303+ ? $ visitorData ['userAgent ' ]
304+ : [];
312305
313306 $ userAgent = new UserAgent (
314307 browser: $ userAgentData ['browser ' ] ?? '' ,
@@ -318,36 +311,33 @@ private function mapResponseToPageAsset(array $response): PageAsset
318311 );
319312
320313 // Create GeoLocation
321- $ geoData = $ visitorData ['geo ' ] ?? [];
322- if (! is_array ($ geoData )) {
323- $ geoData = [];
324- }
314+ $ geoData = isset ($ visitorData ['geo ' ]) && is_array ($ visitorData ['geo ' ]) ? $ visitorData ['geo ' ] : [];
325315
326316 $ geoLocation = new GeoLocation (
327317 city: $ geoData ['city ' ] ?? '' ,
328318 country: $ geoData ['country ' ] ?? '' ,
329319 countryCode: $ geoData ['countryCode ' ] ?? '' ,
330- latitude: $ geoData ['latitude ' ] ?? '' ,
331- longitude: $ geoData ['longitude ' ] ?? '' ,
320+ latitude: ( float )( $ geoData ['latitude ' ] ?? 0 ) ,
321+ longitude: ( float )( $ geoData ['longitude ' ] ?? 0 ) ,
332322 region: $ geoData ['region ' ] ?? ''
333323 );
334324
335325 // Create Visitor
336326 $ visitor = new Visitor (
337- tags: is_array ( $ visitorData [ ' tags ' ] ?? []) ? $ visitorData [ ' tags ' ] : [],
327+ tags: [],
338328 device: $ visitorData ['device ' ] ?? '' ,
339329 isNew: $ visitorData ['isNew ' ] ?? false ,
340330 userAgent: $ userAgent ,
341331 referer: $ visitorData ['referer ' ] ?? '' ,
342332 dmid: $ visitorData ['dmid ' ] ?? '' ,
343333 geo: $ geoLocation ,
344- personas: is_array ( $ visitorData [ ' personas ' ] ?? []) ? $ visitorData [ ' personas ' ] : []
334+ personas: []
345335 );
346336
347337 // Create ViewAs
348338 $ viewAs = new ViewAs (
349339 visitor: $ visitor ,
350- language: is_array ( $ viewAsData [ ' language ' ] ?? []) ? $ viewAsData [ ' language ' ] : [],
340+ language: [],
351341 mode: $ viewAsData ['mode ' ] ?? 'LIVE '
352342 );
353343
0 commit comments