33namespace RahulGodiyal \PhpUpsApiWrapper ;
44
55use RahulGodiyal \PhpUpsApiWrapper \Auth ;
6+ use RahulGodiyal \PhpUpsApiWrapper \Utils \HttpClient ;
67
78class AddressValidation extends Auth
89{
9- private static $ _address ;
10- private $ _request_option ;
11- private $ _version ;
12- private $ _query ;
10+ private const REQUEST_OPTION = "3 " ;
11+ private const VERSION = "v2 " ;
1312
14- public function __construct ()
15- {
16- parent ::__construct ();
17- $ this ->_request_option = "3 " ;
18- $ this ->_version = "v2 " ;
19- $ this ->_query = [
20- "regionalrequestindicator " => "string " ,
21- "maximumcandidatelistsize " => "1 "
22- ];
23- }
13+ private static array $ address ;
14+ private array $ query = [
15+ "regionalrequestindicator " => "string " ,
16+ "maximumcandidatelistsize " => "1 "
17+ ];
2418
25- /**
26- * Set Address to validate
27- * @param array $address
28- * @return self
29- */
30- public static function setAddress (array $ address )
19+ public static function setAddress (array $ address ): self
3120 {
32- self ::$ _address = $ address ;
21+ self ::$ address = $ address ;
3322 return new self ;
3423 }
3524
36- /**
37- * Validate the address
38- * @param string $client_id
39- * @param string $client_secret
40- * @return array of validated address
41- */
42- public function validate (String $ client_id , String $ client_secret )
25+ public function validate (string $ client_id , string $ client_secret ): array
4326 {
4427 $ auth = $ this ->authenticate ($ client_id , $ client_secret );
4528
@@ -48,23 +31,17 @@ public function validate(String $client_id, String $client_secret)
4831 }
4932
5033 $ access_token = $ auth ['access_token ' ];
51- $ curl = curl_init ();
52-
53- curl_setopt_array ($ curl , [
54- CURLOPT_HTTPHEADER => [
55- "Authorization: Bearer $ access_token " ,
56- "Content-Type: application/json "
57- ],
58- CURLOPT_POSTFIELDS => json_encode ($ this ->_payload ()),
59- CURLOPT_URL => $ this ->_getAPIBaseURL () . "/api/addressvalidation/ " . $ this ->_version . "/ " . $ this ->_request_option . "? " . http_build_query ($ this ->_query ),
60- CURLOPT_RETURNTRANSFER => true ,
61- CURLOPT_CUSTOMREQUEST => "POST " ,
34+
35+ $ httpClient = new HttpClient ();
36+ $ httpClient ->setHeader ([
37+ "Authorization: Bearer $ access_token " ,
38+ "Content-Type: application/json "
6239 ]);
63-
64- $ response = curl_exec ( $ curl );
65- curl_close ( $ curl );
66- $ res = json_decode ( $ response );
67-
40+ $ httpClient -> setPayload ( json_encode ( $ this -> getPayLoad ()));
41+ $ httpClient -> setUrl ( $ this -> _getAPIBaseURL () . " /api/addressvalidation/ " . self :: VERSION . " / " . self :: REQUEST_OPTION . " ? " . http_build_query ( $ this -> query ) );
42+ $ httpClient -> setMethod ( " POST " );
43+ $ res = $ httpClient -> fetch ( );
44+
6845 if (!isset ($ res ->XAVResponse )) {
6946 if (isset ($ res ->response )) {
7047 $ error = $ res ->response ->errors [0 ]->message ;
@@ -78,29 +55,20 @@ public function validate(String $client_id, String $client_secret)
7855 return ['status ' => 'fail ' , 'msg ' => "Invalid Address. " ];
7956 }
8057
81- $ addresses = $ this ->_getAddresses ($ res ->XAVResponse ->Candidate );
58+ $ addresses = $ this ->getAddresses ($ res ->XAVResponse ->Candidate );
8259 return ['status ' => 'success ' , 'addresses ' => $ addresses ];
8360 }
8461
85- /**
86- * Get Payload
87- * @return array $payload
88- */
89- private function _payload ()
62+ private function getPayLoad (): array
9063 {
9164 return [
9265 "XAVRequest " => [
93- "AddressKeyFormat " => self ::$ _address
66+ "AddressKeyFormat " => self ::$ address
9467 ]
9568 ];
9669 }
9770
98- /**
99- * Get Addresses
100- * @param array of objects
101- * @return array of addresses
102- */
103- private function _getAddresses (Array $ candidates )
71+ private function getAddresses (array $ candidates ): array
10472 {
10573 $ addresses = [];
10674 foreach ($ candidates as $ candObj ) {
@@ -119,11 +87,7 @@ private function _getAddresses(Array $candidates)
11987 return $ addresses ;
12088 }
12189
122- /**
123- * Set Mode
124- * @param string DEV|PROD
125- */
126- public function setMode (String $ mode )
90+ public function setMode (string $ mode ): self
12791 {
12892 parent ::setMode ($ mode );
12993 return $ this ;
0 commit comments