Skip to content

Commit 84d3e22

Browse files
authored
Update tracking api and validation address api
1 parent dd3b0e8 commit 84d3e22

File tree

7 files changed

+366
-20
lines changed

7 files changed

+366
-20
lines changed

src/Entity/ShipQuery.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
namespace RahulGodiyal\PhpUpsApiWrapper\Entity;
4+
5+
class ShipQuery
6+
{
7+
private string $additionalAddressValidation = "";
8+
9+
public function exists()
10+
{
11+
if (!empty($this->additionalAddressValidation)) {
12+
return true;
13+
}
14+
15+
return false;
16+
}
17+
18+
public function setAdditionalAddressValidation(string $additionalAddressValidation): self
19+
{
20+
$this->additionalAddressValidation = $additionalAddressValidation;
21+
return $this;
22+
}
23+
24+
public function getAdditionalAddressValidation(): string
25+
{
26+
return $this->additionalAddressValidation;
27+
}
28+
29+
public function toArray(): array
30+
{
31+
if ($this->additionalAddressValidation) {
32+
return [
33+
"additionaladdressvalidation" => $this->additionalAddressValidation
34+
];
35+
}
36+
37+
return [];
38+
}
39+
}

src/Entity/TrackingQuery.php

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
<?php
2+
3+
namespace RahulGodiyal\PhpUpsApiWrapper\Entity;
4+
5+
class TrackingQuery
6+
{
7+
private ?string $locale;
8+
private ?string $returnSignature;
9+
private ?string $returnMilestones;
10+
private ?string $returnPOD;
11+
12+
public function exists()
13+
{
14+
if (
15+
!empty($this->locale)
16+
|| !empty($this->returnSignature)
17+
|| !empty($this->returnMilestones)
18+
|| !empty($this->returnPOD)
19+
) {
20+
return true;
21+
}
22+
23+
return false;
24+
}
25+
26+
public function setLocale(string $locale): self
27+
{
28+
$this->locale = $locale;
29+
return $this;
30+
}
31+
32+
public function setReturnSignature(string $returnSignature): self
33+
{
34+
if (in_array(strtolower($returnSignature), ['true', 'false'])) {
35+
$this->returnSignature = $returnSignature;
36+
return $this;
37+
}
38+
39+
throw new \Exception("Return Signature is not valid.");
40+
}
41+
42+
public function setReturnMilestones(string $returnMilestones): self
43+
{
44+
if (in_array(strtolower($returnMilestones), ['true', 'false'])) {
45+
$this->returnMilestones = $returnMilestones;
46+
return $this;
47+
}
48+
49+
throw new \Exception("Return Milestones is not valid.");
50+
}
51+
52+
public function setReturnPOD(string $returnPOD): self
53+
{
54+
if (in_array(strtolower($returnPOD), ['true', 'false'])) {
55+
$this->returnPOD = $returnPOD;
56+
return $this;
57+
}
58+
59+
throw new \Exception("Return POD is not valid.");
60+
}
61+
62+
public function toArray(): array
63+
{
64+
$query = [];
65+
66+
if (isset($this->locale)) {
67+
$query["locale"] = $this->locale;
68+
}
69+
70+
if (isset($this->returnSignature)) {
71+
$query["returnSignature"] = $this->returnSignature;
72+
}
73+
74+
if (isset($this->returnMilestones)) {
75+
$query["returnMilestones"] = $this->returnMilestones;
76+
}
77+
78+
if (isset($this->returnPOD)) {
79+
$query["returnPOD"] = $this->returnPOD;
80+
}
81+
82+
return $query;
83+
}
84+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
3+
namespace RahulGodiyal\PhpUpsApiWrapper\Entity;
4+
5+
class ValidateAddressQuery
6+
{
7+
private ?string $regionalRequestIndicator;
8+
private ?string $maximumCandidateListSize;
9+
10+
public function exists()
11+
{
12+
if (!empty($this->regionalRequestIndicator) || !empty($this->maximumCandidateListSize)) {
13+
return true;
14+
}
15+
16+
return false;
17+
}
18+
19+
public function setRegionalRequestIndicator(string $regionalRequestIndicator): self
20+
{
21+
if (in_array($regionalRequestIndicator, ["True", "False"])) {
22+
$this->regionalRequestIndicator = $regionalRequestIndicator;
23+
return $this;
24+
}
25+
26+
throw new \Exception("Regional Request Indicator is not valid.");
27+
}
28+
29+
public function getRegionalRequestIndicator(): string
30+
{
31+
return $this->regionalRequestIndicator;
32+
}
33+
34+
public function setMaximumCandidateListSize(string $maximumCandidateListSize): self
35+
{
36+
if ($maximumCandidateListSize >= 0 && $maximumCandidateListSize <= 50) {
37+
$this->maximumCandidateListSize = $maximumCandidateListSize;
38+
return $this;
39+
}
40+
41+
throw new \Exception("Maximum Candidate List Size is not valid. Should be between 0-50.");
42+
}
43+
44+
public function getMaximumCandidateListSize(): string
45+
{
46+
return $this->maximumCandidateListSize;
47+
}
48+
49+
public function toArray(): array
50+
{
51+
$address = [];
52+
53+
if (isset($this->regionalRequestIndicator)) {
54+
$address["regionalrequestindicator"] = $this->regionalRequestIndicator;
55+
}
56+
57+
if (isset($this->maximumCandidateListSize)) {
58+
$address["maximumcandidatelistsize"] = $this->maximumCandidateListSize;
59+
}
60+
61+
return $address;
62+
}
63+
}

src/Ship.php

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,31 +3,31 @@
33
namespace RahulGodiyal\PhpUpsApiWrapper;
44

55
use RahulGodiyal\PhpUpsApiWrapper\Auth;
6-
use RahulGodiyal\PhpUpsApiWrapper\Entity\Query;
6+
use RahulGodiyal\PhpUpsApiWrapper\Entity\ShipQuery;
77
use RahulGodiyal\PhpUpsApiWrapper\Entity\ShipmentRequest;
88
use RahulGodiyal\PhpUpsApiWrapper\Utils\HttpClient;
99

1010
class Ship extends Auth
1111
{
1212
private const VERSION = "v2403";
1313

14-
private Query $query;
14+
private ShipQuery $query;
1515
private ShipmentRequest $shipmentRequest;
1616
private bool $onlyLabel;
1717

1818
public function __construct()
1919
{
20-
$this->query = new Query();
20+
$this->query = new ShipQuery();
2121
$this->onlyLabel = false;
2222
}
2323

24-
public function setQuery(Query $query): self
24+
public function setQuery(ShipQuery $query): self
2525
{
2626
$this->query = $query;
2727
return $this;
2828
}
2929

30-
public function getQuery(): Query
30+
public function getQuery(): ShipQuery
3131
{
3232
return $this->query;
3333
}
@@ -53,6 +53,11 @@ public function createShipment(string $client_id, string $client_secret): array
5353

5454
$access_token = $auth['access_token'];
5555

56+
$queryParams = "";
57+
if ($this->query->exists()) {
58+
$queryParams = "?" . http_build_query($this->query->toArray());
59+
}
60+
5661
$httpClient = new HttpClient();
5762
$httpClient->setHeader([
5863
"Authorization: Bearer $access_token",
@@ -61,7 +66,7 @@ public function createShipment(string $client_id, string $client_secret): array
6166
"transactionSrc: testing"
6267
]);
6368
$httpClient->setPayload(json_encode($this->getPayload()));
64-
$httpClient->setUrl($this->_getAPIBaseURL() . "/api/shipments/" . self::VERSION . "/ship?" . http_build_query($this->query->toArray()));
69+
$httpClient->setUrl($this->_getAPIBaseURL() . "/api/shipments/" . self::VERSION . "/ship" . $queryParams);
6570
$httpClient->setMethod("POST");
6671
$res = $httpClient->fetch();
6772

src/Tracking.php

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,29 @@
33
namespace RahulGodiyal\PhpUpsApiWrapper;
44

55
use RahulGodiyal\PhpUpsApiWrapper\Auth;
6+
use RahulGodiyal\PhpUpsApiWrapper\Entity\TrackingQuery;
67
use RahulGodiyal\PhpUpsApiWrapper\Utils\HttpClient;
78

89
class Tracking extends Auth
910
{
10-
private const QUERY = [
11-
"locale" => "en_US",
12-
"returnSignature" => "false",
13-
"returnMilestones" => "false",
14-
"returnPOD" => "false"
15-
];
16-
private static ?string $trackingNumber;
17-
18-
public static function setTrackingNumber(string $trackingNumber): self
11+
private TrackingQuery $query;
12+
private ?string $trackingNumber;
13+
14+
public function __construct()
15+
{
16+
$this->query = new TrackingQuery();
17+
}
18+
19+
public function setQuery(TrackingQuery $query): self
20+
{
21+
$this->query = $query;
22+
return $this;
23+
}
24+
25+
public function setTrackingNumber(string $trackingNumber): self
1926
{
20-
self::$trackingNumber = $trackingNumber;
21-
return new self;
27+
$this->trackingNumber = $trackingNumber;
28+
return $this;
2229
}
2330

2431
public function fetch(string $client_id, string $client_secret): array
@@ -31,13 +38,18 @@ public function fetch(string $client_id, string $client_secret): array
3138

3239
$access_token = $auth['access_token'];
3340

41+
$queryParams = "";
42+
if ($this->query->exists()) {
43+
$queryParams = "?" . http_build_query($this->query->toArray());
44+
}
45+
3446
$httpClient = new HttpClient();
3547
$httpClient->setHeader([
3648
"Authorization: Bearer $access_token",
3749
"transId: string",
3850
"transactionSrc: testing"
3951
]);
40-
$httpClient->setUrl($this->_getAPIBaseURL() . "/api/track/v1/details/" . self::$trackingNumber . "?" . http_build_query(self::QUERY));
52+
$httpClient->setUrl($this->_getAPIBaseURL() . "/api/track/v1/details/" . $this->trackingNumber . $queryParams);
4153
$httpClient->setMethod("GET");
4254
$res = $httpClient->fetch();
4355

@@ -64,7 +76,7 @@ public function fetch(string $client_id, string $client_secret): array
6476
$trackingDetails = $res->trackResponse->shipment;
6577
return ['status' => 'success', 'data' => $trackingDetails];
6678
}
67-
79+
6880
public function setMode(string $mode): self
6981
{
7082
parent::setMode($mode);

src/Utils/HttpClient.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,4 @@ public function fetch(): object
5858
throw new \Exception("Curl request Failed. Reason: ". $e->getMessage());
5959
}
6060
}
61-
}
61+
}

0 commit comments

Comments
 (0)