-
Notifications
You must be signed in to change notification settings - Fork 3
/
RamlDoc.php
169 lines (149 loc) · 3.66 KB
/
RamlDoc.php
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
<?php
/**
* @copyright 2014 Integ S.A.
* @license http://opensource.org/licenses/MIT The MIT License (MIT)
* @author Javier Lorenzana <[email protected]>
*/
namespace GoIntegro\Raml;
class RamlDoc
{
/**
* @var array Read-only.
*/
public $rawRaml;
/**
* @var string
*/
public $fileName;
/**
* @var string
*/
public $fileDir;
/**
* @var Root\MapCollection
*/
public $schemas;
/**
* @var Root\MapCollection
*/
public $resourceTypes;
/**
* @var Root\MapCollection
*/
public $traits;
/**
* @param array $rawRaml
* @param string $fileName
*/
public function __construct(array $rawRaml, $fileName)
{
$this->rawRaml = $rawRaml;
$this->fileName = $fileName;
$this->fileDir = dirname($fileName);
$this->schemata = new Root\MapCollection;
$this->resourceTypes = new Root\MapCollection;
$this->traits = new Root\MapCollection;
}
/**
* @param string $method
* @return boolean
*/
public static function isValidMethod($method)
{
return in_array($method, RamlSpec::$methods);
}
/**
* @param string $mediaType
* @return boolean
*/
public static function isValidMediaType($mediaType)
{
return in_array($mediaType, RamlSpec::$mediaTypes);
}
/**
* @param string $value
* @return boolean
*/
public static function isInclude($value)
{
return 0 === strpos($value, '!include ');
}
/**
* @param string $value
* @return boolean
*/
public static function isResource($value)
{
return 0 === strpos($value, '/');
}
/**
* @param string $value
* @return boolean
*/
public static function isParameter($value)
{
return self::isResource($value)
&& '{' === substr($value, 1, 1) && '}' === substr($value, -1);
}
/**
* @param string $method
* @param string $path
* @return boolean
*/
public function isDefined($method, $path)
{
$raml = $this->getPathDefinition($path);
return isset($raml[$method]);
}
/**
* @param string $path
* @param integer $case
* @return array
*/
public function getAllowedMethods($path, $case = CASE_LOWER)
{
$raml = $this->getPathDefinition($path);
$methods = array_intersect(array_keys($raml), RamlSpec::$methods);
if (CASE_UPPER === $case) {
$callback = function($method) { return strtoupper($method); };
$methods = array_map($callback, $methods);
}
return array_values($methods);
}
/**
* @param string $path
* @return array|NULL
*/
public function getPathDefinition($path)
{
$raml = $this->rawRaml;
foreach (explode('/', substr($path, 1)) as $part) {
$resource = '/' . $part;
if (isset($raml[$resource])) {
$raml = $raml[$resource];
} else {
foreach (array_keys($raml) as $key) {
if (static::isParameter($key)) {
$raml = $raml[$key];
break;
}
}
}
}
return $raml;
}
/**
* @return array
* @see http://raml.org/spec.html#resources-and-nested-resources
*/
public function getResources()
{
$types = [];
foreach ($this->rawRaml as $key => $value) {
if ($this->isResource($key)) {
$types[] = substr($key, 1);
}
}
return $types;
}
}