1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\BpostAddressWebservice; |
4
|
|
|
|
5
|
|
|
use Spatie\BpostAddressWebservice\Gateways\Gateway; |
6
|
|
|
use Spatie\BpostAddressWebservice\Gateways\BpostGateway; |
7
|
|
|
use Spatie\BpostAddressWebservice\Requests\ValidateAddressesRequest; |
8
|
|
|
use Spatie\BpostAddressWebservice\Exceptions\CouldNotValidateAddress; |
9
|
|
|
|
10
|
|
|
class AddressValidator |
11
|
|
|
{ |
12
|
|
|
const OPTION_INCLUDE_FORMATTING = 'IncludeFormatting'; |
13
|
|
|
const OPTION_INCLUDE_SUGGESTIONS = 'IncludeSuggestions'; |
14
|
|
|
const OPTION_INCLUDE_SUBMITTED_ADDRESS = 'IncludeSubmittedAddress'; |
15
|
|
|
const OPTION_INCLUDE_DEFAULT_GEO_LOCATION = 'IncludeDefaultGeoLocation'; |
16
|
|
|
const OPTION_INCLUDE_SUFFIX_LIST = 'IncludeSuffixList'; |
17
|
|
|
const OPTION_INCLUDE_NUMBER_OF_SUFFIXES = 'IncludeNumberOfSuffixes'; |
18
|
|
|
const OPTION_INCLUDE_LIST_OF_BOXES = 'IncludeListOfBoxes'; |
19
|
|
|
const OPTION_INCLUDE_NUMBER_OF_BOXES = 'IncludeNumberOfBoxes'; |
20
|
|
|
|
21
|
|
|
/** @var \Spatie\BpostAddressWebservice\Gateways\Gateway */ |
22
|
|
|
protected $gateway; |
23
|
|
|
|
24
|
|
|
/** @var array */ |
25
|
|
|
protected $options = []; |
26
|
|
|
|
27
|
|
|
public function __construct(Gateway $gateway) |
28
|
|
|
{ |
29
|
|
|
$this->gateway = $gateway; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public static function create(): AddressValidator |
33
|
|
|
{ |
34
|
|
|
return new static(new BpostGateway()); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function withOptions(array $options): AddressValidator |
38
|
|
|
{ |
39
|
|
|
$this->options = $options; |
40
|
|
|
|
41
|
|
|
return $this; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function validate(Address $address): ValidatedAddress |
45
|
|
|
{ |
46
|
|
|
$validateAddressesResponse = $this->gateway->validateAddresses( |
47
|
|
|
new ValidateAddressesRequest([$address], $this->options) |
48
|
|
|
); |
49
|
|
|
|
50
|
|
|
return $validateAddressesResponse->validatedAddresses()[0]; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function validateMany(array $addresses): array |
54
|
|
|
{ |
55
|
|
|
$maximumAddresCount = 100; |
56
|
|
|
|
57
|
|
|
if (count($addresses) > $maximumAddresCount) { |
58
|
|
|
throw CouldNotValidateAddress::tooManyAddresses($addresses, $maximumAddresCount); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
$validateAddressesResponse = $this->gateway->validateAddresses( |
62
|
|
|
new ValidateAddressesRequest($addresses, $this->options) |
63
|
|
|
); |
64
|
|
|
|
65
|
|
|
return $validateAddressesResponse->validatedAddresses(); |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|