Last active
August 23, 2019 20:42
-
-
Save chrisguitarguy/88cf096adadb470aae066fcca2d17d6f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class ClientDto | |
{ | |
public $id; | |
public $name; | |
public $site_url; | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Client | |
{ | |
private $id; | |
private $name; | |
private $siteUrl; | |
// constructor, getters, etc | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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)); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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, | |
]); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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, | |
]); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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