forked from GoIntegro/raml
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DocExpander.php
126 lines (113 loc) · 3.68 KB
/
DocExpander.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
<?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 DocExpander
{
const ERROR_UNKNOWN_TRAIT = "The trait \"%s\" is unknown.",
ERROR_UNKNOWN_RESOURCE_TYPE = "The resource type \"%s\" is unknown.";
/**
* Applies resource types and traits.
* @param RamlDoc $ramlDoc
* @throws \ErrorException
* @see http://raml.org/spec.html#resource-types-and-traits
*/
public function expand(RamlDoc $ramlDoc)
{
$ramlDoc->rawRaml = self::applyAllToTree($ramlDoc, $ramlDoc->rawRaml);
return $this;
}
/**
* Actually applies resource types and traits.
* @param RamlDoc $ramlDoc
* @param array &$item
* @throws \ErrorException
* @see http://raml.org/spec.html#resource-types-and-traits
*/
private static function applyAllToTree(
RamlDoc $ramlDoc, array &$item, $itemKey = NULL
)
{
if (
RamlDoc::isValidMethod($itemKey)
&& !empty($item[RamlSpec::PROPERTY_APPLY_TRAITS])
) {
$item = self::applyTraitsToNode(
$ramlDoc, $item, $item[RamlSpec::PROPERTY_APPLY_TRAITS]
);
} elseif (RamlDoc::isResource($itemKey)) {
if (!empty($item[RamlSpec::PROPERTY_APPLY_RESOURCE_TYPE])) {
$item = self::applyResourceTypeToNode($ramlDoc, $item);
}
if (!empty($item[RamlSpec::PROPERTY_APPLY_TRAITS])) {
foreach ($item as $key => &$value) {
if (RamlDoc::isValidMethod($key)) {
$value = self::applyTraitsToNode(
$ramlDoc,
$value,
$item[RamlSpec::PROPERTY_APPLY_TRAITS]
);
}
}
}
}
foreach ($item as $key => &$value) {
if (
(RamlDoc::isValidMethod($key) || RamlDoc::isResource($key))
&& !empty($value)
) {
$value = call_user_func(__METHOD__, $ramlDoc, $value, $key);
}
}
return $item;
}
/**
* @param RamlDoc $ramlDoc
* @param array &$item
* @return array
* @throws \ErrorException
* @see http://raml.org/spec.html#usage
*/
public static function applyResourceTypeToNode(
RamlDoc $ramlDoc, array &$item
)
{
$typeName = $item[RamlSpec::PROPERTY_APPLY_RESOURCE_TYPE];
if ($ramlDoc->resourceTypes->has($typeName)) {
$typeRaml = $ramlDoc->resourceTypes->get($typeName);
$item = $typeRaml->apply('/replace', $item);
} else {
$message = sprintf(
self::ERROR_UNKNOWN_RESOURCE_TYPE, $typeName
);
throw new \ErrorException($message);
}
return $item;
}
/**
* @param RamlDoc $ramlDoc
* @param array &$item
* @param array $traits
* @return array
* @throws \ErrorException
* @see http://raml.org/spec.html#usage
*/
private static function applyTraitsToNode(
RamlDoc $ramlDoc, array &$item, array $traits
)
{
foreach ($traits as $traitName) {
if ($ramlDoc->traits->has($traitName)) {
$traitRaml = $ramlDoc->traits->get($traitName);
$item = $traitRaml->apply('/replace', $item);
} else {
$message = sprintf(self::ERROR_UNKNOWN_TRAIT, $traitName);
throw new \ErrorException($message);
}
}
return $item;
}
}