Skip to content

Instantly share code, notes, and snippets.

@chrisguitarguy
Last active August 23, 2019 20:42
Show Gist options
  • Select an option

  • Save chrisguitarguy/88cf096adadb470aae066fcca2d17d6f to your computer and use it in GitHub Desktop.

Select an option

Save chrisguitarguy/88cf096adadb470aae066fcca2d17d6f to your computer and use it in GitHub Desktop.

Revisions

  1. chrisguitarguy revised this gist Aug 23, 2019. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions controller_3.php
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,4 @@
    <?php
    use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
    use Symfony\Component\HttpFoundation\Request;
    use Symfony\Component\HttpFoundation\Response;
  2. chrisguitarguy revised this gist Aug 23, 2019. 2 changed files with 2 additions and 2 deletions.
    2 changes: 1 addition & 1 deletion client_dto_2.php
    Original file line number Diff line number Diff line change
    @@ -2,7 +2,7 @@

    use Symfony\Component\Serializer\Annotation\Groups;

    class ClientDto
    class ClientDto extends Dto
    {
    /**
    * @Groups(Dto::GROUP_DEFAULT)
    2 changes: 1 addition & 1 deletion client_dto_3.php
    Original file line number Diff line number Diff line change
    @@ -2,7 +2,7 @@
    use Symfony\Component\Serializer\Annotation\Groups;
    use Symfony\Component\Validator\Constraints as Assert;

    class ClientDto
    class ClientDto extends Dto
    {
    /**
    * @Groups(Dto::GROUP_DEFAULT)
  3. chrisguitarguy revised this gist Aug 23, 2019. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion controller_3.php
    Original file line number Diff line number Diff line change
    @@ -27,7 +27,7 @@ public function createClientAction(Request $request) : Response

    $client = $this->createAndStoreClientFromDto($dto);

    return $this->json($client, Response::HTTP_CREATED, [
    return $this->json(ClientDto::fromClient($client), Response::HTTP_CREATED, [
    'Location' => $this->generateUrl('clients.view', [
    'clientId' => $client->getId(),
    ]),
  4. chrisguitarguy revised this gist Aug 23, 2019. 1 changed file with 38 additions and 0 deletions.
    38 changes: 38 additions & 0 deletions controller_3.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,38 @@
    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($client, Response::HTTP_CREATED, [
    'Location' => $this->generateUrl('clients.view', [
    'clientId' => $client->getId(),
    ]),
    ], [
    'groups' => Dto::GROUP_DEFAULT,
    ]);
    }
    }
  5. chrisguitarguy revised this gist Aug 23, 2019. 1 changed file with 29 additions and 0 deletions.
    29 changes: 29 additions & 0 deletions client_dto_3.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,29 @@
    <?php
    use Symfony\Component\Serializer\Annotation\Groups;
    use Symfony\Component\Validator\Constraints as Assert;

    class ClientDto
    {
    /**
    * @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;
    }
  6. chrisguitarguy revised this gist Aug 23, 2019. 1 changed file with 16 additions and 0 deletions.
    16 changes: 16 additions & 0 deletions controller_2.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,16 @@
    <?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,
    ]);
    }
    }
  7. chrisguitarguy revised this gist Aug 23, 2019. 1 changed file with 21 additions and 0 deletions.
    21 changes: 21 additions & 0 deletions client_dto_2.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,21 @@
    <?php

    use Symfony\Component\Serializer\Annotation\Groups;

    class ClientDto
    {
    /**
    * @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;
    }
  8. chrisguitarguy revised this gist Aug 23, 2019. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions dto.php
    Original file line number Diff line number Diff line change
    @@ -2,7 +2,7 @@

    abstract class Dto
    {
    const GROUP_DEFAULT = 'default';
    const GROUP_CREATE = 'create';
    const GROUP_UPDATE = 'update';
    public const GROUP_DEFAULT = 'default';
    public const GROUP_CREATE = 'create';
    public const GROUP_UPDATE = 'update';
    }
  9. chrisguitarguy revised this gist Aug 23, 2019. 1 changed file with 8 additions and 0 deletions.
    8 changes: 8 additions & 0 deletions dto.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,8 @@
    <?php

    abstract class Dto
    {
    const GROUP_DEFAULT = 'default';
    const GROUP_CREATE = 'create';
    const GROUP_UPDATE = 'update';
    }
  10. chrisguitarguy revised this gist Aug 23, 2019. 1 changed file with 13 additions and 0 deletions.
    13 changes: 13 additions & 0 deletions controller.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,13 @@
    <?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));
    }
    }
  11. chrisguitarguy created this gist Aug 23, 2019.
    9 changes: 9 additions & 0 deletions client_dto_1.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,9 @@
    <?php

    class ClientDto
    {
    public $id;
    public $name;
    public $site_url;
    }

    10 changes: 10 additions & 0 deletions client_entity.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,10 @@
    <?php

    class Client
    {
    private $id;
    private $name;
    private $siteUrl;

    // constructor, getters, etc
    }