HelloResource.php 697 B

123456789101112131415161718192021222324252627282930
  1. <?php
  2. namespace Benchmark\Resources;
  3. use Hamlet\Entities\JsonEntity;
  4. use Hamlet\Entities\PlainTextEntity;
  5. use Hamlet\Requests\Request;
  6. use Hamlet\Resources\WebResource;
  7. use Hamlet\Responses\Response;
  8. use Hamlet\Responses\SimpleOKResponse;
  9. class HelloResource implements WebResource
  10. {
  11. private $json;
  12. public function __construct(bool $json)
  13. {
  14. $this->json = $json;
  15. }
  16. public function getResponse(Request $request): Response
  17. {
  18. if ($this->json) {
  19. $entity = new JsonEntity(['message' => 'Hello, World!']);
  20. } else {
  21. $entity = new PlainTextEntity('Hello, World!');
  22. }
  23. return new SimpleOKResponse($entity);
  24. }
  25. }