2
0

FormDataProcessor.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. <?php
  2. /**
  3. * FormDataProcessor
  4. * PHP version 7.4
  5. *
  6. * @category Class
  7. * @package WikipaliApi
  8. * @author OpenAPI Generator team
  9. * @link https://openapi-generator.tech
  10. */
  11. /**
  12. * MINT API
  13. *
  14. * International Academy Of Pali Tipitaka(国际巴利三藏学院) 由 `php openapi/scripts/generate.php` 依据 api-v13 的路由与控制器源码生成。 响应统一封装为 `{ok, message, data}`;`ok=false` 时 `message` 为错误信息。
  15. *
  16. * The version of the OpenAPI document: 2026.10.15
  17. * Generated by: https://openapi-generator.tech
  18. * Generator version: 7.25.0
  19. */
  20. /**
  21. * NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
  22. * https://openapi-generator.tech
  23. * Do not edit the class manually.
  24. */
  25. namespace WikipaliApi;
  26. use ArrayAccess;
  27. use DateTime;
  28. use GuzzleHttp\Psr7\Utils;
  29. use Psr\Http\Message\StreamInterface;
  30. use SplFileObject;
  31. use WikipaliApi\Model\ModelInterface;
  32. /**
  33. * FormDataProcessor Class Doc Comment
  34. *
  35. * @category Class
  36. * @package WikipaliApi
  37. * @author OpenAPI Generator team
  38. * @link https://openapi-generator.tech
  39. */
  40. class FormDataProcessor
  41. {
  42. /**
  43. * Tags whether payload passed to ::prepare() contains one or more
  44. * SplFileObject or stream values.
  45. */
  46. public bool $has_file = false;
  47. /**
  48. * Take value and turn it into an array suitable for inclusion in
  49. * the http body (form parameter). If it's a string, pass through unchanged
  50. * If it's a datetime object, format it in ISO8601
  51. *
  52. * @param array<string|bool|array|DateTime|ArrayAccess|SplFileObject> $values the value of the form parameter
  53. *
  54. * @return array [key => value] of formdata
  55. */
  56. public function prepare(array $values): array
  57. {
  58. $this->has_file = false;
  59. $result = [];
  60. foreach ($values as $k => $v) {
  61. if ($v === null) {
  62. continue;
  63. }
  64. $result[$k] = $this->makeFormSafe($v);
  65. }
  66. return $result;
  67. }
  68. /**
  69. * Flattens a multi-level array of data and generates a single-level array
  70. * compatible with formdata - a single-level array where the keys use bracket
  71. * notation to signify nested data.
  72. *
  73. * credit: https://github.com/FranBar1966/FlatPHP
  74. */
  75. public static function flatten(array $source, string $start = ''): array
  76. {
  77. $opt = [
  78. 'prefix' => '[',
  79. 'suffix' => ']',
  80. 'suffix-end' => true,
  81. 'prefix-list' => '[',
  82. 'suffix-list' => ']',
  83. 'suffix-list-end' => true,
  84. ];
  85. if ($start === '') {
  86. $currentPrefix = '';
  87. $currentSuffix = '';
  88. $currentSuffixEnd = false;
  89. } elseif (array_is_list($source)) {
  90. $currentPrefix = $opt['prefix-list'];
  91. $currentSuffix = $opt['suffix-list'];
  92. $currentSuffixEnd = $opt['suffix-list-end'];
  93. } else {
  94. $currentPrefix = $opt['prefix'];
  95. $currentSuffix = $opt['suffix'];
  96. $currentSuffixEnd = $opt['suffix-end'];
  97. }
  98. $currentName = $start;
  99. $result = [];
  100. foreach ($source as $key => $val) {
  101. $currentName .= $currentPrefix.$key;
  102. if (is_array($val) && !empty($val)) {
  103. $currentName .= $currentSuffix;
  104. $result += self::flatten($val, $currentName);
  105. } else {
  106. if ($currentSuffixEnd) {
  107. $currentName .= $currentSuffix;
  108. }
  109. if (is_resource($val)) {
  110. $result[$currentName] = $val;
  111. } else {
  112. $result[$currentName] = ObjectSerializer::toString($val);
  113. }
  114. }
  115. $currentName = $start;
  116. }
  117. return $result;
  118. }
  119. /**
  120. * formdata must be limited to scalars or arrays of scalar values,
  121. * or a resource for a file upload. Here we iterate through all available
  122. * data and identify how to handle each scenario
  123. */
  124. protected function makeFormSafe($value)
  125. {
  126. if ($value instanceof SplFileObject) {
  127. return $this->processFiles([$value])[0];
  128. }
  129. if (is_resource($value)) {
  130. $this->has_file = true;
  131. return $value;
  132. }
  133. if ($value instanceof ModelInterface) {
  134. return $this->processModel($value);
  135. }
  136. if (is_array($value) || (is_object($value) && !$value instanceof \DateTimeInterface)) {
  137. $data = [];
  138. foreach ($value as $k => $v) {
  139. $data[$k] = $this->makeFormSafe($v);
  140. }
  141. return $data;
  142. }
  143. return ObjectSerializer::toString($value);
  144. }
  145. /**
  146. * We are able to handle nested ModelInterface. We do not simply call
  147. * json_decode(json_encode()) because any given model may have binary data
  148. * or other data that cannot be serialized to a JSON string
  149. */
  150. protected function processModel(ModelInterface $model): array
  151. {
  152. $result = [];
  153. foreach ($model::openAPITypes() as $name => $type) {
  154. $value = $model->offsetGet($name);
  155. if ($value === null) {
  156. continue;
  157. }
  158. if (strpos($type, '\SplFileObject') !== false) {
  159. $file = is_array($value) ? $value : [$value];
  160. $result[$name] = $this->processFiles($file);
  161. continue;
  162. }
  163. if ($value instanceof ModelInterface) {
  164. $result[$name] = $this->processModel($value);
  165. continue;
  166. }
  167. if (is_array($value) || is_object($value)) {
  168. $result[$name] = $this->makeFormSafe($value);
  169. continue;
  170. }
  171. $result[$name] = ObjectSerializer::toString($value);
  172. }
  173. return $result;
  174. }
  175. /**
  176. * Handle file data
  177. */
  178. protected function processFiles(array $files): array
  179. {
  180. $this->has_file = true;
  181. $result = [];
  182. foreach ($files as $i => $file) {
  183. if (is_array($file)) {
  184. $result[$i] = $this->processFiles($file);
  185. continue;
  186. }
  187. if ($file instanceof StreamInterface) {
  188. $result[$i] = $file;
  189. continue;
  190. }
  191. if ($file instanceof SplFileObject) {
  192. $result[$i] = $this->tryFopen($file);
  193. }
  194. }
  195. return $result;
  196. }
  197. private function tryFopen(SplFileObject $file)
  198. {
  199. return Utils::tryFopen($file->getRealPath(), 'rb');
  200. }
  201. }