1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of indragunawan/rest-service package. |
5
|
|
|
* |
6
|
|
|
* (c) Indra Gunawan <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace IndraGunawan\RestService; |
13
|
|
|
|
14
|
|
|
use IndraGunawan\RestService\Parser\SpecificationParser; |
15
|
|
|
|
16
|
|
|
class Service implements ServiceInterface |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var array |
20
|
|
|
*/ |
21
|
|
|
private $service; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @param string $specificationFile |
25
|
|
|
* @param array $defaults |
26
|
|
|
* @param string $cacheDir |
27
|
|
|
* @param bool $debug |
28
|
|
|
* |
29
|
|
|
* @throws \IndraGunawan\RestService\Exception\InvalidSpecificationException |
30
|
|
|
*/ |
31
|
7 |
|
public function __construct($specificationFile, array $defaults = [], $cacheDir = null, $debug = false) |
32
|
|
|
{ |
33
|
7 |
|
$parser = new SpecificationParser(); |
34
|
7 |
|
$this->service = $parser->parse($specificationFile, $defaults ?: [], $cacheDir, $debug); |
35
|
7 |
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* {@inheritdoc} |
39
|
|
|
*/ |
40
|
1 |
|
public function getName() |
41
|
|
|
{ |
42
|
1 |
|
return $this->service['name']; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* {@inheritdoc} |
47
|
|
|
*/ |
48
|
3 |
|
public function getEndpoint() |
49
|
|
|
{ |
50
|
3 |
|
return $this->service['endpoint']; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* {@inheritdoc} |
55
|
|
|
*/ |
56
|
3 |
|
public function getOperations() |
57
|
|
|
{ |
58
|
3 |
|
return $this->service['operations']; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* {@inheritdoc} |
63
|
|
|
*/ |
64
|
3 |
|
public function hasOperation($name) |
65
|
|
|
{ |
66
|
3 |
|
return array_key_exists($name, $this->getOperations()); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* {@inheritdoc} |
71
|
|
|
*/ |
72
|
3 |
|
public function getOperation($name) |
73
|
|
|
{ |
74
|
3 |
|
if ($this->hasOperation($name)) { |
75
|
3 |
|
return $this->getOperations()[$name]; |
76
|
|
|
} |
77
|
|
|
|
78
|
1 |
|
return; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* {@inheritdoc} |
83
|
|
|
*/ |
84
|
1 |
|
public function getShapes() |
85
|
|
|
{ |
86
|
1 |
|
return $this->service['shapes']; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* {@inheritdoc} |
91
|
|
|
*/ |
92
|
1 |
|
public function hasShape($name) |
93
|
|
|
{ |
94
|
1 |
|
return array_key_exists($name, $this->getShapes()); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* {@inheritdoc} |
99
|
|
|
*/ |
100
|
1 |
|
public function getShape($name) |
101
|
|
|
{ |
102
|
1 |
|
if ($this->hasShape($name)) { |
103
|
1 |
|
return $this->getShapes()[$name]; |
104
|
|
|
} |
105
|
|
|
|
106
|
1 |
|
return; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* {@inheritdoc} |
111
|
|
|
*/ |
112
|
1 |
|
public function getErrorShapes() |
113
|
|
|
{ |
114
|
1 |
|
return $this->service['errorShapes']; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* {@inheritdoc} |
119
|
|
|
*/ |
120
|
1 |
|
public function hasErrorShape($name) |
121
|
|
|
{ |
122
|
1 |
|
return array_key_exists($name, $this->getErrorShapes()); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* {@inheritdoc} |
127
|
|
|
*/ |
128
|
1 |
|
public function getErrorShape($name) |
129
|
|
|
{ |
130
|
1 |
|
if ($this->hasErrorShape($name)) { |
131
|
1 |
|
return $this->getErrorShapes()[$name]; |
132
|
|
|
} |
133
|
|
|
|
134
|
1 |
|
return; |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|