Skip to content

Commit ca726a0

Browse files
committed
add data validation
1 parent 1bcc7e2 commit ca726a0

File tree

3 files changed

+59
-1
lines changed

3 files changed

+59
-1
lines changed

src/Apis/BaseApi.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use GuzzleHttp\Client;
77
use GuzzleHttp\Exception\GuzzleException;
88
use GuzzleHttp\Exception\ClientException;
9+
use Codeboxr\PaperflyCourier\Exceptions\PaperflyValidationException;
910

1011
class BaseApi
1112
{
@@ -103,5 +104,37 @@ public function send($method, $uri, $body = [])
103104
throw new PaperflyException($message, $e->getCode(), $errors);
104105
}
105106
}
107+
108+
109+
/**
110+
* Ecourier validation
111+
*
112+
* @param array $data
113+
* @param array $requiredFields
114+
*
115+
* @throws PaperflyValidationException
116+
*/
117+
public function validation($data, $requiredFields)
118+
{
119+
if (!is_array($data) || !is_array($requiredFields)) {
120+
throw new \TypeError("Argument must be of the type array", 500);
121+
}
122+
123+
if (!count($data) || !count($requiredFields)) {
124+
throw new PaperflyValidationException("Invalid data!", 422);
125+
}
126+
127+
$requiredColumns = array_diff($requiredFields, array_keys($data));
128+
if (count($requiredColumns)) {
129+
throw new PaperflyValidationException($requiredColumns, 422);
130+
}
131+
132+
foreach ($requiredFields as $filed) {
133+
if (isset($data[$filed]) && empty($data[$filed])) {
134+
throw new PaperflyValidationException("$filed is required", 422);
135+
}
136+
}
137+
138+
}
106139

107140
}

src/Apis/OrderApi.php

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

55
use Codeboxr\PaperflyCourier\Exceptions\PaperflyException;
66
use GuzzleHttp\Exception\GuzzleException;
7+
use Codeboxr\PaperflyCourier\Exceptions\PaperflyValidationException;
78

89
class OrderApi extends BaseApi
910
{
1011
/**
1112
* New Order Create
1213
*
1314
* @param $array
15+
*
1416
* @return mixed
1517
* @throws PaperflyException
16-
* @throws GuzzleException
18+
* @throws GuzzleException|PaperflyValidationException
1719
*/
1820
public function create($array)
1921
{
22+
$this->validation($array, ["pickMerchantThana", "productSizeWeight", "max_weight", "deliveryOption", "customerThana", "customerDistrict", "custPhone"]);
23+
2024
$response = $this->authorization()->send("POST", "OrderPlacement", $array);
2125
return $response->success;
2226
}
@@ -25,6 +29,7 @@ public function create($array)
2529
* Order Tracking
2630
*
2731
* @param $referenceNumber
32+
*
2833
* @return mixed
2934
* @throws GuzzleException
3035
* @throws PaperflyException
@@ -42,6 +47,7 @@ public function tracking($referenceNumber)
4247
* invoice details
4348
*
4449
* @param $referenceNumber
50+
*
4551
* @return mixed
4652
* @throws GuzzleException
4753
* @throws PaperflyException
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace Codeboxr\PaperflyCourier\Exceptions;
4+
5+
use Throwable;
6+
use Exception;
7+
8+
class PaperflyValidationException 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)