Skip to content

Commit e737383

Browse files
committed
add data validation
1 parent 1e57644 commit e737383

File tree

4 files changed

+82
-3
lines changed

4 files changed

+82
-3
lines changed

src/Apis/BaseApi.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use GuzzleHttp\Exception\GuzzleException;
88
use GuzzleHttp\Exception\ClientException;
99
use Codeboxr\PathaoCourier\Exceptions\PathaoException;
10+
use Codeboxr\PathaoCourier\Exceptions\PathaoCourierValidationException;
1011

1112
class BaseApi
1213
{
@@ -159,4 +160,35 @@ public function send($method, $uri, $body = [])
159160
}
160161
}
161162

163+
/**
164+
* Data Validation
165+
*
166+
* @param array $data
167+
* @param array $requiredFields
168+
*
169+
* @throws PathaoCourierValidationException
170+
*/
171+
public function validation($data, $requiredFields)
172+
{
173+
if (!is_array($data) || !is_array($requiredFields)) {
174+
throw new \TypeError("Argument must be of the type array", 500);
175+
}
176+
177+
if (!count($data) || !count($requiredFields)) {
178+
throw new PathaoCourierValidationException("Invalid data!", 422);
179+
}
180+
181+
$requiredColumns = array_diff($requiredFields, array_keys($data));
182+
if (count($requiredColumns)) {
183+
throw new PathaoCourierValidationException($requiredColumns, 422);
184+
}
185+
186+
foreach ($requiredFields as $filed) {
187+
if (isset($data[$filed]) && empty($data[$filed])) {
188+
throw new PathaoCourierValidationException("$filed is required", 422);
189+
}
190+
}
191+
192+
}
193+
162194
}

src/Apis/OrderApi.php

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use GuzzleHttp\Exception\GuzzleException;
66
use Codeboxr\PathaoCourier\Exceptions\PathaoException;
7+
use Codeboxr\PathaoCourier\Exceptions\PathaoCourierValidationException;
78

89
class OrderApi extends BaseApi
910
{
@@ -14,10 +15,23 @@ class OrderApi extends BaseApi
1415
*
1516
* @return mixed
1617
* @throws PathaoException
17-
* @throws GuzzleException
18+
* @throws GuzzleException|PathaoCourierValidationException
1819
*/
1920
public function create($array)
2021
{
22+
$this->validation($array, [
23+
"store_id",
24+
"recipient_name",
25+
"recipient_phone",
26+
"recipient_address",
27+
"recipient_city",
28+
"recipient_zone",
29+
"delivery_type",
30+
"item_type",
31+
"item_quantity",
32+
"amount_to_collect",
33+
]);
34+
2135
$response = $this->authorization()->send("POST", "aladdin/api/v1/orders", $array);
2236
return $response->data;
2337
}
@@ -44,10 +58,19 @@ public function orderDetails($consignmentId)
4458
*
4559
* @return mixed
4660
* @throws GuzzleException
47-
* @throws PathaoException
61+
* @throws PathaoException|PathaoCourierValidationException
4862
*/
4963
public function priceCalculation($array)
5064
{
65+
$this->validation($array, [
66+
"store_id",
67+
"item_type",
68+
"delivery_type",
69+
"item_weight",
70+
"recipient_city",
71+
"recipient_zone"
72+
]);
73+
5174
$response = $this->authorization()->send("POST", "aladdin/api/v1/merchant/price-plan", $array);
5275
return $response->data;
5376
}

src/Apis/StoreApi.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use GuzzleHttp\Exception\GuzzleException;
66
use Codeboxr\PathaoCourier\Exceptions\PathaoException;
7+
use Codeboxr\PathaoCourier\Exceptions\PathaoCourierValidationException;
78

89
class StoreApi extends BaseApi
910
{
@@ -29,10 +30,14 @@ public function list($page = 1)
2930
*
3031
* @return mixed
3132
* @throws GuzzleException
32-
* @throws PathaoException
33+
* @throws PathaoException|PathaoCourierValidationException
3334
*/
3435
public function create($storeInfo)
3536
{
37+
$this->validation($storeInfo, [
38+
"name", "contact_name", "contact_number", "address", "city_id", "zone_id", "area_id"
39+
]);
40+
3641
$response = $this->authorization()->send("POST", "aladdin/api/v1/stores", $storeInfo);
3742
return $response->data;
3843
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace Codeboxr\PathaoCourier\Exceptions;
4+
5+
use Exception;
6+
use Throwable;
7+
8+
class PathaoCourierValidationException extends Exception
9+
{
10+
public function __construct($message = "", $code = 0, Throwable $previous = null)
11+
{
12+
if (is_array($message)) {
13+
$requiredColumnsImplode = implode(",", $message);
14+
parent::__construct("$requiredColumnsImplode filed is required", $code, $previous);
15+
} else {
16+
parent::__construct($message, $code, $previous);
17+
}
18+
}
19+
}

0 commit comments

Comments
 (0)