diff --git a/composer.json b/composer.json index d865c89..beed943 100644 --- a/composer.json +++ b/composer.json @@ -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": { @@ -19,5 +19,8 @@ "src/stringHelpers.php", "src/numberHelpers.php" ] + }, + "require-dev": { + "laravel/pint": "^1.16" } } diff --git a/src/numberHelpers.php b/src/numberHelpers.php index 7c89b38..ff6968c 100644 --- a/src/numberHelpers.php +++ b/src/numberHelpers.php @@ -1,12 +1,13 @@ 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 ); } } diff --git a/src/stringHelpers.php b/src/stringHelpers.php index c37fb13..c544029 100644 --- a/src/stringHelpers.php +++ b/src/stringHelpers.php @@ -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); @@ -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) { @@ -155,6 +161,7 @@ function getDigitText($num, $index): string return ''; } } + return ''; } @@ -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)); @@ -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); } }