-
Notifications
You must be signed in to change notification settings - Fork 2
/
StreetAddress.php
736 lines (597 loc) · 22.6 KB
/
StreetAddress.php
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
<?php namespace ProcessWire;
/**
* 2018-2019 Netcarver.
*
*/
class StreetAddress
{
/**
* Store initial wakeup values to allow detection of changed fields.
*/
protected $snapshot = [];
/**
* Address fields
*/
public $recipient = '';
public $organization = '';
public $street_address = '';
public $street_address_2 = '';
public $street_address_3 = '';
public $locality = '';
public $dependent_locality = '';
public $admin_area = '';
public $postal_code = '';
public $sorting_code = '';
public $country = '';
public $country_iso = '';
public $origin_iso = '';
/**
* Consrtuctor can optionally take an array of address lines.
*/
public function __construct(string $value = '{}')
{
$lines = json_decode($value, TRUE);
// Make sure address data uses lowercase keys...
$lines = array_change_key_case($lines, CASE_LOWER);
// Merge in defaults
$lines = array_merge(self::$address_lines, $lines);
if (!empty($lines)) {
foreach ($lines as $key => $value) {
$this->$key = $value;
}
}
}
public function getValue()
{
$vars = get_object_vars($this);
unset($vars['snapshot']);
unset($vars['country']);
/* unset($vars['append_destination_iso']); */
/* unset($vars['destination_country_fmt']); */
return $vars;
}
/**
* Convenience Format Methods...
*/
public function formatMultiHtml($format_overrides = [])
{
return $this->format(true, false, $format_overrides);
}
public function formatSingleHtml($line_glue = ', ', $format_overrides = [])
{
return $this->format(true, $line_glue, $format_overrides);
}
public function formatMultiPlain($format_overrides = [])
{
return $this->format(false, false, $format_overrides);
}
public function formatSinglePlain($line_glue = ', ', $format_overrides = [])
{
return $this->format(false, $line_glue, $format_overrides);
}
public function formatSingle($html = false, $line_glue = ', ', $format_overrides = [])
{
return $this->format($html, $line_glue, $format_overrides);
}
/**
* Format address
*
* @param bool $html Controls formatting for HTML. If true, HTML wrappers will be used and output will be escaped.
* @param string|bool $line_glue False => Multiline output. A string acts as glue for joining the address lines.
* @param array $format_overrides Allows optional overrides of the formatting metadata. Most useful field is probably
* the 'upper' string. To prevent output from uppercasing fields use ['upper' => '']
*
* @see Contents of formats.php
* @see StreetAddress::formatLines()
*/
public function format($html = false, $line_glue = false, $format_overrides = [])
{
$vars = $this->getValue();
/* $vars = get_object_vars($this); */
/* unset($vars['snapshot']); */
/* unset($vars['country']); */
return self::formatLines($vars, $html, $line_glue, $format_overrides);
}
/**
* Magic method to convert to a formatted string - non HTML.
*/
public function __toString()
{
if (!empty($this->form_builder)) {
$value = $this->getValue();
unset($value['form_builder']);
$value = array_filter($value, function($v) { return $v !== ''; }); // Remove empty fields as not needed.
$result = json_encode($value);
if (false === $result)
return "";
return $result;
}
return $this->formatSingle();
}
/**
* Check the address is valid.
*/
public function validate()
{
$vars = $this->getValue();
return self::validateLines($vars);
}
/**
* Take a snapshot of the fields to allow change detection later on.
*/
public function snapshot()
{
$snapshot = $this->getValue();
unset($snapshot['origin_iso']);
$this->snapshot = $snapshot;
return $this;
}
/**
* Check if a field has changed since the last snapshot.
*/
public function isChanged($field)
{
return (@$this->snapshot[$field] != $this->$field);
}
/**
* Check if this instance is empty or has at least a partial value.
*
* The country, origin_iso and country_iso are ignored in this check. In reality, we want values like
* street_address, locality and postal_code to be populated.
*/
public function isEmpty()
{
$vars = $this->getValue();
/* $vars = get_object_vars($this); */
/* unset($vars['snapshot']); */
/* unset($vars['country']); */
unset($vars['country_iso']);
unset($vars['origin_iso']);
/* unset($vars['append_destination_iso']); */
/* unset($vars['destination_country_fmt']); */
$num_set_fields = 0;
foreach ($vars as $value) {
if (!empty($value)) $num_set_fields++;
}
$is_empty = 0 === $num_set_fields;
return $is_empty;
}
/**
* Passes each address line to a definable callback function to prepare it for output.
*/
public function prepareLines($callback, array $data = [])
{
if (!is_callable($callback)) return $this;
$vars = $this->getValue();
if (count($vars)) {
foreach ($vars as $key => $value) {
//$newvalue = $callback($value, $key, $data);
$newvalue = call_user_func($callback, $value, $key, $data);
if ($newvalue != $value) {
$this->$key = $newvalue;
}
}
}
return $this;
}
// =================================================================================================================
/**
*
*/
protected static $country_formats;
/**
* If set, entries in the remappings array allow you to specify custom names for supplied address fields
* beyond those used by google's i18n feed.
*
* For example, if your system has a field called 'rue' that you wish to use for the street address field, you would
* add the following line to your remappings...
*
* 'rue' => 'street_address',
*
* In the US, if you wish to use 'city' instead of the standard 'locality' field add...
*
* 'city' => 'locality',
*
* Note, these mappings are done in addition to some standard remappings for any locale-dependent hints and known
* postcodes.
*
* These remappings are applied prior to formatting a supplied address.
*
* @see function remapAddressFields();
* @see function setFieldMappings();
* @see function getFieldMappings();
*/
protected static $remappings = [];
public static function setFieldMappings(array $mappings)
{
self::$remappings = $mappings;
}
public static function getFieldMappings()
{
return self::$remappings;
}
/**
* Google's address field shortcut letters. Used when formatting an address.
*/
protected static $address_mapping = [
'A' => 'street_address',
'C' => 'locality',
'D' => 'dependent_locality',
'N' => 'recipient',
'O' => 'organization', // Yes, dealing with American spelling from Google.
'R' => 'country',
'S' => 'admin_area',
'X' => 'sorting_code',
'Z' => 'postal_code',
];
public static function getAddressMappings()
{
return self::$address_mapping;
}
/**
* Itemprops for generating HTML with PostalAddress schema markup.
*/
protected static $itemprops = [
'admin_area' => 'addressRegion',
'locality' => 'addressLocality',
'recipient' => 'name',
'organization' => 'affiliation',
'dependent_locality' => '',
'postal_code' => 'postalCode',
'sorting_code' => '',
'street_address' => 'streetAddress',
'country' => 'addressCountry',
];
/**
* Scratch for values.
*/
protected static $address_lines = [
'recipient' => '',
'organization' => '',
'street_address' => '',
'street_address_2' => '',
'street_address_3' => '',
'locality' => '', // usually city/postal town
'dependent_locality' => '', // usually sub-district
'admin_area' => '', // usually state/county
'postal_code' => '',
'sorting_code' => '',
'country' => '', // Internal use
'country_iso' => '',
'origin_iso' => '', // Internal use
];
public static function getAddressFieldNames($all=false)
{
$lines = self::$address_lines;
if (!$all) {
// Remove 'internal' keys
unset($lines['country']);
unset($lines['origin_iso']);
}
$keys = array_keys($lines);
return $keys;
}
/**
* Maps a locale-specific alternative field to one of the standard address fields...
*/
protected static function remapAddressField($from_key, $to_key, &$data)
{
if (empty($from_key)) return;
$from_key = mb_convert_case($from_key, MB_CASE_LOWER, 'utf-8');
$original = @$data[$from_key];
if (empty($original)) return; // Don't risk overwriting the $data[$to_key] field if there is no source data.
$data[$to_key] = $data[$from_key];
}
/**
* Allow country/language specific remappings for the state and locality fields.
* Adds rules to capture common remappings for the postal_code field as well.
*/
protected static function remapAddressFields($info, $data)
{
$out = $data;
$state_remap = @$info['state_name_type'];
$locality_remap = @$info['locality_name_type'];
// Handle hint data from Google i18n feed...
self::remapAddressField($state_remap, 'admin_area', $data);
self::remapAddressField($locality_remap, 'locality', $data);
// Handle common alternative spelling of 'organisation'...
self::remapAddressField('organisation', 'organization', $data);
// Handle alternatives to postal_code such as "postcode", "zip" etc.....
$country_iso = $data['country_iso'];
$lang = @$info['lang'];
if ('en' === $lang) {
if ($country_iso === 'US' || $country_iso === 'PH') {
self::remapAddressField('zip', 'postal_code', $data);
self::remapAddressField('zipcode', 'postal_code', $data);
self::remapAddressField('zip_code', 'postal_code', $data);
} else if ($country_iso === 'IE') {
self::remapAddressField('eircode', 'postal_code', $data);
} else {
self::remapAddressField('postcode', 'postal_code', $data);
}
}
switch ($country_iso) {
case 'NL':
self::remapAddressField('postcode', 'postal_code', $data);
break;
case 'BR':
self::remapAddressField('cep', 'postal_code', $data);
break;
case 'IN':
self::remapAddressField('pincode', 'postal_code', $data);
self::remapAddressField('pin_code', 'postal_code', $data);
break;
case 'GB':
self::remapAddressField('postcode', 'postal_code', $data);
break;
case 'DE':
case 'AT':
case 'LI':
self::remapAddressField('plz', 'postal_code', $data);
break;
case 'IT':
self::remapAddressField('cap', 'postal_code', $data);
break;
}
// Apply any custom remappings...
if (!empty(self::$remappings)) {
foreach (self::$remappings as $from => $to) {
self::remapAddressField($from, $to, $data);
}
}
return $data;
}
protected static $country_cache = [];
/**
*
*/
public static function country($data, $format_info, $dest_iso, $origin_iso)
{
$modname = "LibLocalisation";
$can_localise = wire('modules')->isInstalled($modname);
$country = '';
$locale = '';
$load = !isset(self::$country_cache[$origin_iso]) || !is_array(self::$country_cache[$origin_iso]);
// TODO remove use of '++' for appending destination country to output.
// Would be better to keep "origin_iso" and "append_destination" settings separately.
// This will allow localisation no matter what the append settings are.
if ('++' === $origin_iso || !isset($format_info['destination_country_fmt']) || 0 == $format_info['destination_country_fmt'] || !$can_localise) {
if (isset($format_info['name'])) {
$list = [$dest_iso => $format_info['name']];
}
} else {
if ($load) {
$localisation = wire('modules')->get($modname);
$locale = LibLocalisation::countryToLocale(strtolower($origin_iso));
$localisation->setLocale($locale);
$list = $localisation->country('');
if (!isset($list) || !is_array($list) || !isset($list[$dest_iso])) {
$country_file = __DIR__."/countries.php";
if(is_readable($country_file)) {
$list = include($country_file);
}
}
self::$country_cache[$origin_iso] = $list;
} else {
$list = self::$country_cache[$origin_iso];
}
}
if (isset($list[$dest_iso])) {
$country = $list[$dest_iso];
}
if (isset($format_info['destination_country_fmt']) && 2 == $format_info['destination_country_fmt']) {
$en_name = isset($format_info['name']) ? $format_info['name'] : '';
if (!empty($en_name) && mb_strtoupper($en_name) != mb_strtoupper($country)) {
$country .= " / " . $en_name;
}
}
if (isset($format_info['append_destination_iso']) && 1 == $format_info['append_destination_iso']) {
$country .= " ($dest_iso)";
}
return $country;
}
/**
* Format the given address data into an address string.
*
* @param array $data A set of named address strings.
* @param bool $html true => wrap address elements in HTML markup that includes microformat classes...
* @return string The formatted address.
*/
protected static function formatLines(array $data, $html = false, $line_glue = false, $format_info = [])
{
$data = array_change_key_case($data, CASE_LOWER);
$format_info = array_change_key_case($format_info, CASE_LOWER);
$data = array_merge(self::$address_lines, $data);
$address_country_iso = strtoupper($data['country_iso']);
$origin_iso = strtoupper($data['origin_iso']);
$format_info = array_merge(self::getFormat($address_country_iso), $format_info);
$data = self::remapAddressFields($format_info, $data);
$upper = isset($format_info['upper']) ? $format_info['upper'] : '';
$formatted_address = $format_info['fmt'];
// Setup the inter-line glue
if (!is_bool($line_glue)) {
$glue = $line_glue;
} else {
$glue = $html ? '<br>' : "\n";
}
if ($address_country_iso !== $origin_iso) {
// This is an international address - add the country to the format if it is not already present...
$pos = strpos($formatted_address, '%R');
if (false === $pos) {
$formatted_address .= '%n%R';
}
$upper .= "R"; // Make sure country is uppercase
$country = self::country($data, $format_info, $address_country_iso, $origin_iso);
$data['country'] = $country;
}
// Replace formatted address elements with items from the data as needed.
foreach (self::$address_mapping as $id => $key) {
$value = trim(strip_tags($data[$key]));
$value = str_replace([''', '''], "'", $value);
$is_upper = (false !== stripos($upper, $id));
if ('Z' === $id) {
$value = self::sanitizePostalCode($value, $address_country_iso);
}
// Make sure the fields marked as "upper" in the google feed are converted to uppercase.
if ($is_upper) {
$value = mb_convert_case($value, MB_CASE_UPPER, 'utf-8');
}
if ($html && $value) {
$value = htmlspecialchars($value, ENT_QUOTES | ENT_HTML5, 'utf-8', false);
}
if ('A' === $id) {
$value2 = $data['street_address_2'];
$value3 = $data['street_address_3'];
if ($is_upper) {
$value2 = mb_convert_case($value2, MB_CASE_UPPER, 'utf-8');
$value3 = mb_convert_case($value3, MB_CASE_UPPER, 'utf-8');
}
if ($html) {
$value2 = htmlspecialchars($value2, ENT_QUOTES | ENT_HTML5, 'utf-8', false);
$value3 = htmlspecialchars($value3, ENT_QUOTES | ENT_HTML5, 'utf-8', false);
}
$value = $value . ($value2 ? $glue . $value2 : '');
$value = $value . ($value3 ? $glue . $value3 : '');
}
// HMTL gets the postal address microformat wrapping spans...
if ($html && $value) {
$value = "<span" . self::getItemProp($key) . ">{$value}</span>";
}
$formatted_address = str_replace("%{$id}", $value, $formatted_address);
}
$formatted_address = trim(str_replace('%n', "\n", $formatted_address));
$formatted_address = preg_replace("/(^[\r\n]*|[\r\n]+)[\s\t]*[\r\n]+/", "\n", $formatted_address); // \n2+ -> \n
$formatted_address = str_replace("\n", $glue, $formatted_address);
return $formatted_address;
}
/**
*/
public static function getFormat($country_iso)
{
$international_layout = '%N%n%O%n%A%n%C, %S %Z %R';
if (self::$country_formats === null) {
$formats = include_once(__DIR__.'/formats.php');
$overrides = false;
$overrides_file = __DIR__.'/formats_overrides.php';
if (file_exists($overrides_file)) {
$overrides = include_once($overrides_file);
}
if (is_array($overrides)) {
$formats = array_replace_recursive($formats, $overrides);
}
self::setFormats($formats);
}
$country_iso = strtoupper($country_iso);
// Return international format for missing
if (false === array_key_exists($country_iso, self::$country_formats)) {
$format_info = ['fmt' => $international_layout];
} else {
$format_info = self::$country_formats[$country_iso];
if (!isset($format_info['fmt'])) {
$format_info['fmt'] = $international_layout;
}
}
return $format_info;
}
public static function getBlankFormattedValue($iso)
{
$fmt = self::getFormat($iso);
$fmt = $fmt['fmt'];
foreach (self::$address_mapping as $id => $key) {
$fmt = str_replace("%{$id}", '', $fmt);
}
$fmt = trim(str_replace('%n', "\n", $fmt));
// Clean up runs of multiple newlines...
$fmt = preg_replace("/(^[\r\n]*|[\r\n]+)[\s\t]*[\r\n]+/", "\n", $fmt);
return $fmt;
}
public static function getPostalCodeRegex($iso)
{
$info = self::getFormat($iso);
$pcregex = @$info['zip'];
return $pcregex;
}
public static function checkPostalCode($pc, $iso)
{
$regex = self::getPostalCodeRegex($iso);
if (empty($regex)) return true;
$result = preg_match("~$regex~", $pc, $m);
$malformed = (0 === $result) || ($m[0] !== $pc);
return !$malformed;
}
public static function sanitizePostalCode($pc, $iso)
{
$regex = self::getPostalCodeRegex($iso);
if (empty($regex)) return $pc;
$result = preg_match("~$regex~", $pc, $m);
if (1 === $result) {
return $m[0];
}
return '';
}
/**
* Check the address meets google's requirements for an address of the given country.
*/
protected static function validateLines(array $data)
{
$results = [
'valid' => true,
'missing' => [],
'postal_code_valid' => true,
];
// Check all required fields are present...
$data = array_change_key_case($data, CASE_LOWER);
// Merge in defaults
$data = array_merge(self::$address_lines, $data);
// Load country option
$format_info = self::getFormat($data['country_iso']);
$data = self::remapAddressFields($format_info, $data);
$required = @$format_info['require'];
if (empty($required)) {
$required = 'AC'; // Inherit from ZZ default meta data.
}
if (!empty($data['postal_code'])) {
$results['postal_code_valid'] = self::checkPostalCode($data['postal_code'], $data['country_iso']);
}
if (!empty($required)) {
$required = str_split($required);
foreach ($required as $key) {
$addressline = self::$address_mapping[$key];
$value = $data[$addressline];
if (empty($value)) {
$results['valid'] = false;
$results['missing'][] = $addressline;
if ('postal_code' === $addressline) {
$results['postal_code_valid'] = false;
}
}
}
}
return $results;
}
/**
* @param array|null $country_formats
*/
public static function setFormats($country_formats)
{
self::$country_formats = $country_formats;
}
/**
* @return array|null
*/
public static function getFormats()
{
return self::$country_formats;
}
/**
* @param string $key
* @return string
*/
protected static function getItemProp($key)
{
if ($prop = self::$itemprops[$key]) {
return " itemprop=\"{$prop}\"";
}
return '';
}
}