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