PHP code example of carousel / dto
1. Go to this page and download the library: Download carousel/dto 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/ */
carousel / dto example snippets
use Carousel\DTO\DTOClass;
//mock request data (array)
$request = [
'myUsername' => 'John Scofield',
'my_timezone' => 'UTC+1'
];
//inject DTO in controller or middleware
public function __construct($request)
{
$this->dto = new DTOClass($request);
}
//camelize input key
$camelized = $this->dto->camelize('my_timezone');
//camelize all input keys
$camelized = $this->dto->camelize();
//exclude data from input
$except = $this->dto->except(['myUsername']);
//get only subset of data from input
$only = $this->dto->only(['myUsername']);
//serialize input
$serialized = $this->dto->serialize();
//decamelize all request keys in DTO object
$request = $dto->decamelize();
$dto->request = $request;
//return one decamelized key
return json_encode($dto->my_username);