PHP code example of uthayakumar-dinesh / statusify
1. Go to this page and download the library: Download uthayakumar-dinesh/statusify library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
uthayakumar-dinesh / statusify example snippets
use UthayakumarDinesh\Statusify\Statusify;
// Using class constants
echo Statusify::OK; // 200
echo Statusify::NOT_FOUND; // 404
echo Statusify::INTERNAL_SERVER_ERROR; // 500
// Reverse lookup - get status name from code
echo Statusify::getStatusName(200); // "OK"
echo Statusify::getStatusName(404); // "NOT_FOUND"
echo Statusify::getStatusName(500); // "INTERNAL_SERVER_ERROR"
echo Statusify::getStatusName(999); // "UNKNOWN_STATUS"
namespace App\Http\Controllers;
use Illuminate\Http\JsonResponse;
use UthayakumarDinesh\Statusify\Statusify;
class UserController extends Controller
{
public function show($id): JsonResponse
{
$user = User::find($id);
if (!$user) {
return response()->json([
'error' => 'User not found',
'status_code' => Statusify::NOT_FOUND,
'status_name' => Statusify::getStatusName(Statusify::NOT_FOUND)
], Statusify::NOT_FOUND);
}
return response()->json($user, Statusify::OK);
}
public function store(Request $request): JsonResponse
{
$user = User::create($request->validated());
return response()->json([
'data' => $user,
'status_code' => Statusify::CREATED,
'status_name' => Statusify::getStatusName(Statusify::CREATED)
], Statusify::CREATED);
}
}
namespace App\Controller;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Routing\Annotation\Route;
use UthayakumarDinesh\Statusify\Statusify;
class ApiController
{
#[Route('/api/users/{id}', methods: ['GET'])]
public function getUser(int $id): JsonResponse
{
$user = $this->userRepository->find($id);
if (!$user) {
return new JsonResponse([
'error' => 'User not found',
'status_code' => Statusify::NOT_FOUND,
'status_name' => Statusify::getStatusName(Statusify::NOT_FOUND)
], Statusify::NOT_FOUND);
}
return new JsonResponse($user, Statusify::OK);
}
}
namespace App\Exceptions;
use UthayakumarDinesh\Statusify\Statusify;
use Exception;
class ApiException extends Exception
{
private int $statusCode;
public function __construct(string $message, int $statusCode = null)
{
$this->statusCode = $statusCode ?? Statusify::INTERNAL_SERVER_ERROR;
parent::__construct($message);
}
public function getStatusCode(): int
{
return $this->statusCode;
}
public function getStatusName(): string
{
return Statusify::getStatusName($this->statusCode);
}
public function toArray(): array
{
return [
'error' => $this->getMessage(),
'status_code' => $this->getStatusCode(),
'status_name' => $this->getStatusName()
];
}
}
// Usage
throw new ApiException('User not found', Statusify::NOT_FOUND);
throw new ApiException('Invalid request data', Statusify::BAD_REQUEST);
class Statusify
{
// 1xx Informational
public const CONTINUE = 100;
public const SWITCHING_PROTOCOLS = 101;
// 2xx Success
public const OK = 200;
public const CREATED = 201;
// ... and more
}
UthayakumarDinesh\Statusify\Statusify;
// Test with a known status code
$code = 200;
echo "Code: $code\n";
echo "Name: " . Statusify::getStatusName($code) . "\n";
// Test with an unknown status code
$unknownCode = 999;
echo "Code: $unknownCode\n";
echo "Name: " . Statusify::getStatusName($unknownCode) . "\n";