Skip to content

Instantly share code, notes, and snippets.

@chrisguitarguy
Last active August 23, 2019 20:42
Show Gist options
  • Save chrisguitarguy/88cf096adadb470aae066fcca2d17d6f to your computer and use it in GitHub Desktop.
Save chrisguitarguy/88cf096adadb470aae066fcca2d17d6f to your computer and use it in GitHub Desktop.
<?php
class ClientDto
{
public $id;
public $name;
public $site_url;
}
<?php
use Symfony\Component\Serializer\Annotation\Groups;
class ClientDto extends Dto
{
/**
* @Groups(Dto::GROUP_DEFAULT)
*/
public $id;
/**
* @Groups({Dto::GROUP_DEFAULT, Dto::GROUP_CREATE})
*/
public $name;
/**
* @Groups({Dto::GROUP_DEFAULT, Dto::GROUP_CREATE, Dto::GROUP_UPDATE})
*/
public $site_url;
}
<?php
use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Validator\Constraints as Assert;
class ClientDto extends Dto
{
/**
* @Groups(Dto::GROUP_DEFAULT)
* @var string
*/
public $id;
/**
* @Groups({Dto::GROUP_DEFAULT, Dto::GROUP_CREATE})
* @Assert\NotBlank(groups={Dto::GROUP_CREATE, Dto::GROUP_UPDATE})
* @Assert\Type(type="string", groups={Dto::GROUP_CREATE, Dto::GROUP_UPDATE})
* @var string
*/
public $name;
/**
* @Groups({Dto::GROUP_DEFAULT, Dto::GROUP_CREATE, Dto::GROUP_UPDATE})
* @Assert\NotBlank(groups={Dto::GROUP_CREATE, Dto::GROUP_UPDATE})
* @Assert\Type(type="string", groups={Dto::GROUP_CREATE, Dto::GROUP_UPDATE})
* @Assert\Url(protocols={"http", "https"}, groups={Dto::GROUP_CREATE, Dto::GROUP_UPDATE})
* @var string
*/
public $site_url;
}
<?php
class Client
{
private $id;
private $name;
private $siteUrl;
// constructor, getters, etc
}
<?php
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
class ClientController extends AbstractController
{
public function viewClientAction(string $clientId) : Response
{
$client = $this->getClientOr404($clientId);
return $this->json(ClientDto::fromClient($client));
}
}
<?php
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
class ClientController extends AbstractController
{
public function viewClientAction(string $clientId) : Response
{
$client = $this->getClientOr404($clientId);
return $this->json(ClientDto::fromClient($client), Response::HTTP_OK, [], [
'groups' => Dto::GROUP_DEFAULT,
]);
}
}
<?php
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
class ClientController extends AbstractController
{
// ...
public function createClientAction(Request $request) : Response
{
// probably want to validate that the request is JSON and such here...
// this will throw a NotEncodableValueException from the serializer component
// if the JSON is invalid. May want to catch and convert to a bad request
// exception.
$dto = $this->get('serializer')->deserialize(
$request->getContent(),
ClientDto::class,
'json',
['groups' => Dto::GROUP_CREATE]
);
$validationErrors = $this->get('validator')->validate($dto, null, [Dto::GROUP_CREATE]);
if (count($validationError) > 0) {
return $this->json($validationErrors, Response::HTTP_BAD_REQUEST);
}
$client = $this->createAndStoreClientFromDto($dto);
return $this->json(ClientDto::fromClient($client), Response::HTTP_CREATED, [
'Location' => $this->generateUrl('clients.view', [
'clientId' => $client->getId(),
]),
], [
'groups' => Dto::GROUP_DEFAULT,
]);
}
}
<?php
abstract class Dto
{
public const GROUP_DEFAULT = 'default';
public const GROUP_CREATE = 'create';
public const GROUP_UPDATE = 'update';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment