PHP code example of emyoutis / whitehouse-responder

1. Go to this page and download the library: Download emyoutis/whitehouse-responder 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/ */

    

emyoutis / whitehouse-responder example snippets


use Emyoutis\WhiteHouseResponder\ErrorsRepository;
use Emyoutis\WhiteHouseResponder\Response;

$errorsRepository = new ErrorsRepository();
$response = new Response($errorsRepository);

$errorsRepository->register(
     '40001',
     'Verbose, plain language description of the problem. Provide developers suggestions about how to solve their problems here',
     'This is a message that can be passed along to end-users, if needed.',
     'http://www.example.gov/developer/path/to/help/for/444444'
);

$errorsRepository->unregister('40001');

    $response->clientError('40001');
    

    $response->serverError('40001');
    

$response->error('40001', 422);

$errorsRepository->register(
     '40001',
     'The class `:class` is undefined.',
     'An error has been occurred in while finding the :entity.',
     'http://www.example.gov/developer/path/to/help/for/444444'
);

return $response->clientError(40001, [
     'class'  => 'Entities/User',
     'entity' => 'user',
])

$errorsRepository->disableExceptions();

$results  = [
     [
          'id'    => 1,
          'title' => 'First Item',
     ],
     [
          'id'    => 2,
          'title' => 'Second Item',
     ],
];
$metadata = ['page' => 1];
    
$response = $response->success($results, $metadata);

$errorsRepository->register(
     '40001',
     'Verbose, plain language description of the problem. Provide developers suggestions about how to solve their problems here',
     'This is a message that can be passed along to end-users, if needed.',
     'http://www.example.gov/developer/path/to/help/for/444444'
);

$response->registerFormatter(function ($key) {
    return snake_case($key);
});


return $response->clientError(40001);