-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathZendTrait.php
More file actions
87 lines (78 loc) · 2.63 KB
/
ZendTrait.php
File metadata and controls
87 lines (78 loc) · 2.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
<?php
/**
* @author Patsura Dmitry https://github.com/ovr <talk@dmtry.me>
*/
namespace Ovr\Swagger;
use InvalidArgumentException;
use RuntimeException;
use Swagger\Annotations\Operation;
use Swagger\Annotations\Parameter;
use Zend\Http\Request;
use Zend\Http\Response;
trait ZendTrait
{
/**
* Prepare a Zend Request by Operation with $parameters
*
* @param Operation $operation
* @param array $parameters
* @param int $options BitMask of options to skip or something else
* @return Request
* @throws \InvalidArgumentException
* @throws \RuntimeException
*/
public function makeRequestByOperation(Operation $operation, array $parameters = [], $options = 0)
{
$request = new Request();
$path = $operation->path;
SwaggerWrapper::hydrateRequestByOperation(
$operation,
function (Parameter $parameter, $value) use ($request, &$path) {
switch ($parameter->in) {
case 'header':
$request->getHeaders()->addHeaderLine($parameter->name, $value);
break;
case 'path':
$path = str_replace('{' . $parameter->name . '}', $value, $path);
break;
case 'query':
$request->getQuery()->set($parameter->name, $value);
break;
case 'formData':
$request->getPost()->set($parameter->name, $value);
break;
default:
throw new RuntimeException(
sprintf(
'Parameter "%s" with ->in = "%s" is not supported',
$parameter->parameter,
$parameter->in
)
);
}
},
$parameters,
$options
);
$request->setUri($path);
$request->setMethod($operation->method);
return $request;
}
/**
* @param Response $response
* @return ResponseData
*/
protected function extractResponseData(Response $response)
{
$header = $response->getHeaders()->get('content-type');
if ($header) {
$contentType = $header->getFieldValue();
return ResponseData::factory(
$contentType,
(string) $response->getContent(),
$response->getStatusCode()
);
}
throw new RuntimeException('Unknown HTTP "content-type"');
}
}