DependenciesController.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. declare(strict_types=1);
  3. namespace TestApp\Controller;
  4. use Cake\Controller\Controller;
  5. use Cake\Event\EventManagerInterface;
  6. use Cake\Http\ServerRequest;
  7. use stdClass;
  8. use TestApp\ReflectionDependency;
  9. /**
  10. * DependenciesController class
  11. */
  12. class DependenciesController extends Controller
  13. {
  14. public ?stdClass $inject;
  15. public function __construct(
  16. ?ServerRequest $request = null,
  17. ?string $name = null,
  18. ?EventManagerInterface $eventManager = null,
  19. ?stdClass $inject = null
  20. ) {
  21. parent::__construct($request, $name, $eventManager);
  22. $this->inject = $inject;
  23. }
  24. /**
  25. * @return \Cake\Http\Response
  26. */
  27. public function requiredString(string $str)
  28. {
  29. return $this->response->withStringBody(json_encode(compact('str')));
  30. }
  31. /**
  32. * @return \Cake\Http\Response
  33. */
  34. public function optionalString(string $str = 'default val')
  35. {
  36. return $this->response->withStringBody(json_encode(compact('str')));
  37. }
  38. public function requiredTyped(float $one, int $two, bool $three, array $four)
  39. {
  40. return $this->response->withStringBody(json_encode(
  41. compact('one', 'two', 'three', 'four'),
  42. JSON_PRESERVE_ZERO_FRACTION
  43. ));
  44. }
  45. public function optionalTyped(float $one = 1.0, int $two = 2, bool $three = true)
  46. {
  47. return $this->response->withStringBody(json_encode(compact('one', 'two', 'three'), JSON_PRESERVE_ZERO_FRACTION));
  48. }
  49. public function unsupportedTyped(iterable $one)
  50. {
  51. return $this->response->withStringBody(json_encode(compact('one')));
  52. }
  53. public function typedUnion(string|int $one)
  54. {
  55. return $this->response->withStringBody(json_encode(compact('one')));
  56. }
  57. /**
  58. * @param mixed $any
  59. * @return \Cake\Http\Response
  60. */
  61. public function optionalDep($any = null, ?string $str = null, ?stdClass $dep = null)
  62. {
  63. return $this->response->withStringBody(json_encode(compact('dep', 'any', 'str')));
  64. }
  65. /**
  66. * @param \TestApp\ReflectionDependency $dep
  67. * @return \Cake\Http\Response
  68. */
  69. public function reflectionDep(ReflectionDependency $dep)
  70. {
  71. return $this->response->withStringBody(json_encode(compact('dep')));
  72. }
  73. /**
  74. * @param mixed $any
  75. * @return \Cake\Http\Response
  76. */
  77. public function requiredDep(stdClass $dep, $any = null, ?string $str = null)
  78. {
  79. return $this->response->withStringBody(json_encode(compact('dep', 'any', 'str')));
  80. }
  81. /**
  82. * @return \Cake\Http\Response
  83. */
  84. public function variadic()
  85. {
  86. return $this->response->withStringBody(json_encode(['args' => func_get_args()]));
  87. }
  88. /**
  89. * @return \Cake\Http\Response
  90. */
  91. public function spread(string ...$args)
  92. {
  93. return $this->response->withStringBody(json_encode(['args' => $args]));
  94. }
  95. /**
  96. * @param mixed $one
  97. * @return \Cake\Http\Response
  98. */
  99. public function requiredParam($one)
  100. {
  101. return $this->response->withStringBody(json_encode(compact('one')));
  102. }
  103. }