Skip to content

Commit

Permalink
add data validation
Browse files Browse the repository at this point in the history
  • Loading branch information
dipudey committed Oct 14, 2022
1 parent 1bcc7e2 commit ca726a0
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 1 deletion.
33 changes: 33 additions & 0 deletions src/Apis/BaseApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use GuzzleHttp\Client;
use GuzzleHttp\Exception\GuzzleException;
use GuzzleHttp\Exception\ClientException;
use Codeboxr\PaperflyCourier\Exceptions\PaperflyValidationException;

class BaseApi
{
Expand Down Expand Up @@ -103,5 +104,37 @@ public function send($method, $uri, $body = [])
throw new PaperflyException($message, $e->getCode(), $errors);
}
}


/**
* Ecourier validation
*
* @param array $data
* @param array $requiredFields
*
* @throws PaperflyValidationException
*/
public function validation($data, $requiredFields)
{
if (!is_array($data) || !is_array($requiredFields)) {
throw new \TypeError("Argument must be of the type array", 500);
}

if (!count($data) || !count($requiredFields)) {
throw new PaperflyValidationException("Invalid data!", 422);
}

$requiredColumns = array_diff($requiredFields, array_keys($data));
if (count($requiredColumns)) {
throw new PaperflyValidationException($requiredColumns, 422);
}

foreach ($requiredFields as $filed) {
if (isset($data[$filed]) && empty($data[$filed])) {
throw new PaperflyValidationException("$filed is required", 422);
}
}

}

}
8 changes: 7 additions & 1 deletion src/Apis/OrderApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,23 @@

use Codeboxr\PaperflyCourier\Exceptions\PaperflyException;
use GuzzleHttp\Exception\GuzzleException;
use Codeboxr\PaperflyCourier\Exceptions\PaperflyValidationException;

class OrderApi extends BaseApi
{
/**
* New Order Create
*
* @param $array
*
* @return mixed
* @throws PaperflyException
* @throws GuzzleException
* @throws GuzzleException|PaperflyValidationException
*/
public function create($array)
{
$this->validation($array, ["pickMerchantThana", "productSizeWeight", "max_weight", "deliveryOption", "customerThana", "customerDistrict", "custPhone"]);

$response = $this->authorization()->send("POST", "OrderPlacement", $array);
return $response->success;
}
Expand All @@ -25,6 +29,7 @@ public function create($array)
* Order Tracking
*
* @param $referenceNumber
*
* @return mixed
* @throws GuzzleException
* @throws PaperflyException
Expand All @@ -42,6 +47,7 @@ public function tracking($referenceNumber)
* invoice details
*
* @param $referenceNumber
*
* @return mixed
* @throws GuzzleException
* @throws PaperflyException
Expand Down
19 changes: 19 additions & 0 deletions src/Exceptions/PaperflyValidationException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Codeboxr\PaperflyCourier\Exceptions;

use Throwable;
use Exception;

class PaperflyValidationException extends Exception
{
public function __construct($message = "", $code = 0, Throwable $previous = null)
{
if (is_array($message)) {
$requiredColumnsImplode = implode(",", $message);
parent::__construct("$requiredColumnsImplode filed is required", $code, $previous);
} else {
parent::__construct($message, $code, $previous);
}
}
}

0 comments on commit ca726a0

Please sign in to comment.