Skip to content

Instantly share code, notes, and snippets.

@jsdecena
Created February 15, 2021 05:34
Show Gist options
  • Save jsdecena/edab823b53516c27079829b3ace026bb to your computer and use it in GitHub Desktop.
Save jsdecena/edab823b53516c27079829b3ace026bb to your computer and use it in GitHub Desktop.
Laravel Rule
<?php
// ================================ Current METHOD ==================================== //
/**
* Balance inquiry
*
* @param Request $request
* @return array|JsonResponse
* @throws Exception
*/
public function inquireBalance(Request $request)
{
$this->validate($request, [
'mobile' => ['required', 'regex:' . RegexFormat::phMobileNumber()]
]);
Log::info('Retailer Inquire Balance Payload', $request->only('mobile'));
if ($request->has('retailer')) {
$loggedUserMobileNumber = '0' . substr($request->retailer->retailer_min, 3);
$mobileNumber = Mobile::formatMobile($request->input('mobile'));
if ($loggedUserMobileNumber !== $mobileNumber) {
return response()->json(trans('errors.notAllowed'), 401);
}
}
try {
$response = $this->prepaidSvc->inquireBalance($request->input('mobile'));
Log::info('Retailer Inquire Balance Response', $response);
return $response;
} catch (GeneralException $e) {
return response()->json(trans('errors.alternate_500'), 500);
}
}
//================================= Using Laravel Rule
/**
* Balance inquiry
*
* @param Request $request
* @return array|JsonResponse
* @throws Exception
*/
public function inquireBalance(InquireBalanceRequest $request)
{
try {
$response = $this->prepaidSvc->inquireBalance($request->input('mobile'));
Log::info('Retailer Inquire Balance Response', $response);
return $response;
} catch (GeneralException $e) {
return response()->json(trans('errors.alternate_500'), 500);
}
}
//======= InquireBalanceRequest
namespace Smart\Mobile\App\Http\Requests\RetailerLoad;
use Smart\Mobile\App\Helpers\RegexFormat;
use Smart\Mobile\App\Http\Requests\BaseRequest;
class InquireBalanceRequest extends BaseRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'mobile' => ['required', 'regex:' . RegexFormat::phMobileNumber()],
'retailer' => [new InquireBalanceRule($this->retailer->retailer_min)] // Add here
];
}
....
}
namespace Smart\Mobile\App\Http\Requests\RetailerLoad\Rules;
use Illuminate\Contracts\Validation\Rule;
use Smart\Mobile\App\Helpers\Mobile;
/**
* The logged-in user must be inquiring only their own balance
*
* Class InquireBalanceRule
* @package Smart\Mobile\App\Http\Requests\RetailerLoad\Rules
*/
class InquireBalanceRule implements Rule
{
/** @var string $loggedUserMin */
public $loggedUserMin;
/**
* InquireBalanceRule constructor.
* @param string $loggedUserMin
*/
public function __construct(string $loggedUserMin)
{
$this->loggedUserMin = Mobile::formatMobile($loggedUserMin);
}
/**
* @param string $attribute
* @param mixed $mobile
* @return bool
*/
public function passes($attribute, $mobile): bool
{
return $this->loggedUserMin === Mobile::formatMobile($mobile);
}
/**
* @return string
*/
public function message(): string
{
return trans('errors.notAllowed');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment