Jake Vanderwerf
2026-02-10 c348d35c7ecb6c74f71cf90b982412f267c5d807
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
<?php
namespace JVBase\managers\SEO;
 
use JVBase\meta\Meta;
 
if (!defined('ABSPATH')) {
    exit;
}
 
/**
 * Helper methods for auto-building complex schema fields
 *
 * SINGLE SOURCE OF TRUTH for field enhancement.
 * All pattern resolution and value enhancement flows through here.
 */
class SchemaFieldHelpers
{
    /**
     * Auto-resolve and enhance field values
     * Main entry point for all field enhancement logic
     *
     * @param string $fieldName Field name
     * @param mixed $value Raw value
     * @param Meta|null $meta Optional Meta for accessing related fields
     * @return mixed Enhanced value
     */
    public static function autoResolve(string $fieldName, mixed $value, ?Meta $meta = null): mixed
    {
        // Skip empty values
        if ($value === null || $value === '') {
            return $value;
        }
 
        // Skip if already enhanced (has @type)
        if (is_array($value) && isset($value['@type'])) {
            return $value;
        }
 
        // Auto-enhance based on field name
        return match($fieldName) {
            // Location data -> PostalAddress + GeoCoordinates
            'location', 'address' => is_array($value) ? self::buildLocation($value) : $value,
 
            // Image fields -> ImageObject
            'image', 'logo', 'photo','image_portrait', 'image_landscape', 'featured_image'
            => is_numeric($value) ? self::buildImage($value) : self::wrapImageUrl($value),
 
            // Hours -> openingHours array
            'hours', 'opening_hours', 'openingHoursSpecification'
            => is_array($value) ? self::buildOpeningHours($value)['openingHours'] ?? $value : $value,
 
            // Links -> sameAs array
            'links', 'sameAs'
            => is_array($value) ? self::buildSameAs($value)['sameAs'] ?? $value : [$value],
// Navigation -> SiteNavigationElement array
 
            'hasPart'
            => is_array($value) ? self::buildSiteNavigation($value)['hasPart'] ?? $value : $value,
            'hasOfferCatalog'
            => is_array($value) ? self::offer_catalog_array($value) : $value,
            // Services -> OfferCatalog
            'services'
            => is_array($value) ? self::buildServiceCatalog($value) : $value,
 
            // Amenities -> amenityFeature
            'amenities'
            => self::buildAmenityFeatures($value)['amenityFeature'] ?? $value,
 
            // Languages -> availableLanguage
            'languages'
            => is_array($value) ? self::buildAvailableLanguages($value)['availableLanguage'] ?? $value : $value,
 
            // Rating -> AggregateRating (needs rating_count from meta)
            'rating'
            => $meta ? self::buildAggregateRating($value, $meta->get('rating_count')) : $value,
 
            // Geo coordinates
            'geo'
            => is_array($value) ? self::buildGeoCoordinates($value) : $value,
            'image_object' => self::image_object($value),
            'image_url' => self::image_url($value),
            'associatedMedia', 'image_object_array' => self::image_object_array($value),
            // Add to the match statement:
            'brand' => is_array($value) ? self::buildBrandObject($value) : $value,
            'offers' => is_array($value) ? self::buildOfferObject($value) : $value,
            'review' => is_array($value) ? self::buildReviewArray($value) : $value,
            'parentOrganization', 'subOrganization'
            => is_array($value) ? self::buildOrganizationReference($value) : $value,
            'employee' => is_array($value) ? self::buildPersonReferenceArray($value) : $value,
            'starRating' => is_array($value) ? self::buildRatingObject($value) : $value,
            // Default: return as-is
            default => $value
        };
    }
 
    /**
     * Check if a value is a pattern (contains {{...}})
     */
    public static function isPattern(mixed $value): bool
    {
        return is_string($value) && str_contains($value, '{{') && str_contains($value, '}}');
    }
 
    /**
     * Get Jake Van creator attribution (ONLY for Website schema)
     */
    public static function getCreator(): array
    {
        return [
            '@type'         => 'Person',
            '@id'           => 'https://jakevan.ca/#person',
            'name'          => 'Jake Vanderwerf',
            'alternateName' => 'JakeVan',
            'url'           => 'https://jakevan.ca',
            'jobTitle'      => ['Graphic Designer', 'Website Designer', 'Website Developer'],
            'sameAs'        => [
                'https://github.com/jakevanderwerf',
                'https://www.linkedin.com/in/jakevanderwerf'
            ]
        ];
    }
 
    /**
     * Create proper ImageObject from WordPress attachment ID or URL
     *
     * @param int|string $image Image ID or URL
     * @param string $size Image size (default: 'full')
     * @return array|string ImageObject schema or URL
     */
    public static function buildImage(int|string $image, string $size = 'full'): array|string
    {
        // If it's empty, return empty string
        if (empty($image)) {
            return '';
        }
 
        // If it's already a URL, wrap it
        if (is_string($image) && (str_starts_with($image, 'http://') || str_starts_with($image, 'https://'))) {
            return self::wrapImageUrl($image);
        }
 
        // Treat as attachment ID
        $image_id = (int)$image;
        $image_url = wp_get_attachment_image_url($image_id, $size);
 
        if (!$image_url) {
            return '';
        }
 
        $image_meta = wp_get_attachment_metadata($image_id);
        $image_post = get_post($image_id);
 
        $imageObject = [
            '@type'      => 'ImageObject',
            'url'        => $image_url,
            'contentUrl' => $image_url,
        ];
 
        // Add dimensions if available
        if (!empty($image_meta['width']) && !empty($image_meta['height'])) {
            $imageObject['width'] = $image_meta['width'];
            $imageObject['height'] = $image_meta['height'];
        }
 
        // Add caption if available
        if ($image_post && !empty($image_post->post_excerpt)) {
            $imageObject['caption'] = $image_post->post_excerpt;
        }
 
        // Add alt text
        $alt = get_post_meta($image_id, '_wp_attachment_image_alt', true);
        if ($alt) {
            $imageObject['description'] = $alt;
        }
 
        return $imageObject;
    }
 
    /**
     * Wrap a URL string in minimal ImageObject
     */
    private static function wrapImageUrl(mixed $value): array|string
    {
        if (!is_string($value) || !filter_var($value, FILTER_VALIDATE_URL)) {
            return $value;
        }
 
        return [
            '@type'      => 'ImageObject',
            'url'        => $value,
            'contentUrl' => $value,
        ];
    }
 
    /**
     * Build PostalAddress and GeoCoordinates from location data
     *
     * Returns array with 'address' and 'geo' keys
     *
     * @param array $location Location data from Meta
     * @return array Schema with address and geo fields
     */
    public static function buildLocation(array $location): array
    {
        $schema = [];
 
        // Build PostalAddress
        if (!empty($location['address'])) {
            $address = [
                '@type'         => 'PostalAddress',
                'streetAddress' => $location['address']
            ];
 
            if (!empty($location['city'])) {
                $address['addressLocality'] = $location['city'];
            }
 
            if (!empty($location['province'])) {
                $address['addressRegion'] = $location['province'];
            }
 
            if (!empty($location['postal_code'])) {
                $address['postalCode'] = $location['postal_code'];
            }
 
            if (!empty($location['country'])) {
                $address['addressCountry'] = $location['country'];
            }
 
            $schema['address'] = $address;
        }
 
        // Build GeoCoordinates
        if (!empty($location['lat']) && !empty($location['lng'])) {
            $schema['geo'] = self::buildGeoCoordinates([
                'latitude'  => $location['lat'],
                'longitude' => $location['lng']
            ]);
        }
 
        return $schema;
    }
 
    /**
     * Build GeoCoordinates from lat/lng data
     */
    public static function buildGeoCoordinates(array $coords): array
    {
        $lat = $coords['latitude'] ?? $coords['lat'] ?? null;
        $lng = $coords['longitude'] ?? $coords['lng'] ?? null;
 
        if (!$lat || !$lng) {
            return [];
        }
 
        return [
            '@type'     => 'GeoCoordinates',
            'latitude'  => (float)$lat,
            'longitude' => (float)$lng
        ];
    }
 
    /**
     * Build opening hours from repeater field
     *
     * @param array $hours Hours data from Meta
     * @return array Schema with openingHours field
     */
    public static function buildOpeningHours(array $hours): array
    {
        if (empty($hours)) {
            return [];
        }
 
        $formatted = [];
 
        foreach ($hours as $entry) {
            if (empty($entry['day'])) {
                continue;
            }
 
            $day = ucfirst($entry['day']);
            $opens = $entry['time_opens'] ?? '09:00';
            $closes = $entry['time_closes'] ?? '17:00';
 
            // Format: "Mo-Fr 09:00-17:00" or "Mo 09:00-17:00"
            $formatted[] = "{$day} {$opens}-{$closes}";
        }
 
        return !empty($formatted) ? ['openingHours' => $formatted] : [];
    }
 
    /**
     * Build sameAs array from links repeater
     *
     * @param array $links Links data from Meta
     * @return array Schema with sameAs field
     */
    public static function buildSameAs(array $links): array
    {
        if (empty($links)) {
            return [];
        }
 
        $urls = [];
 
        foreach ($links as $link) {
            if (is_array($link) && !empty($link['url'])) {
                $urls[] = $link['url'];
            } elseif (is_string($link)) {
                $urls[] = $link;
            }
        }
 
        return !empty($urls) ? ['sameAs' => $urls] : [];
    }
 
    /**
     * Build service catalog from services array
     * Returns properly formatted OfferCatalog with itemListElement
     *
     * @param array $services Services data
     * @return array OfferCatalog schema
     */
    public static function buildServiceCatalog(array $services): array
    {
        if (empty($services)) {
            return [];
        }
 
        $items = [];
 
        foreach ($services as $service) {
            // Support both 'type' and '@type' in service data
            $serviceType = $service['type'] ?? $service['@type'] ?? 'Service';
 
            $item = [
                '@type' => $serviceType,
                'name'  => $service['name'] ?? $service['title'] ?? ''
            ];
 
            if (!empty($service['description'])) {
                $item['description'] = $service['description'];
            }
 
            // Handle pricing - can be simple text or structured
            if (!empty($service['price'])) {
                // Check if price is already an Offer object
                if (is_array($service['price']) && isset($service['price']['@type'])) {
                    $item['offers'] = $service['price'];
                } else {
                    // Create simple offer with price text
                    $item['offers'] = [
                        '@type'         => 'Offer',
                        'price'         => (string)$service['price'],
                        'priceCurrency' => $service['currency'] ?? $service['priceCurrency'] ?? 'CAD'
                    ];
                }
            }
 
            // Handle priceRange if provided instead of price
            if (!empty($service['priceRange'])) {
                $item['offers'] = [
                    '@type'      => 'Offer',
                    'price'      => $service['priceRange'],
                    'priceCurrency' => $service['currency'] ?? $service['priceCurrency'] ?? 'CAD'
                ];
            }
 
            if (!empty($item['name'])) {
                $items[] = $item;
            }
        }
 
        if (empty($items)) {
            return [];
        }
 
        return [
            '@type'           => 'OfferCatalog',
            'name'            => 'Services',
            'itemListElement' => $items
        ];
    }
 
    /**
     * Build amenity features from amenities array or string
     *
     * @param array|string $amenities Amenities data
     * @return array Schema with amenityFeature field
     */
    public static function buildAmenityFeatures(array|string $amenities): array
    {
        if (empty($amenities)) {
            return [];
        }
 
        // Convert string to array
        if (is_string($amenities)) {
            $amenities = array_map('trim', explode(',', $amenities));
        }
 
        $features = [];
 
        foreach ($amenities as $amenity) {
            if (is_array($amenity) && isset($amenity['name'])) {
                $features[] = [
                    '@type' => 'LocationFeatureSpecification',
                    'name'  => $amenity['name'],
                    'value' => true
                ];
            } elseif (is_string($amenity) && $amenity !== '') {
                $features[] = [
                    '@type' => 'LocationFeatureSpecification',
                    'name'  => $amenity,
                    'value' => true
                ];
            }
        }
 
        return !empty($features) ? ['amenityFeature' => $features] : [];
    }
 
    /**
     * Build available languages from languages array
     *
     * @param array $languages Languages data
     * @return array Schema with availableLanguage field
     */
    public static function buildAvailableLanguages(array $languages): array
    {
        if (empty($languages)) {
            return [];
        }
 
        $items = [];
 
        foreach ($languages as $lang) {
            if (is_array($lang) && isset($lang['language'])) {
                $items[] = [
                    '@type' => 'Language',
                    'name'  => $lang['language']
                ];
            } elseif (is_string($lang) && $lang !== '') {
                $items[] = [
                    '@type' => 'Language',
                    'name'  => $lang
                ];
            }
        }
 
        return !empty($items) ? ['availableLanguage' => $items] : [];
    }
 
    /**
     * Build aggregate rating from rating value and count
     *
     * @param float|string $rating Rating value
     * @param int|string|null $count Number of ratings
     * @return array|null Schema with aggregateRating or null
     */
    public static function buildAggregateRating(float|string $rating, int|string|null $count): ?array
    {
        if (empty($rating)) {
            return null;
        }
 
        $ratingValue = (float)$rating;
        $ratingCount = (int)($count ?? 0);
 
        if ($ratingCount === 0) {
            // Can't have aggregate rating without count
            return null;
        }
 
        return [
            '@type'       => 'AggregateRating',
            'ratingValue' => $ratingValue,
            'ratingCount' => $ratingCount,
            'bestRating'  => 5.0,
            'worstRating' => 1.0
        ];
    }
 
    /**
     * Transform text value
     */
    public static function text($value): string
    {
        return (string)$value;
    }
 
    /**
     * Transform URL value
     */
    public static function url($value): string
    {
        return esc_url_raw($value);
    }
 
    /**
     * Transform email value
     */
    public static function email($value): string
    {
        return sanitize_email($value);
    }
 
    /**
     * Transform number value
     */
    public static function number($value): float|int
    {
        return is_numeric($value) ? (float)$value : 0;
    }
 
    /**
     * Transform date value to ISO format (YYYY-MM-DD)
     */
    public static function date($value): string
    {
        if (empty($value)) return '';
 
        // If already in ISO format, return as-is
        if (preg_match('/^\d{4}-\d{2}-\d{2}$/', $value)) {
            return $value;
        }
 
        // Otherwise convert to ISO format
        $timestamp = is_numeric($value) ? $value : strtotime($value);
        return $timestamp ? date('Y-m-d', $timestamp) : '';
    }
 
    /**
     * Transform datetime value to ISO 8601 format
     */
    public static function datetime($value): string
    {
        if (empty($value)) return '';
 
        // If already in ISO format, return as-is
        if (preg_match('/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}/', $value)) {
            return $value;
        }
 
        // Otherwise convert to ISO format
        $timestamp = is_numeric($value) ? $value : strtotime($value);
        return $timestamp ? date('c', $timestamp) : '';
    }
 
    /**
     * Transform dimension value to QuantitativeValue schema
     * Examples: "10cm" -> {value: 10, unitCode: "CM"}
     */
    public static function dimension($value): array|string
    {
        if (empty($value)) return '';
 
        // If already an object, return as-is
        if (is_array($value) && isset($value['@type'])) {
            return $value;
        }
 
        // Extract number and unit (e.g., "10cm" -> ["10", "cm"])
        if (preg_match('/^([\d.]+)\s*([a-z]+)$/i', $value, $matches)) {
            return [
                '@type' => 'QuantitativeValue',
                'value' => (float)$matches[1],
                'unitCode' => strtoupper($matches[2])
            ];
        }
 
        return $value;
    }
 
    /**
     * Transform array of text values from repeater
     * Handles various repeater field formats
     */
    public static function text_array($value): array
    {
        if (!is_array($value)) {
            return [$value];
        }
 
        return array_map(function($item) {
            if (is_array($item)) {
                // Handle repeater format with common field names
                return $item['name'] ?? $item['keyword'] ?? $item['topic'] ?? $item['value'] ?? '';
            }
            return (string)$item;
        }, array_filter($value));
    }
 
    /**
     * Transform array of URLs from repeater
     */
    public static function url_array($value): array
    {
        if (!is_array($value)) {
            return [$value];
        }
 
        return array_map(function($item) {
            if (is_array($item)) {
                return esc_url_raw($item['url'] ?? '');
            }
            return esc_url_raw($item);
        }, array_filter($value));
    }
 
    /**
     * Transform image ID to ImageObject
     * Reuses existing buildImage method
     */
    public static function image_object($imageId): array|string
    {
        if (!$imageId) return '';
        return self::buildImage($imageId);
    }
 
    /**
     * Transform array of image IDs to ImageObject array
     * Handles two formats:
     * 1. Simple array: [123, 456, 789]
     * 2. Repeater format: [['image' => 123, 'caption' => 'Before'], ...]
     */
    public static function image_object_array($value): array
    {
        if (!is_array($value)) {
            return [];
        }
 
        return array_values(array_filter(array_map(function($item, $index) {
            // Handle repeater format with sub-fields
            if (is_array($item) && isset($item['image'])) {
                $imageObject = self::buildImage($item['image']);
 
                if (empty($imageObject)) {
                    return null;
                }
 
                if (!empty($item['caption'])) {
                    $imageObject['caption'] = $item['caption'];
                }
 
                if (isset($item['position'])) {
                    $imageObject['position'] = (int)$item['position'];
                } else {
                    $imageObject['position'] = $index;
                }
 
                return $imageObject;
            }
 
            // Handle simple array of IDs
            if (is_numeric($item)) {
                $imageObject = self::buildImage($item);
 
                if (empty($imageObject)) {
                    return null;
                }
 
                $imageObject['position'] = $index;
 
                // Try to get caption from image post
                $post = get_post($item);
                if ($post && !empty($post->post_excerpt)) {
                    $imageObject['caption'] = $post->post_excerpt;
                }
 
                return $imageObject;
            }
 
            return null;
        }, $value, array_keys($value))));
    }
 
    public static function image_url($imageId): string
    {
        if (!$imageId) {
            return '';
        }
 
        // If already a URL string, return as-is
        if (is_string($imageId) && (str_starts_with($imageId, 'http://') || str_starts_with($imageId, 'https://'))) {
            return $imageId;
        }
 
        // Get URL from attachment ID
        $image_url = wp_get_attachment_image_url((int)$imageId, 'full');
 
        return $image_url ?: '';
    }
    /**
     * Transform location to PostalAddress + GeoCoordinates
     * Returns array with 'address' and 'geo' keys
     *
     * Special case: returns multiple schema properties
     */
    public static function location_complex($location): array
    {
        if (!$location) return [];
        return self::buildLocation($location);
    }
 
    /**
     * Transform location to just PostalAddress
     */
    public static function postal_address($location): array
    {
        if (!is_array($location) || empty($location['address'])) {
            return [];
        }
 
        $address = [
            '@type'         => 'PostalAddress',
            'streetAddress' => $location['address']
        ];
 
        if (!empty($location['city'])) {
            $address['addressLocality'] = $location['city'];
        }
 
        if (!empty($location['province'])) {
            $address['addressRegion'] = $location['province'];
        }
 
        if (!empty($location['postal_code'])) {
            $address['postalCode'] = $location['postal_code'];
        }
 
        if (!empty($location['country'])) {
            $address['addressCountry'] = $location['country'];
        }
 
        return $address;
    }
 
    /**
     * Transform coordinates to GeoCoordinates
     * Reuses existing buildGeoCoordinates method
     */
    public static function geo_coordinates($coords): array
    {
        if (!is_array($coords)) return [];
        return self::buildGeoCoordinates($coords);
    }
 
    /**
     * Transform opening hours group to OpeningHoursSpecification
     * Reuses existing buildOpeningHours method
     */
    public static function opening_hours_specification($hours): array
    {
        if (!is_array($hours)) return [];
        $result = self::buildOpeningHours($hours);
        return $result['openingHours'] ?? [];
    }
 
    /**
     * Transform contact points repeater to ContactPoint array
     */
    public static function contact_point_array($contacts): array
    {
        if (!is_array($contacts)) return [];
 
        $contactPoints = [];
        foreach ($contacts as $contact) {
            if (empty($contact['contactType'])) continue;
 
            $point = [
                '@type' => 'ContactPoint',
                'contactType' => $contact['contactType']
            ];
 
            if (!empty($contact['telephone'])) {
                $point['telephone'] = $contact['telephone'];
            }
 
            if (!empty($contact['email'])) {
                $point['email'] = $contact['email'];
            }
 
            $contactPoints[] = $point;
        }
 
        return $contactPoints;
    }
 
    /**
     * Transform amenity features repeater
     * Reuses existing buildAmenityFeatures method
     */
    public static function amenity_feature_array($amenities): array
    {
        if (!is_array($amenities)) return [];
        $result = self::buildAmenityFeatures($amenities);
        return $result['amenityFeature'] ?? [];
    }
 
    /**
     * Transform languages repeater
     * Reuses existing buildAvailableLanguages method
     */
    public static function language_array($languages): array
    {
        if (!is_array($languages)) return [];
        $result = self::buildAvailableLanguages($languages);
        return $result['availableLanguage'] ?? [];
    }
 
    /**
     * Transform aggregate rating group to AggregateRating schema
     */
    public static function aggregate_rating($rating): ?array
    {
        if (!is_array($rating) || empty($rating['ratingValue'])) {
            return null;
        }
 
        $aggregateRating = [
            '@type' => 'AggregateRating',
            'ratingValue' => (float)$rating['ratingValue']
        ];
 
        if (!empty($rating['bestRating'])) {
            $aggregateRating['bestRating'] = (float)$rating['bestRating'];
        }
 
        if (!empty($rating['worstRating'])) {
            $aggregateRating['worstRating'] = (float)$rating['worstRating'];
        }
 
        if (!empty($rating['ratingCount'])) {
            $aggregateRating['ratingCount'] = (int)$rating['ratingCount'];
        }
 
        if (!empty($rating['reviewCount'])) {
            $aggregateRating['reviewCount'] = (int)$rating['reviewCount'];
        }
 
        return $aggregateRating;
    }
 
    /**
     * Transform hasOfferCatalog field data to OfferCatalog schema
     * Handles both manual items and auto-generated from post type
     * Reuses existing buildServiceCatalog method
     */
    public static function offer_catalog_array($data): array
    {
        if (!is_array($data)) return [];
 
        // Extract manual items if present
        if (array_key_exists('manual_items', $data) && !empty($data['manual_items'])) {
            $services = $data['manual_items'];
        }
        // Otherwise expect array of items directly
        else if (isset($data[0])) {
            $services = $data;
        }
        else {
            return [];
        }
 
        // Build the catalog using existing method
        return self::buildServiceCatalog($services);
    }
 
    /**
     * Transform credentials repeater to EducationalOccupationalCredential array
     */
    public static function credential_array($credentials): array
    {
        if (!is_array($credentials)) return [];
 
        $items = [];
        foreach ($credentials as $cred) {
            if (empty($cred['name'])) continue;
 
            $item = [
                '@type' => 'EducationalOccupationalCredential',
                'name' => $cred['name']
            ];
 
            if (!empty($cred['credentialCategory'])) {
                $item['credentialCategory'] = $cred['credentialCategory'];
            }
 
            if (!empty($cred['issuedBy'])) {
                $item['recognizedBy'] = [
                    '@type' => 'Organization',
                    'name' => $cred['issuedBy']
                ];
            }
 
            $items[] = $item;
        }
 
        return $items;
    }
 
    /**
     * Transform FAQ repeater to Question schema array
     */
    public static function faq_array($faqs): array
    {
        if (!is_array($faqs)) return [];
 
        $questions = [];
        foreach ($faqs as $faq) {
            if (empty($faq['question']) || empty($faq['answer'])) {
                continue;
            }
 
            $questions[] = [
                '@type' => 'Question',
                'name' => $faq['question'],
                'acceptedAnswer' => [
                    '@type' => 'Answer',
                    'text' => $faq['answer']
                ]
            ];
        }
 
        return $questions;
    }
 
    /**
     * Transform PotentialAction configurations to schema.org format
     *
     * @param array $actions Array of action configurations
     * @return array Formatted PotentialAction array
     */
    public static function potential_action_array($actions): array
    {
        if (empty($actions) || !is_array($actions)) {
            return [];
        }
 
        $formatted = [];
 
        foreach ($actions as $action) {
            if (empty($action['type']) || empty($action['name'])) {
                continue; // Skip invalid actions
            }
 
            $formattedAction = [
                '@type' => $action['type'],
                'name'  => $action['name'],
            ];
 
            // Add target (required for most actions)
            if (!empty($action['target'])) {
                $target = $action['target'];
 
                // If target contains a query placeholder, format as EntryPoint
                if (str_contains($target, '{')) {
                    $formattedAction['target'] = [
                        '@type'       => 'EntryPoint',
                        'urlTemplate' => $target,
                    ];
                } else {
                    $formattedAction['target'] = $target;
                }
            }
 
            // Add optional fields
            if (!empty($action['description'])) {
                $formattedAction['description'] = $action['description'];
            }
 
            if (!empty($action['url'])) {
                $formattedAction['url'] = $action['url'];
            }
 
            $formatted[] = $formattedAction;
        }
 
        return $formatted;
    }
 
 
    /**
     * Build a JSON-LD @id reference
     * String → ['@id' => $value], array with @id → pass through
     */
    public static function reference(mixed $value): array|string
    {
        if (is_array($value) && isset($value['@id'])) {
            return $value;
        }
 
        if (is_string($value) && !empty($value)) {
            return ['@id' => $value];
        }
 
        return $value;
    }
 
 
 
    /**
     * Build SiteNavigationElement array from navigation items
     */
    public static function buildSiteNavigation(array $items): array
    {
        $elements = [];
        $position = 1;
 
        foreach ($items as $item) {
            if (empty($item['name']) || empty($item['url'])) continue;
 
            $nav = [
                '@type' => 'SiteNavigationElement',
                '@id' => $item['url'] . '#navigation',
                'position' => $position++,
                'name' => $item['name'],
                'url' => $item['url'],
            ];
 
            if (!empty($item['description'])) {
                $nav['description'] = $item['description'];
            }
 
            $elements[] = $nav;
        }
 
        return ['hasPart' => $elements];
    }
 
    /**
     * Build Offer object
     */
    public static function buildOfferObject(array $data): array
    {
        $offer = ['@type' => 'Offer'];
 
        if (!empty($data['price'])) {
            $offer['price'] = (string)$data['price'];
            $offer['priceCurrency'] = $data['priceCurrency'] ?? 'USD';
        }
 
        if (!empty($data['availability'])) {
            $offer['availability'] = 'https://schema.org/' . $data['availability'];
        }
 
        if (!empty($data['validFrom'])) {
            $offer['validFrom'] = $data['validFrom'];
        }
 
        if (!empty($data['validThrough'])) {
            $offer['validThrough'] = $data['validThrough'];
        }
 
        return $offer;
    }
 
    /**
     * Build Brand object or simple text
     */
    public static function buildBrandObject(array $data): array|string
    {
        if (empty($data['name'])) {
            return '';
        }
 
        // Simple text brand
        if (empty($data['type']) || $data['type'] === 'text') {
            return $data['name'];
        }
 
        // Organization/Brand object
        $brand = [
            '@type' => 'Brand',
            'name' => $data['name'],
        ];
 
        if (!empty($data['url'])) {
            $brand['url'] = $data['url'];
        }
 
        if (!empty($data['logo'])) {
            $brand['logo'] = self::buildImage($data['logo']);
        }
 
        return $brand;
    }
 
    /**
     * Build Review array
     */
    public static function buildReviewArray(array $reviews): array
    {
        $output = [];
 
        foreach ($reviews as $review) {
            if (empty($review['author']) && empty($review['reviewBody'])) {
                continue;
            }
 
            $item = ['@type' => 'Review'];
 
            if (!empty($review['author'])) {
                $item['author'] = [
                    '@type' => 'Person',
                    'name' => $review['author']
                ];
            }
 
            if (!empty($review['reviewRating'])) {
                $item['reviewRating'] = [
                    '@type' => 'Rating',
                    'ratingValue' => $review['reviewRating'],
                ];
            }
 
            if (!empty($review['reviewBody'])) {
                $item['reviewBody'] = $review['reviewBody'];
            }
 
            if (!empty($review['datePublished'])) {
                $item['datePublished'] = $review['datePublished'];
            }
 
            $output[] = $item;
        }
 
        return $output;
    }
 
    /**
     * Build organization reference
     */
    public static function buildOrganizationReference(array $data): array
    {
        if (empty($data['name'])) {
            return [];
        }
 
        $org = [
            '@type' => 'Organization',
            'name' => $data['name'],
        ];
 
        if (!empty($data['url'])) {
            $org['url'] = $data['url'];
        }
 
        return $org;
    }
 
    /**
     * Build organization reference array
     */
    public static function buildOrganizationReferenceArray(array $items): array
    {
        return array_map([self::class, 'buildOrganizationReference'], $items);
    }
 
    /**
     * Build person reference array
     */
    public static function buildPersonReferenceArray(array $items): array
    {
        $output = [];
 
        foreach ($items as $item) {
            if (empty($item['name'])) continue;
 
            $person = [
                '@type' => 'Person',
                'name' => $item['name'],
            ];
 
            if (!empty($item['jobTitle'])) {
                $person['jobTitle'] = $item['jobTitle'];
            }
 
            $output[] = $person;
        }
 
        return $output;
    }
 
    /**
     * Boolean transformer
     */
    public static function buildBoolean(mixed $value): bool
    {
        return (bool)$value;
    }
 
    /**
     * Time transformer
     */
    public static function buildTime(string $value): string
    {
        // Ensure format is HH:MM
        return date('H:i', strtotime($value));
    }
 
    /**
     * Rating object
     */
    public static function buildRatingObject(array $data): array
    {
        if (empty($data['ratingValue'])) {
            return [];
        }
 
        return [
            '@type' => 'Rating',
            'ratingValue' => (float)$data['ratingValue'],
            'bestRating' => 5,
        ];
    }
}