aolbrich/request-response

v1.0.6 2023-04-02 16:49 UTC

This package is not auto-updated.

Last update: 2024-06-24 20:50:57 UTC


README

Request, Response classes with request validator (With Dependency Injection)

Request

You can create request or response classes individually, or both of them with the following helper:

Note, if you initiate individually, you have to use the dependency injection to initiate the class(es)

use Aolbrich\PhpDiContainer\Container;
use Aolbrich\RequestResponse\Http\Request\Request;
use Aolbrich\RequestResponse\Http\Response\ResponseInterface;

$container =  new Container();

// Initiate
$request = $container->get(Request::class);
$response = $container->get(Response::class);

// Initiate as singleton
$request = $container->singleton(Request::class);
$response = $container->singleton(Response::class);

Create both classes with helper class: (it will be initated as sigleton)

use Aolbrich\RequestResponse\RequestResponse;

[$request, $response] = RequestResponse::initiate();

Create both calsses as non singleton

use Aolbrich\RequestResponse\RequestResponse;

[$request, $response] = RequestResponse::initiate(false);

Use your existing DI container:

use Aolbrich\PhpDiContainer\Container;
use Aolbrich\RequestResponse\RequestResponse;

$container =  new Container();

// set first parameter to false if the classes should not be singleton
[$request, $response] = RequestResponse::initiate(true, $container);

Note

Singleton here means that if you re-create the class with the DI container like $container->get(Request::class); then it will return with the same request or response object.

Functions

$request->getUri(); // Returns the URI
$request->getMethod(); // Returns the method like GET, POST, DELETE...
$request->body(); // Returns the POST request body
$request->jsonBody(); // If the body is json, it will return the array representation of the json, otherwise null
$request->params(); // Return the GET and POST parameters sanitized for displaying, storing

Request validator

It is possible to validate the input with the build in validator: Exampe:

$validated = $request->validate(
    [
        'email' => 'required',
        'age' => 'required|min:18|max:65',
        'accountNumber' => 'regex:/^[0-9]+$/',
    ]
);

print_r($validated); // The fields validated,
print_r($request->validationErrors()); // The fields errored with the error description

Example validation errors:

Array
(
    [par1] => Required
    [par2] => The value should be more or equal then 5
    [par11] => Date format is incorrect
)

Custom Closure validation:

If the closure returns null then it will be valid, otherwise return an error message.

$validated = $request->validate(
    [
        'email' => 'required',
        'age' => 'required|min:18|max:65',
        'account' => function (mixed $accountId) {
            $accountExist = $this->accountExists($accountId)
            if ($accountExist) {
                return "Account {$accountId} already exists";    
            }

            return null
        },
    ]
);

Add fixed custom closure validator

// Must preceed the validation
$request->setRule('customRule', function (mixed $value) {
    return "If {$value} does not validate, then return error message, if validate return null";
});

$validated = $request->validate(
    [
        'accountNumber' => 'customRule',
    ]
);

Create your own validation class:

1. Create a new class following inheriting the class

<?php

declare(strict_types=1);

namespace <your namespace>;

use Aolbrich\RequestResponse\Http\Request\Validator\ValidationRuleInterface;
use Aolbrich\RequestResponse\Http\Request\Validator\Rules\ValidationRuleBase;

class YourValidationRule extends ValidationRuleBase implements ValidationRuleInterface
{
    public function applyRule(mixed $value, string $validationParam = ''): bool
    {
        return (bool) strtotime($value);
    }

    public function message(): string
    {
        return 'Date format is incorrect';
    }
}

2. Add your new class to validators

$request->setRule('customRule', <your namespace>\YourValidationRule::class);

Response

The response object renders a text (or using other class json) with HTTP headers and status code

Functions

Set response code

$response->setResponseCode(404);

Get response code

$response->getResponseCode();

Set headers

$response->setHeader('Content-type', "application/json");

Get the headers

$response->headers();

Set text (body)

$response->setBody('Body text');

Get body test

$response->getBody();

Render the response

$response->render();

Example: render 404

$response->setResponseCode(404);
$response->setBody('404 Not Found');
$response->render();

Example Render JSON:

$response->setHeader('Content-type', "application/json");
$response->setBody(json_encode(['message' => 'Hello World']));
$response->render();

## JSON Response
### You can alternatively render JSON responses

Example:

use Aolbrich\RequestResponse\Http\Response\JsonResponse;

$jsonResponse = new JsonResponse();
$jsonResponse->arrayToJson(['message' => 'Hello World']);
$jsonResponse->render();

The JsonResponse class:

The class has additional functions next to the base response class:

arrayToJson(<array>); // This will be returned as a json when calling render
getBodyAsArray(); // This will return an array or null, what is previously set
mergeToJson(<array>) Merges the existing content with a new array

What comes

  • CORRS, Header support
  • Further validators