ParamHelpers.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. namespace Mpociot\ApiDoc\Extracting;
  3. use Faker\Factory;
  4. use stdClass;
  5. trait ParamHelpers
  6. {
  7. protected function generateDummyValue(string $type)
  8. {
  9. $faker = Factory::create();
  10. if ($this->config->get('faker_seed')) {
  11. $faker->seed($this->config->get('faker_seed'));
  12. }
  13. $fakeFactories = [
  14. 'integer' => function () use ($faker) {
  15. return $faker->numberBetween(1, 20);
  16. },
  17. 'number' => function () use ($faker) {
  18. return $faker->randomFloat();
  19. },
  20. 'float' => function () use ($faker) {
  21. return $faker->randomFloat();
  22. },
  23. 'boolean' => function () use ($faker) {
  24. return $faker->boolean();
  25. },
  26. 'string' => function () use ($faker) {
  27. return $faker->word;
  28. },
  29. 'array' => function () {
  30. return [];
  31. },
  32. 'object' => function () {
  33. return new stdClass;
  34. },
  35. ];
  36. $fakeFactory = $fakeFactories[$type] ?? $fakeFactories['string'];
  37. return $fakeFactory();
  38. }
  39. /**
  40. * Cast a value from a string to a specified type.
  41. *
  42. * @param string $value
  43. * @param string $type
  44. *
  45. * @return mixed
  46. */
  47. protected function castToType(string $value, string $type)
  48. {
  49. $casts = [
  50. 'integer' => 'intval',
  51. 'int' => 'intval',
  52. 'float' => 'floatval',
  53. 'number' => 'floatval',
  54. 'double' => 'floatval',
  55. 'boolean' => 'boolval',
  56. 'bool' => 'boolval',
  57. ];
  58. // First, we handle booleans. We can't use a regular cast,
  59. //because PHP considers string 'false' as true.
  60. if ($value == 'false' && ($type == 'boolean' || $type == 'bool')) {
  61. return false;
  62. }
  63. if (isset($casts[$type])) {
  64. return $casts[$type]($value);
  65. }
  66. return $value;
  67. }
  68. /**
  69. * Normalizes the stated "type" of a parameter (eg "int", "integer", "double")
  70. * to a number of standard types (integer, boolean, float).
  71. *
  72. * @param string $type
  73. *
  74. * @return mixed|string
  75. */
  76. protected function normalizeParameterType(string $type)
  77. {
  78. $typeMap = [
  79. 'int' => 'integer',
  80. 'bool' => 'boolean',
  81. 'double' => 'float',
  82. ];
  83. return $type ? ($typeMap[$type] ?? $type) : 'string';
  84. }
  85. /**
  86. * Allows users to specify that we shouldn't generate an example for the parameter
  87. * by writing 'No-example'.
  88. *
  89. * @param string $description
  90. *
  91. * @return bool If true, don't generate an example for this.
  92. */
  93. protected function shouldExcludeExample(string $description)
  94. {
  95. return strpos($description, ' No-example') !== false;
  96. }
  97. /**
  98. * Allows users to specify an example for the parameter by writing 'Example: the-example',
  99. * to be used in example requests and response calls.
  100. *
  101. * @param string $description
  102. * @param string $type The type of the parameter. Used to cast the example provided, if any.
  103. *
  104. * @return array The description and included example.
  105. */
  106. protected function parseParamDescription(string $description, string $type)
  107. {
  108. $example = null;
  109. if (preg_match('/(.*)\bExample:\s*(.+)\s*/', $description, $content)) {
  110. $description = trim($content[1]);
  111. // examples are parsed as strings by default, we need to cast them properly
  112. $example = $this->castToType($content[2], $type);
  113. }
  114. return [$description, $example];
  115. }
  116. }