Skip to content

Commit

Permalink
Merge pull request #1 from FarhadShirmardi/version2
Browse files Browse the repository at this point in the history
change response shapes.
  • Loading branch information
FarhadShirmardi authored Jun 30, 2024
2 parents 045ad98 + 5bb6a81 commit d128553
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 58 deletions.
7 changes: 5 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
}
],
"require": {
"php": "^7.2 || ^7.3 || ^7.4 || ^8.0",
"laravel/framework": ">=5.6"
"php": "^8.0",
"laravel/framework": ">=11"
},
"autoload": {
"psr-4": {
Expand All @@ -19,5 +19,8 @@
"src/stringHelpers.php",
"src/numberHelpers.php"
]
},
"require-dev": {
"laravel/pint": "^1.16"
}
}
5 changes: 3 additions & 2 deletions src/numberHelpers.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
<?php

if (!function_exists('roundDown')) {
if (! function_exists('roundDown')) {
function roundDown($number, $precision)
{
if ($number == 0) {
return 0;
}
$half = 0.5 / 10 ** $precision;

return round($number - $half, $precision);
}
}
}
88 changes: 49 additions & 39 deletions src/responseHelpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,92 +5,102 @@
use Illuminate\Http\Resources\Json\JsonResource;
use Illuminate\Http\Response;
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Support\Arr;
use Illuminate\Validation\Validator;

if (!function_exists('validationError')) {
if (! function_exists('validationError')) {
function validationError(Validator $validator): JsonResponse
{
return response()->json(
[
'error' => $validator->errors(),
'message' => $validator->errors()->all(),
'message' => Arr::join($validator->errors()->all(), "\n"),
],
Response::HTTP_UNPROCESSABLE_ENTITY
);
}
}

if (!function_exists('apiResponse')) {
if (! function_exists('apiResponse')) {
function apiResponse(
?string $message = null,
int $status = Response::HTTP_OK,
$data = null,
$jsonResourceClassName = null,
array $metaData = [],
$resource = null,
array $meta = [],
array $validation = []
): JsonResponse
{
$metaData += [
'message' => $message == null ? [] : [$message],
];
if ($data !== null and $jsonResourceClassName !== null) {
): JsonResponse {
$additional = Arr::where([
'message' => $message,
'meta' => $meta,
'validation' => $validation,
], fn ($value) => filled($value));

if ($data !== null and $resource !== null) {
if ($data instanceof Collection or
$data instanceof \Illuminate\Support\Collection or
$data instanceof LengthAwarePaginator
) {
/** @var JsonResource $jsonResource */
$jsonResource = ($jsonResourceClassName)::collection($data);
$jsonResource = ($resource)::collection($data);
} else {
$jsonResource = new $jsonResourceClassName($data);
$jsonResource = new $resource($data);
}
$jsonResource->additional($metaData + ['validation' => $validation]);
return $jsonResource->response()->setStatusCode($status);
}
if ($validation != []) {
$metaData += ['validation' => $validation];

return $jsonResource
->additional($additional)
->response()
->setStatusCode($status);
}
$metaData += ['data' => $data];
$additional += ['data' => $data];

return response()->json($metaData, $status);
return response()->json($additional, $status);
}
}

if (!function_exists('responseOK')) {
if (! function_exists('responseOK')) {
function responseOK(
?string $message = null,
$data = null,
?string $jsonResourceClassName = null,
array $metaData = [],
?string $resource = null,
array $meta = [],
array $validation = []
): JsonResponse
{
return apiResponse($message, Response::HTTP_OK, $data, $jsonResourceClassName, $metaData, $validation);
): JsonResponse {
return apiResponse(
$message,
Response::HTTP_OK,
$data,
$resource,
$meta,
$validation
);
}
}

if (!function_exists('responseError')) {
if (! function_exists('responseError')) {
function responseError(
?string $message = null,
$data = null,
?string $jsonResourceClassName = null,
array $metaData = []
): JsonResponse
{
return apiResponse($message, Response::HTTP_UNPROCESSABLE_ENTITY, $data, $jsonResourceClassName, $metaData);
?string $resource = null,
array $meta = []
): JsonResponse {
return apiResponse(
$message,
Response::HTTP_UNPROCESSABLE_ENTITY,
$data,
$resource,
$meta
);
}
}

if (!function_exists('responseNotFound')) {
if (! function_exists('responseNotFound')) {
function responseNotFound(
?string $message = null
): JsonResponse
{
): JsonResponse {
return apiResponse(
$message ?: trans('message.not_found'),
Response::HTTP_NOT_FOUND,
null,
null,
[]
Response::HTTP_NOT_FOUND
);
}
}
38 changes: 23 additions & 15 deletions src/stringHelpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,24 @@ function persianString($string): string
$num = range(0, 9);
$arabicNumbers = ['۰', '١', '٢', '٣', '٤', '٥', '٦', '٧', '٨', '٩'];
$string = str_replace($arabicNumbers, $persian, $string);

return str_replace($num, $persian, $string);
}
}

if (!function_exists('englishString')) {
if (! function_exists('englishString')) {
function englishString($string): string
{
$arabicNumbers = ['۰', '١', '٢', '٣', '٤', '٥', '٦', '٧', '٨', '٩'];
$persianNumbers = ['۰', '۱', '۲', '۳', '۴', '۵', '۶', '۷', '۸', '۹'];
$num = range(0, 9);
$string = str_replace($persianNumbers, $num, $string);

return str_replace($arabicNumbers, $num, $string);
}
}

if (!function_exists('normalizeText')) {
if (! function_exists('normalizeText')) {
function normalizeText(string $text): string
{
$text = str_replace('ي', 'ی', $text);
Expand All @@ -36,49 +38,53 @@ function normalizeText(string $text): string
}
}

if (!function_exists('formatPhoneNumber')) {
if (! function_exists('formatPhoneNumber')) {
function formatPhoneNumber(string $phoneNumber): string
{
if (Str::startsWith($phoneNumber, '0')) {
$phoneNumber = (string)(int)$phoneNumber;
$phoneNumber = (string) (int) $phoneNumber;
} elseif (Str::startsWith($phoneNumber, '+98') and strlen($phoneNumber) != 10) {
$phoneNumber = substr($phoneNumber, 3);
} elseif (Str::startsWith($phoneNumber, '98') and strlen($phoneNumber) != 10) {
$phoneNumber = substr($phoneNumber, 2);
}

return $phoneNumber;
}
}

if (!function_exists('reformatPhoneNumber')) {
if (! function_exists('reformatPhoneNumber')) {
function reformatPhoneNumber(string $phoneNumber): string
{
if (!Str::startsWith($phoneNumber, '0')) {
$phoneNumber = '0' . $phoneNumber;
if (! Str::startsWith($phoneNumber, '0')) {
$phoneNumber = '0'.$phoneNumber;
}

return $phoneNumber;
}
}

if (!function_exists('formatPrice')) {
if (! function_exists('formatPrice')) {
function formatPrice($number): string
{
if ($number < 0) {
return '-' . number_format($number * -1);
return '-'.number_format($number * -1);
}

return number_format($number);
}
}

if (!function_exists('reformatPrice')) {
if (! function_exists('reformatPrice')) {
function reformatPrice($number): int
{
$number = str_replace(',', '', $number);

return intval($number);
}
}

if (!function_exists('convertNumberToText')) {
if (! function_exists('convertNumberToText')) {
function getNumberTextPostfix($level): string
{
return match ($level) {
Expand Down Expand Up @@ -155,6 +161,7 @@ function getDigitText($num, $index): string
return '';
}
}

return '';
}

Expand All @@ -168,8 +175,8 @@ function convertNumberToText(int $num): string
$n = $num % 10;
if ($n != 0 or $nn != 0) {
$text =
getDigitText($nn < 20 ? $nn : $n, $index) . (($index == 1 or $text == '') ? '' :
' و ') . $text;
getDigitText($nn < 20 ? $nn : $n, $index).(($index == 1 or $text == '') ? '' :
' و ').$text;
}
$index += $nn < 20 ? 2 : 1;
$num = intval($num / ($nn < 20 ? 100 : 10));
Expand All @@ -180,13 +187,14 @@ function convertNumberToText(int $num): string
$numText = convertNumberToText($n);
$text =
($numText == '' ? '' :
($numText . ' ' . getNumberTextPostfix($index))) . (($numText == '' or $index == 1 or $text == '') ?
($numText.' '.getNumberTextPostfix($index))).(($numText == '' or $index == 1 or $text == '') ?
'' :
' و ') . $text;
' و ').$text;
$index++;
$num = intval($num / 1000);
}
}

return trim($text);
}
}

0 comments on commit d128553

Please sign in to comment.