PHP code example of thiagocordeiro / serializer-bundle
1. Go to this page and download the library: Download thiagocordeiro/serializer-bundle 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/ */
thiagocordeiro / serializer-bundle example snippets
return [
Symfony\Bundle\FrameworkBundle\FrameworkBundle::class => ['all' => true],
...
Serializer\SerializerBundle\SerializerBundle::class => ['all' => true],
];
namespace App\Framework\Controller\User;
use App\Domain\User\CreateUserService;
use App\Domain\User\CreateUser;
use App\Domain\User\UserCreated;
use Serializer\Serializer;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\HttpException;
class CreateUserController
{
/** @var CreateUserService */
private $service;
/** @var Serializer */
private $serializer;
public function __construct(CreateUserService $service, Serializer $serializer)
{
$this->service = $service;
$this->serializer = $serializer;
}
public function __invoke(Request $request): UserCreated
{
$createUser = $this->serializer->deserialize((string) $request->getContent(), CreateUser::class);
try {
$userCreated = $this->service->create($createUser);
} catch (UserAlreadyRegistered $e) {
throw new HttpException(Response::HTTP_CONFLICT, 'User Already Registered', $e);
}
return $userCreated;
}
}
...
class CreateUserController
{
/** @var CreateUserService */
private $service;
public function __construct(CreateUserService $service)
{
$this->service = $service;
}
public function __invoke(CreateUser $createUser): UserCreated
{
try {
$userCreated = $this->service->create($createUser);
} catch (UserAlreadyRegistered $e) {
throw new HttpException(Response::HTTP_CONFLICT, 'User Already Registered', $e);
}
return $userCreated;
}
}