1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Vox\Webservice; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\Psr7\Request; |
6
|
|
|
use Psr\Http\Message\RequestInterface; |
7
|
|
|
use Vox\Webservice\Mapping\Resource; |
8
|
|
|
use Vox\Webservice\Metadata\TransferMetadata; |
9
|
|
|
|
10
|
|
|
class Criteria implements CriteriaInterface |
11
|
|
|
{ |
12
|
|
|
private $params = []; |
13
|
|
|
|
14
|
|
|
private $query = []; |
15
|
|
|
|
16
|
|
|
private $path; |
17
|
|
|
|
18
|
|
|
private $operationType = self::OPERATION_TYPE_COLLECTION; |
19
|
|
|
|
20
|
1 |
|
public function withParams(array $params): CriteriaInterface |
21
|
|
|
{ |
22
|
1 |
|
$this->params = $params; |
23
|
|
|
|
24
|
1 |
|
return $this; |
25
|
|
|
} |
26
|
|
|
|
27
|
1 |
|
public function withQuery(array $query): CriteriaInterface |
28
|
|
|
{ |
29
|
1 |
|
$this->query = $query; |
30
|
|
|
|
31
|
1 |
|
return $this; |
32
|
|
|
} |
33
|
|
|
|
34
|
1 |
|
public function setParam($name, $value): CriteriaInterface |
35
|
|
|
{ |
36
|
1 |
|
$this->params[$name] = $value; |
37
|
|
|
|
38
|
1 |
|
return $this; |
39
|
|
|
} |
40
|
|
|
|
41
|
1 |
|
public function setQuery($name, $value): CriteriaInterface |
42
|
|
|
{ |
43
|
1 |
|
$this->query[$name] = $value; |
44
|
|
|
|
45
|
1 |
|
return $this; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function withPath(string $path): CriteriaInterface |
49
|
|
|
{ |
50
|
|
|
$this->path = $path; |
51
|
|
|
|
52
|
|
|
return $this; |
53
|
|
|
} |
54
|
|
|
|
55
|
4 |
|
public function withOperationType(string $operationType): CriteriaInterface |
56
|
|
|
{ |
57
|
4 |
|
if (!in_array($operationType, [self::OPERATION_TYPE_ITEM, self::OPERATION_TYPE_COLLECTION])) { |
58
|
|
|
throw new \InvalidArgumentException('invalid operation type'); |
59
|
|
|
} |
60
|
|
|
|
61
|
4 |
|
$this->operationType = $operationType; |
62
|
|
|
|
63
|
4 |
|
return $this; |
64
|
|
|
} |
65
|
|
|
|
66
|
4 |
|
public function createRequest(TransferMetadata $metadata): RequestInterface |
67
|
|
|
{ |
68
|
|
|
/* @var $resource Resource */ |
69
|
4 |
|
$resource = $metadata->getAnnotation(Resource::class, true); |
70
|
|
|
|
71
|
4 |
|
$route = $resource->route . "/{$this->path}"; |
72
|
|
|
|
73
|
4 |
|
foreach ($this->params as $name => $value) { |
74
|
1 |
|
$route .= "/$name/$value"; |
75
|
|
|
} |
76
|
|
|
|
77
|
4 |
|
$route = preg_replace('.(/{2,}).', '/', $route); |
78
|
4 |
|
$route = preg_replace('.(/+)$.', '', $route); |
79
|
|
|
|
80
|
4 |
|
if (!empty($this->query)) { |
81
|
2 |
|
$route .= '?' . http_build_query($this->query); |
82
|
|
|
} |
83
|
|
|
|
84
|
4 |
|
return new Request('GET', $route, ['Content-Type' => 'application/json']); |
85
|
|
|
} |
86
|
|
|
|
87
|
2 |
|
public function getOperationType(): string |
88
|
|
|
{ |
89
|
2 |
|
return $this->operationType; |
90
|
|
|
} |
91
|
|
|
} |