PluginProbe
PostNL for WooCommerce / 4.4.0
PostNL for WooCommerce v4.4.0
5.9.12 5.9.11 5.9.10 5.9.9 5.9.8 5.9.7 5.9.6 trunk 2.5.0 2.5.1 2.5.2 2.5.3 2.5.4 2.5.5 3.1.4 3.1.5 3.1.6 3.1.7 4.0.0 4.0.1 4.0.2 4.3.2 4.3.3 4.4.0 4.4.1 All 72 releases
woo-postnl / vendor / myparcelnl / sdk / src / Helper / ValidateStreet.php

ValidateStreet.php in PostNL for WooCommerce 4.4.0, at vendor/myparcelnl/sdk/src/Helper/ValidateStreet.php

89 lines 3.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php declare(strict_types=1);
2
3 namespace MyParcelNL\Sdk\src\Helper;
4
5 use MyParcelNL\Sdk\src\Model\Consignment\AbstractConsignment;
6
7 class ValidateStreet
8 {
9 /**
10 * Regular expression used to split street name from house number for the Netherlands.
11 *
12 * This regex goes from right to left
13 * Contains php keys to store the data in an array
14 */
15 const SPLIT_STREET_REGEX_NL =
16 '~(?P<street>.{1,78}?)' . // The rest belongs to the street
17 '\s' . // Separator between street and number
18 '(?P<number>\d{1,5})' . // Number can contain a maximum of 5 numbers
19 '[/\s\-]{0,2}' . // Separators between number and addition
20 '(?P<number_suffix>' .
21 '[a-z]{1}-?\d{1,3}|' . // Numbers suffix starts with a letter with optional - followed by numbers or
22 '-\d{1,4}|' . // starts with - and has up to 4 numbers or
23 '\d{2}\w{1,2}|' . // starts with 2 numbers followed by letters or
24 '[a-z]{1}[a-z\s]{0,3}' . // has up to 4 letters with a space
25 ')?$~i';
26
27 const SPLIT_STREET_REGEX_BE =
28 '~(?P<street>.*?)\s(?P<street_suffix>(?P<number>[0-9\-]{1,8})\s?(?P<box_separator>' . SplitStreet::BOX_NL . ')?\s?(?P<box_number>\d{0,8})\s?(?P<number_suffix>[A-z]{0,4}$)?)$~';
29
30 /**
31 * @param string $fullStreet
32 * @param string $localCountry
33 * @param string|null $destinationCountry
34 *
35 * @return bool
36 */
37 public static function validate(string $fullStreet, string $localCountry, ?string $destinationCountry): bool
38 {
39 if (null === $destinationCountry) {
40 return true;
41 }
42
43 $regex = ValidateStreet::getStreetRegexByCountry($localCountry, $destinationCountry);
44
45 if (! $regex) {
46 return true;
47 }
48
49 $result = preg_match($regex, $fullStreet, $matches);
50
51 if (! $result || ! is_array($matches)) {
52 // Invalid full street supplied
53 return false;
54 }
55
56 $fullStreet = str_replace('\n', ' ', $fullStreet);
57 if ($fullStreet != $matches[0]) {
58 // Characters are gone by preg_match
59 return false;
60 }
61
62 return (bool) $result;
63 }
64
65 /**
66 * @param string $local
67 * @param string $destination
68 *
69 * @return null|string
70 */
71 public static function getStreetRegexByCountry(string $local, string $destination): ?string
72 {
73 $localIsBe = $local === AbstractConsignment::CC_BE;
74 $localIsNlOrBe = in_array($local, [AbstractConsignment::CC_BE, AbstractConsignment::CC_NL]);
75 $destinationIsNl = $destination === AbstractConsignment::CC_NL;
76 $destinationIsBe = $destination === AbstractConsignment::CC_BE;
77
78 if ($localIsNlOrBe && $destinationIsNl) {
79 return self::SPLIT_STREET_REGEX_NL;
80 }
81
82 if ($localIsBe && $destinationIsBe) {
83 return self::SPLIT_STREET_REGEX_BE;
84 }
85
86 return null;
87 }
88 }
89