@@ -23,201 +23,201 @@ |
||
23 | 23 | */ |
24 | 24 | class Nav |
25 | 25 | { |
26 | - protected static $tree = array(); |
|
27 | - public static $root = 'home'; |
|
26 | + protected static $tree = array(); |
|
27 | + public static $root = 'home'; |
|
28 | 28 | |
29 | - /** |
|
30 | - * @var array all paths beginning with any of the following will be excluded |
|
31 | - * from documentation. if an empty string is given it will exclude the root |
|
32 | - */ |
|
33 | - public static $excludedPaths = array(''); |
|
34 | - /** |
|
35 | - * @var array prefix additional menu items with one of the following syntax |
|
36 | - * [$path => $text] |
|
37 | - * [$path] |
|
38 | - * [$path => ['text' => $text, 'url' => $url, 'trail'=> $trail]] |
|
39 | - */ |
|
40 | - public static $prepends = array(); |
|
41 | - /** |
|
42 | - * @var array suffix additional menu items with one of the following syntax |
|
43 | - * [$path => $text] |
|
44 | - * [$path] |
|
45 | - * [$path => ['text' => $text, 'url' => $url, 'trail'=> $trail]] |
|
46 | - */ |
|
47 | - public static $appends = array(); |
|
29 | + /** |
|
30 | + * @var array all paths beginning with any of the following will be excluded |
|
31 | + * from documentation. if an empty string is given it will exclude the root |
|
32 | + */ |
|
33 | + public static $excludedPaths = array(''); |
|
34 | + /** |
|
35 | + * @var array prefix additional menu items with one of the following syntax |
|
36 | + * [$path => $text] |
|
37 | + * [$path] |
|
38 | + * [$path => ['text' => $text, 'url' => $url, 'trail'=> $trail]] |
|
39 | + */ |
|
40 | + public static $prepends = array(); |
|
41 | + /** |
|
42 | + * @var array suffix additional menu items with one of the following syntax |
|
43 | + * [$path => $text] |
|
44 | + * [$path] |
|
45 | + * [$path => ['text' => $text, 'url' => $url, 'trail'=> $trail]] |
|
46 | + */ |
|
47 | + public static $appends = array(); |
|
48 | 48 | |
49 | - public static $addExtension = true; |
|
49 | + public static $addExtension = true; |
|
50 | 50 | |
51 | - protected static $extension = ''; |
|
52 | - protected static $activeTrail = ''; |
|
53 | - protected static $url; |
|
51 | + protected static $extension = ''; |
|
52 | + protected static $activeTrail = ''; |
|
53 | + protected static $url; |
|
54 | 54 | |
55 | - public static function get($for = '', $activeTrail = null) |
|
56 | - { |
|
57 | - if (empty(static::$tree)) { |
|
58 | - /** @var Restler $restler */ |
|
59 | - $restler = Scope::get('Restler'); |
|
60 | - if (static::$addExtension) |
|
61 | - static::$extension = isset($restler->responseFormat) |
|
62 | - ? '.' . $restler->responseFormat->getExtension() |
|
63 | - : '.html'; |
|
64 | - static::$url = $restler->getBaseUrl(); |
|
65 | - if (empty(static::$url)) |
|
66 | - static::$url = ''; |
|
67 | - static::$activeTrail = $activeTrail = empty($activeTrail) |
|
68 | - ? (empty($restler->url) || $restler->url == 'index' |
|
69 | - ? static::$root |
|
70 | - : $restler->url |
|
71 | - ) |
|
72 | - : $activeTrail; |
|
73 | - if (static::$addExtension) |
|
74 | - static::$extension = isset($restler->responseFormat) |
|
75 | - ? '.' . $restler->responseFormat->getExtension() |
|
76 | - : '.html'; |
|
77 | - static::addUrls(static::$prepends); |
|
78 | - $map = Routes::findAll( |
|
79 | - static::$excludedPaths, |
|
80 | - array('POST', 'DELETE', 'PUT', 'PATCH'), |
|
81 | - $restler->getRequestedApiVersion() |
|
82 | - ); |
|
83 | - foreach ($map as $path => $data) { |
|
84 | - foreach ($data as $item) { |
|
85 | - $access = $item['access']; |
|
86 | - $route = $item['route']; |
|
87 | - $url = $route['url']; |
|
88 | - if ($access && !Text::contains($url, '{')) { |
|
89 | - $label = Util::nestedValue( |
|
90 | - $route, |
|
91 | - 'metadata', |
|
92 | - CommentParser::$embeddedDataName, |
|
93 | - 'label' |
|
94 | - ); |
|
95 | - if (!empty($url)) { |
|
96 | - $url .= static::$extension; |
|
97 | - } |
|
98 | - static::add($url, $label); |
|
99 | - } |
|
100 | - } |
|
101 | - } |
|
102 | - static::addUrls(static::$appends); |
|
103 | - } elseif (empty($activeTrail)) { |
|
104 | - $activeTrail = static::$activeTrail; |
|
105 | - } |
|
106 | - $tree = static::$tree; |
|
107 | - $activeTrail = explode('/', $activeTrail); |
|
108 | - $nested = & static::nested($tree, $activeTrail); |
|
109 | - if (is_array($nested)) { |
|
110 | - $nested['active'] = true; |
|
111 | - } |
|
112 | - if (!empty($for)) { |
|
113 | - $for = explode('/', $for); |
|
114 | - $tree = static::nested($tree, $for)['children']; |
|
115 | - } |
|
116 | - return array_filter($tree); |
|
117 | - } |
|
55 | + public static function get($for = '', $activeTrail = null) |
|
56 | + { |
|
57 | + if (empty(static::$tree)) { |
|
58 | + /** @var Restler $restler */ |
|
59 | + $restler = Scope::get('Restler'); |
|
60 | + if (static::$addExtension) |
|
61 | + static::$extension = isset($restler->responseFormat) |
|
62 | + ? '.' . $restler->responseFormat->getExtension() |
|
63 | + : '.html'; |
|
64 | + static::$url = $restler->getBaseUrl(); |
|
65 | + if (empty(static::$url)) |
|
66 | + static::$url = ''; |
|
67 | + static::$activeTrail = $activeTrail = empty($activeTrail) |
|
68 | + ? (empty($restler->url) || $restler->url == 'index' |
|
69 | + ? static::$root |
|
70 | + : $restler->url |
|
71 | + ) |
|
72 | + : $activeTrail; |
|
73 | + if (static::$addExtension) |
|
74 | + static::$extension = isset($restler->responseFormat) |
|
75 | + ? '.' . $restler->responseFormat->getExtension() |
|
76 | + : '.html'; |
|
77 | + static::addUrls(static::$prepends); |
|
78 | + $map = Routes::findAll( |
|
79 | + static::$excludedPaths, |
|
80 | + array('POST', 'DELETE', 'PUT', 'PATCH'), |
|
81 | + $restler->getRequestedApiVersion() |
|
82 | + ); |
|
83 | + foreach ($map as $path => $data) { |
|
84 | + foreach ($data as $item) { |
|
85 | + $access = $item['access']; |
|
86 | + $route = $item['route']; |
|
87 | + $url = $route['url']; |
|
88 | + if ($access && !Text::contains($url, '{')) { |
|
89 | + $label = Util::nestedValue( |
|
90 | + $route, |
|
91 | + 'metadata', |
|
92 | + CommentParser::$embeddedDataName, |
|
93 | + 'label' |
|
94 | + ); |
|
95 | + if (!empty($url)) { |
|
96 | + $url .= static::$extension; |
|
97 | + } |
|
98 | + static::add($url, $label); |
|
99 | + } |
|
100 | + } |
|
101 | + } |
|
102 | + static::addUrls(static::$appends); |
|
103 | + } elseif (empty($activeTrail)) { |
|
104 | + $activeTrail = static::$activeTrail; |
|
105 | + } |
|
106 | + $tree = static::$tree; |
|
107 | + $activeTrail = explode('/', $activeTrail); |
|
108 | + $nested = & static::nested($tree, $activeTrail); |
|
109 | + if (is_array($nested)) { |
|
110 | + $nested['active'] = true; |
|
111 | + } |
|
112 | + if (!empty($for)) { |
|
113 | + $for = explode('/', $for); |
|
114 | + $tree = static::nested($tree, $for)['children']; |
|
115 | + } |
|
116 | + return array_filter($tree); |
|
117 | + } |
|
118 | 118 | |
119 | - protected static function & nested(array & $tree, array $parts) |
|
120 | - { |
|
121 | - if (!empty($parts)) { |
|
122 | - $part = array_shift($parts); |
|
123 | - if (empty($tree[$part])) { |
|
124 | - return $tree[$part]; |
|
125 | - } elseif (empty($parts)) { |
|
126 | - return static::nested($tree[$part], $parts); |
|
127 | - } elseif (!empty($tree[$part]['children'])) { |
|
128 | - return static::nested($tree[$part]['children'], $parts); |
|
129 | - } |
|
130 | - } else { |
|
131 | - return $tree; |
|
132 | - } |
|
133 | - $value = null; |
|
134 | - return $value; |
|
135 | - } |
|
119 | + protected static function & nested(array & $tree, array $parts) |
|
120 | + { |
|
121 | + if (!empty($parts)) { |
|
122 | + $part = array_shift($parts); |
|
123 | + if (empty($tree[$part])) { |
|
124 | + return $tree[$part]; |
|
125 | + } elseif (empty($parts)) { |
|
126 | + return static::nested($tree[$part], $parts); |
|
127 | + } elseif (!empty($tree[$part]['children'])) { |
|
128 | + return static::nested($tree[$part]['children'], $parts); |
|
129 | + } |
|
130 | + } else { |
|
131 | + return $tree; |
|
132 | + } |
|
133 | + $value = null; |
|
134 | + return $value; |
|
135 | + } |
|
136 | 136 | |
137 | - public static function addUrls(array $urls) |
|
138 | - { |
|
139 | - foreach ($urls as $url => $label) { |
|
140 | - $trail = null; |
|
141 | - if (is_array($label)) { |
|
142 | - if (isset($label['trail'])) { |
|
143 | - $trail = $label['trail']; |
|
144 | - } |
|
145 | - if (isset($label['url'])) { |
|
146 | - $url = $label['url']; |
|
147 | - $label = isset($label['label']) ? $label['label'] : null; |
|
148 | - } else { |
|
149 | - $url = current(array_keys($label)); |
|
150 | - $label = current($label); |
|
151 | - } |
|
137 | + public static function addUrls(array $urls) |
|
138 | + { |
|
139 | + foreach ($urls as $url => $label) { |
|
140 | + $trail = null; |
|
141 | + if (is_array($label)) { |
|
142 | + if (isset($label['trail'])) { |
|
143 | + $trail = $label['trail']; |
|
144 | + } |
|
145 | + if (isset($label['url'])) { |
|
146 | + $url = $label['url']; |
|
147 | + $label = isset($label['label']) ? $label['label'] : null; |
|
148 | + } else { |
|
149 | + $url = current(array_keys($label)); |
|
150 | + $label = current($label); |
|
151 | + } |
|
152 | 152 | |
153 | - } |
|
154 | - if (is_numeric($url)) { |
|
155 | - $url = $label; |
|
156 | - $label = null; |
|
157 | - } |
|
158 | - static::add($url, $label, $trail); |
|
159 | - } |
|
160 | - return static::$tree; |
|
161 | - } |
|
153 | + } |
|
154 | + if (is_numeric($url)) { |
|
155 | + $url = $label; |
|
156 | + $label = null; |
|
157 | + } |
|
158 | + static::add($url, $label, $trail); |
|
159 | + } |
|
160 | + return static::$tree; |
|
161 | + } |
|
162 | 162 | |
163 | - public static function add($url, $label = null, $trail = null) |
|
164 | - { |
|
165 | - $r = parse_url($url); |
|
166 | - if (is_null($trail)) { |
|
167 | - $trail = isset($r['path']) ? $r['path'] : static::$root; |
|
168 | - } |
|
169 | - //remove / prefix and / suffixes and any extension |
|
170 | - $trail = strtok(trim($trail, '/'), '.'); |
|
171 | - $parts = explode('/', $trail); |
|
172 | - if (count($parts) == 1 && empty($parts[0])) |
|
173 | - $parts = array(static::$root); |
|
174 | - if (isset($r['fragment'])) { |
|
175 | - $parts[] = $r['fragment']; |
|
176 | - if (is_null($label)) { |
|
177 | - $label = Text::title($r['fragment']); |
|
178 | - } |
|
179 | - } |
|
180 | - if (empty($r['scheme'])) { |
|
181 | - //relative url found |
|
182 | - if (empty($url)) { |
|
183 | - $label = Text::title(static::$root); |
|
184 | - $url = static::$url; |
|
185 | - } else { |
|
186 | - $url = static::$url . '/' . ltrim($url, '/'); |
|
187 | - } |
|
188 | - } |
|
189 | - if (is_null($label)) { |
|
190 | - $label = Text::title(strtok(end($parts), '.')); |
|
191 | - } |
|
192 | - $r['url'] = $url; |
|
193 | - $r['path'] = $trail; |
|
194 | - $r['parts'] = $parts; |
|
195 | - $r['label'] = $label; |
|
196 | - static::build($r); |
|
197 | - return $r; |
|
198 | - } |
|
163 | + public static function add($url, $label = null, $trail = null) |
|
164 | + { |
|
165 | + $r = parse_url($url); |
|
166 | + if (is_null($trail)) { |
|
167 | + $trail = isset($r['path']) ? $r['path'] : static::$root; |
|
168 | + } |
|
169 | + //remove / prefix and / suffixes and any extension |
|
170 | + $trail = strtok(trim($trail, '/'), '.'); |
|
171 | + $parts = explode('/', $trail); |
|
172 | + if (count($parts) == 1 && empty($parts[0])) |
|
173 | + $parts = array(static::$root); |
|
174 | + if (isset($r['fragment'])) { |
|
175 | + $parts[] = $r['fragment']; |
|
176 | + if (is_null($label)) { |
|
177 | + $label = Text::title($r['fragment']); |
|
178 | + } |
|
179 | + } |
|
180 | + if (empty($r['scheme'])) { |
|
181 | + //relative url found |
|
182 | + if (empty($url)) { |
|
183 | + $label = Text::title(static::$root); |
|
184 | + $url = static::$url; |
|
185 | + } else { |
|
186 | + $url = static::$url . '/' . ltrim($url, '/'); |
|
187 | + } |
|
188 | + } |
|
189 | + if (is_null($label)) { |
|
190 | + $label = Text::title(strtok(end($parts), '.')); |
|
191 | + } |
|
192 | + $r['url'] = $url; |
|
193 | + $r['path'] = $trail; |
|
194 | + $r['parts'] = $parts; |
|
195 | + $r['label'] = $label; |
|
196 | + static::build($r); |
|
197 | + return $r; |
|
198 | + } |
|
199 | 199 | |
200 | - public static function build(array $r) |
|
201 | - { |
|
202 | - $p = & static::$tree; |
|
203 | - $parts = $r['parts']; |
|
204 | - $last = count($parts) - 1; |
|
205 | - foreach ($parts as $i => $part) { |
|
206 | - if ($i == $last) { |
|
207 | - $p[$part]['text'] = $r['label']; |
|
208 | - $p[$part]['href'] = $r['url']; |
|
209 | - $p[$part]['class'] = Text::slug($part); |
|
210 | - /* dynamically do it at run time instead |
|
200 | + public static function build(array $r) |
|
201 | + { |
|
202 | + $p = & static::$tree; |
|
203 | + $parts = $r['parts']; |
|
204 | + $last = count($parts) - 1; |
|
205 | + foreach ($parts as $i => $part) { |
|
206 | + if ($i == $last) { |
|
207 | + $p[$part]['text'] = $r['label']; |
|
208 | + $p[$part]['href'] = $r['url']; |
|
209 | + $p[$part]['class'] = Text::slug($part); |
|
210 | + /* dynamically do it at run time instead |
|
211 | 211 | if ($r['path'] == static::$activeTrail) |
212 | 212 | $p[$part]['active'] = true; |
213 | 213 | */ |
214 | - } elseif (!isset($p[$part])) { |
|
215 | - $p[$part] = array(); |
|
216 | - $p[$part]['text'] = Text::title($part); |
|
217 | - $p[$part]['href'] = '#'; |
|
218 | - $p[$part]['children'] = array(); |
|
219 | - } |
|
220 | - $p = & $p[$part]['children']; |
|
221 | - } |
|
222 | - } |
|
214 | + } elseif (!isset($p[$part])) { |
|
215 | + $p[$part] = array(); |
|
216 | + $p[$part]['text'] = Text::title($part); |
|
217 | + $p[$part]['href'] = '#'; |
|
218 | + $p[$part]['children'] = array(); |
|
219 | + } |
|
220 | + $p = & $p[$part]['children']; |
|
221 | + } |
|
222 | + } |
|
223 | 223 | } |
@@ -59,7 +59,7 @@ discard block |
||
59 | 59 | $restler = Scope::get('Restler'); |
60 | 60 | if (static::$addExtension) |
61 | 61 | static::$extension = isset($restler->responseFormat) |
62 | - ? '.' . $restler->responseFormat->getExtension() |
|
62 | + ? '.'.$restler->responseFormat->getExtension() |
|
63 | 63 | : '.html'; |
64 | 64 | static::$url = $restler->getBaseUrl(); |
65 | 65 | if (empty(static::$url)) |
@@ -72,7 +72,7 @@ discard block |
||
72 | 72 | : $activeTrail; |
73 | 73 | if (static::$addExtension) |
74 | 74 | static::$extension = isset($restler->responseFormat) |
75 | - ? '.' . $restler->responseFormat->getExtension() |
|
75 | + ? '.'.$restler->responseFormat->getExtension() |
|
76 | 76 | : '.html'; |
77 | 77 | static::addUrls(static::$prepends); |
78 | 78 | $map = Routes::findAll( |
@@ -183,7 +183,7 @@ discard block |
||
183 | 183 | $label = Text::title(static::$root); |
184 | 184 | $url = static::$url; |
185 | 185 | } else { |
186 | - $url = static::$url . '/' . ltrim($url, '/'); |
|
186 | + $url = static::$url.'/'.ltrim($url, '/'); |
|
187 | 187 | } |
188 | 188 | } |
189 | 189 | if (is_null($label)) { |
@@ -57,23 +57,26 @@ discard block |
||
57 | 57 | if (empty(static::$tree)) { |
58 | 58 | /** @var Restler $restler */ |
59 | 59 | $restler = Scope::get('Restler'); |
60 | - if (static::$addExtension) |
|
61 | - static::$extension = isset($restler->responseFormat) |
|
60 | + if (static::$addExtension) { |
|
61 | + static::$extension = isset($restler->responseFormat) |
|
62 | 62 | ? '.' . $restler->responseFormat->getExtension() |
63 | 63 | : '.html'; |
64 | + } |
|
64 | 65 | static::$url = $restler->getBaseUrl(); |
65 | - if (empty(static::$url)) |
|
66 | - static::$url = ''; |
|
66 | + if (empty(static::$url)) { |
|
67 | + static::$url = ''; |
|
68 | + } |
|
67 | 69 | static::$activeTrail = $activeTrail = empty($activeTrail) |
68 | 70 | ? (empty($restler->url) || $restler->url == 'index' |
69 | 71 | ? static::$root |
70 | 72 | : $restler->url |
71 | 73 | ) |
72 | 74 | : $activeTrail; |
73 | - if (static::$addExtension) |
|
74 | - static::$extension = isset($restler->responseFormat) |
|
75 | + if (static::$addExtension) { |
|
76 | + static::$extension = isset($restler->responseFormat) |
|
75 | 77 | ? '.' . $restler->responseFormat->getExtension() |
76 | 78 | : '.html'; |
79 | + } |
|
77 | 80 | static::addUrls(static::$prepends); |
78 | 81 | $map = Routes::findAll( |
79 | 82 | static::$excludedPaths, |
@@ -169,8 +172,9 @@ discard block |
||
169 | 172 | //remove / prefix and / suffixes and any extension |
170 | 173 | $trail = strtok(trim($trail, '/'), '.'); |
171 | 174 | $parts = explode('/', $trail); |
172 | - if (count($parts) == 1 && empty($parts[0])) |
|
173 | - $parts = array(static::$root); |
|
175 | + if (count($parts) == 1 && empty($parts[0])) { |
|
176 | + $parts = array(static::$root); |
|
177 | + } |
|
174 | 178 | if (isset($r['fragment'])) { |
175 | 179 | $parts[] = $r['fragment']; |
176 | 180 | if (is_null($label)) { |
@@ -34,453 +34,453 @@ |
||
34 | 34 | */ |
35 | 35 | class Forms implements iFilter |
36 | 36 | { |
37 | - const FORM_KEY = 'form_key'; |
|
38 | - public static $filterFormRequestsOnly = false; |
|
37 | + const FORM_KEY = 'form_key'; |
|
38 | + public static $filterFormRequestsOnly = false; |
|
39 | 39 | |
40 | - public static $excludedPaths = array(); |
|
40 | + public static $excludedPaths = array(); |
|
41 | 41 | |
42 | - private static $style; |
|
43 | - /** |
|
44 | - * @var bool should we fill up the form using given data? |
|
45 | - */ |
|
46 | - public static $preFill = true; |
|
47 | - /** |
|
48 | - * @var ValidationInfo |
|
49 | - */ |
|
50 | - public static $validationInfo = null; |
|
51 | - protected static $inputTypes = array( |
|
52 | - 'hidden', |
|
53 | - 'password', |
|
54 | - 'button', |
|
55 | - 'image', |
|
56 | - 'file', |
|
57 | - 'reset', |
|
58 | - 'submit', |
|
59 | - 'search', |
|
60 | - 'checkbox', |
|
61 | - 'radio', |
|
62 | - 'email', |
|
63 | - 'text', |
|
64 | - 'color', |
|
65 | - 'date', |
|
66 | - 'datetime', |
|
67 | - 'datetime-local', |
|
68 | - 'email', |
|
69 | - 'month', |
|
70 | - 'number', |
|
71 | - 'range', |
|
72 | - 'search', |
|
73 | - 'tel', |
|
74 | - 'time', |
|
75 | - 'url', |
|
76 | - 'week', |
|
77 | - ); |
|
78 | - protected static $fileUpload = false; |
|
79 | - private static $key = array(); |
|
80 | - /** |
|
81 | - * @var ApiMethodInfo; |
|
82 | - */ |
|
83 | - private static $info; |
|
42 | + private static $style; |
|
43 | + /** |
|
44 | + * @var bool should we fill up the form using given data? |
|
45 | + */ |
|
46 | + public static $preFill = true; |
|
47 | + /** |
|
48 | + * @var ValidationInfo |
|
49 | + */ |
|
50 | + public static $validationInfo = null; |
|
51 | + protected static $inputTypes = array( |
|
52 | + 'hidden', |
|
53 | + 'password', |
|
54 | + 'button', |
|
55 | + 'image', |
|
56 | + 'file', |
|
57 | + 'reset', |
|
58 | + 'submit', |
|
59 | + 'search', |
|
60 | + 'checkbox', |
|
61 | + 'radio', |
|
62 | + 'email', |
|
63 | + 'text', |
|
64 | + 'color', |
|
65 | + 'date', |
|
66 | + 'datetime', |
|
67 | + 'datetime-local', |
|
68 | + 'email', |
|
69 | + 'month', |
|
70 | + 'number', |
|
71 | + 'range', |
|
72 | + 'search', |
|
73 | + 'tel', |
|
74 | + 'time', |
|
75 | + 'url', |
|
76 | + 'week', |
|
77 | + ); |
|
78 | + protected static $fileUpload = false; |
|
79 | + private static $key = array(); |
|
80 | + /** |
|
81 | + * @var ApiMethodInfo; |
|
82 | + */ |
|
83 | + private static $info; |
|
84 | 84 | |
85 | - public static function setStyles(HtmlForm $style) |
|
86 | - { |
|
87 | - static::$style = get_class($style); |
|
88 | - } |
|
85 | + public static function setStyles(HtmlForm $style) |
|
86 | + { |
|
87 | + static::$style = get_class($style); |
|
88 | + } |
|
89 | 89 | |
90 | - /** |
|
91 | - * Get the form |
|
92 | - * |
|
93 | - * @param string $method http method to submit the form |
|
94 | - * @param string $action relative path from the web root. When set to null |
|
95 | - * it uses the current api method's path |
|
96 | - * @param bool $dataOnly if you want to render the form yourself use this |
|
97 | - * option |
|
98 | - * @param string $prefix used for adjusting the spacing in front of |
|
99 | - * form elements |
|
100 | - * @param string $indent used for adjusting indentation |
|
101 | - * |
|
102 | - * @return array|T |
|
103 | - * |
|
104 | - * @throws RestException |
|
105 | - */ |
|
106 | - public static function get($method = 'POST', $action = null, $dataOnly = false, $prefix = '', $indent = ' ') |
|
107 | - { |
|
108 | - if (!static::$style) { |
|
109 | - static::$style = 'Luracast\\Restler\\UI\HtmlForm'; |
|
110 | - } |
|
90 | + /** |
|
91 | + * Get the form |
|
92 | + * |
|
93 | + * @param string $method http method to submit the form |
|
94 | + * @param string $action relative path from the web root. When set to null |
|
95 | + * it uses the current api method's path |
|
96 | + * @param bool $dataOnly if you want to render the form yourself use this |
|
97 | + * option |
|
98 | + * @param string $prefix used for adjusting the spacing in front of |
|
99 | + * form elements |
|
100 | + * @param string $indent used for adjusting indentation |
|
101 | + * |
|
102 | + * @return array|T |
|
103 | + * |
|
104 | + * @throws RestException |
|
105 | + */ |
|
106 | + public static function get($method = 'POST', $action = null, $dataOnly = false, $prefix = '', $indent = ' ') |
|
107 | + { |
|
108 | + if (!static::$style) { |
|
109 | + static::$style = 'Luracast\\Restler\\UI\HtmlForm'; |
|
110 | + } |
|
111 | 111 | |
112 | - try { |
|
113 | - /** @var Restler $restler */ |
|
114 | - $restler = Scope::get('Restler'); |
|
115 | - if (is_null($action)) { |
|
116 | - $action = $restler->url; |
|
117 | - } |
|
112 | + try { |
|
113 | + /** @var Restler $restler */ |
|
114 | + $restler = Scope::get('Restler'); |
|
115 | + if (is_null($action)) { |
|
116 | + $action = $restler->url; |
|
117 | + } |
|
118 | 118 | |
119 | - $info = $restler->url == $action |
|
120 | - && Util::getRequestMethod() == $method |
|
121 | - ? $restler->apiMethodInfo |
|
122 | - : Routes::find( |
|
123 | - trim($action, '/'), |
|
124 | - $method, |
|
125 | - $restler->getRequestedApiVersion(), |
|
126 | - static::$preFill || |
|
127 | - ($restler->requestMethod == $method && |
|
128 | - $restler->url == $action) |
|
129 | - ? $restler->getRequestData() |
|
130 | - : array() |
|
131 | - ); |
|
119 | + $info = $restler->url == $action |
|
120 | + && Util::getRequestMethod() == $method |
|
121 | + ? $restler->apiMethodInfo |
|
122 | + : Routes::find( |
|
123 | + trim($action, '/'), |
|
124 | + $method, |
|
125 | + $restler->getRequestedApiVersion(), |
|
126 | + static::$preFill || |
|
127 | + ($restler->requestMethod == $method && |
|
128 | + $restler->url == $action) |
|
129 | + ? $restler->getRequestData() |
|
130 | + : array() |
|
131 | + ); |
|
132 | 132 | |
133 | - } catch (RestException $e) { |
|
134 | - //echo $e->getErrorMessage(); |
|
135 | - $info = false; |
|
136 | - } |
|
137 | - if (!$info) { |
|
138 | - throw new RestException(500, 'invalid action path for form `' . $method . ' ' . $action . '`'); |
|
139 | - } |
|
140 | - static::$info = $info; |
|
141 | - $m = $info->metadata; |
|
142 | - $r = static::fields($dataOnly); |
|
143 | - if ($method != 'GET' && $method != 'POST') { |
|
144 | - if (empty(Defaults::$httpMethodOverrideProperty)) { |
|
145 | - throw new RestException( |
|
146 | - 500, |
|
147 | - 'Forms require `Defaults::\$httpMethodOverrideProperty`' . |
|
148 | - "for supporting HTTP $method" |
|
149 | - ); |
|
150 | - } |
|
151 | - if ($dataOnly) { |
|
152 | - $r[] = array( |
|
153 | - 'tag' => 'input', |
|
154 | - 'name' => Defaults::$httpMethodOverrideProperty, |
|
155 | - 'type' => 'hidden', |
|
156 | - 'value' => 'method', |
|
157 | - ); |
|
158 | - } else { |
|
159 | - $r[] = T::input() |
|
160 | - ->name(Defaults::$httpMethodOverrideProperty) |
|
161 | - ->value($method) |
|
162 | - ->type('hidden'); |
|
163 | - } |
|
133 | + } catch (RestException $e) { |
|
134 | + //echo $e->getErrorMessage(); |
|
135 | + $info = false; |
|
136 | + } |
|
137 | + if (!$info) { |
|
138 | + throw new RestException(500, 'invalid action path for form `' . $method . ' ' . $action . '`'); |
|
139 | + } |
|
140 | + static::$info = $info; |
|
141 | + $m = $info->metadata; |
|
142 | + $r = static::fields($dataOnly); |
|
143 | + if ($method != 'GET' && $method != 'POST') { |
|
144 | + if (empty(Defaults::$httpMethodOverrideProperty)) { |
|
145 | + throw new RestException( |
|
146 | + 500, |
|
147 | + 'Forms require `Defaults::\$httpMethodOverrideProperty`' . |
|
148 | + "for supporting HTTP $method" |
|
149 | + ); |
|
150 | + } |
|
151 | + if ($dataOnly) { |
|
152 | + $r[] = array( |
|
153 | + 'tag' => 'input', |
|
154 | + 'name' => Defaults::$httpMethodOverrideProperty, |
|
155 | + 'type' => 'hidden', |
|
156 | + 'value' => 'method', |
|
157 | + ); |
|
158 | + } else { |
|
159 | + $r[] = T::input() |
|
160 | + ->name(Defaults::$httpMethodOverrideProperty) |
|
161 | + ->value($method) |
|
162 | + ->type('hidden'); |
|
163 | + } |
|
164 | 164 | |
165 | - $method = 'POST'; |
|
166 | - } |
|
167 | - if (session_id() != '') { |
|
168 | - $form_key = static::key($method, $action); |
|
169 | - if ($dataOnly) { |
|
170 | - $r[] = array( |
|
171 | - 'tag' => 'input', |
|
172 | - 'name' => static::FORM_KEY, |
|
173 | - 'type' => 'hidden', |
|
174 | - 'value' => 'hidden', |
|
175 | - ); |
|
176 | - } else { |
|
177 | - $key = T::input() |
|
178 | - ->name(static::FORM_KEY) |
|
179 | - ->type('hidden') |
|
180 | - ->value($form_key); |
|
181 | - $r[] = $key; |
|
182 | - } |
|
183 | - } |
|
165 | + $method = 'POST'; |
|
166 | + } |
|
167 | + if (session_id() != '') { |
|
168 | + $form_key = static::key($method, $action); |
|
169 | + if ($dataOnly) { |
|
170 | + $r[] = array( |
|
171 | + 'tag' => 'input', |
|
172 | + 'name' => static::FORM_KEY, |
|
173 | + 'type' => 'hidden', |
|
174 | + 'value' => 'hidden', |
|
175 | + ); |
|
176 | + } else { |
|
177 | + $key = T::input() |
|
178 | + ->name(static::FORM_KEY) |
|
179 | + ->type('hidden') |
|
180 | + ->value($form_key); |
|
181 | + $r[] = $key; |
|
182 | + } |
|
183 | + } |
|
184 | 184 | |
185 | - $s = array( |
|
186 | - 'tag' => 'button', |
|
187 | - 'type' => 'submit', |
|
188 | - 'label' => |
|
189 | - Util::nestedValue($m, 'return', CommentParser::$embeddedDataName, 'label') |
|
190 | - ?: 'Submit' |
|
191 | - ); |
|
185 | + $s = array( |
|
186 | + 'tag' => 'button', |
|
187 | + 'type' => 'submit', |
|
188 | + 'label' => |
|
189 | + Util::nestedValue($m, 'return', CommentParser::$embeddedDataName, 'label') |
|
190 | + ?: 'Submit' |
|
191 | + ); |
|
192 | 192 | |
193 | - if (!$dataOnly) { |
|
194 | - $s = Emmet::make(static::style('submit', $m), $s); |
|
195 | - } |
|
196 | - $r[] = $s; |
|
197 | - $t = array( |
|
198 | - 'action' => $restler->getBaseUrl() . '/' . rtrim($action, '/'), |
|
199 | - 'method' => $method, |
|
200 | - ); |
|
201 | - if (static::$fileUpload) { |
|
202 | - static::$fileUpload = false; |
|
203 | - $t['enctype'] = 'multipart/form-data'; |
|
204 | - } |
|
205 | - if (isset($m[CommentParser::$embeddedDataName])) { |
|
206 | - $t += $m[CommentParser::$embeddedDataName]; |
|
207 | - } |
|
208 | - if (!$dataOnly) { |
|
209 | - $t = Emmet::make(static::style('form', $m), $t); |
|
210 | - $t->prefix = $prefix; |
|
211 | - $t->indent = $indent; |
|
212 | - $t[] = $r; |
|
213 | - } else { |
|
214 | - $t['fields'] = $r; |
|
215 | - } |
|
216 | - return $t; |
|
217 | - } |
|
193 | + if (!$dataOnly) { |
|
194 | + $s = Emmet::make(static::style('submit', $m), $s); |
|
195 | + } |
|
196 | + $r[] = $s; |
|
197 | + $t = array( |
|
198 | + 'action' => $restler->getBaseUrl() . '/' . rtrim($action, '/'), |
|
199 | + 'method' => $method, |
|
200 | + ); |
|
201 | + if (static::$fileUpload) { |
|
202 | + static::$fileUpload = false; |
|
203 | + $t['enctype'] = 'multipart/form-data'; |
|
204 | + } |
|
205 | + if (isset($m[CommentParser::$embeddedDataName])) { |
|
206 | + $t += $m[CommentParser::$embeddedDataName]; |
|
207 | + } |
|
208 | + if (!$dataOnly) { |
|
209 | + $t = Emmet::make(static::style('form', $m), $t); |
|
210 | + $t->prefix = $prefix; |
|
211 | + $t->indent = $indent; |
|
212 | + $t[] = $r; |
|
213 | + } else { |
|
214 | + $t['fields'] = $r; |
|
215 | + } |
|
216 | + return $t; |
|
217 | + } |
|
218 | 218 | |
219 | - public static function style($name, array $metadata, $type = '') |
|
220 | - { |
|
221 | - if (isset($metadata[CommentParser::$embeddedDataName][$name])) { |
|
222 | - return $metadata[CommentParser::$embeddedDataName][$name]; |
|
223 | - } |
|
224 | - $style = static::$style . '::' . $name; |
|
225 | - $typedStyle = $style . '_' . $type; |
|
226 | - if (defined($typedStyle)) { |
|
227 | - return constant($typedStyle); |
|
228 | - } |
|
229 | - if (defined($style)) { |
|
230 | - return constant($style); |
|
231 | - } |
|
232 | - return null; |
|
233 | - } |
|
219 | + public static function style($name, array $metadata, $type = '') |
|
220 | + { |
|
221 | + if (isset($metadata[CommentParser::$embeddedDataName][$name])) { |
|
222 | + return $metadata[CommentParser::$embeddedDataName][$name]; |
|
223 | + } |
|
224 | + $style = static::$style . '::' . $name; |
|
225 | + $typedStyle = $style . '_' . $type; |
|
226 | + if (defined($typedStyle)) { |
|
227 | + return constant($typedStyle); |
|
228 | + } |
|
229 | + if (defined($style)) { |
|
230 | + return constant($style); |
|
231 | + } |
|
232 | + return null; |
|
233 | + } |
|
234 | 234 | |
235 | - public static function fields($dataOnly = false) |
|
236 | - { |
|
237 | - $m = static::$info->metadata; |
|
238 | - $params = $m['param']; |
|
239 | - $values = static::$info->parameters; |
|
240 | - $r = array(); |
|
241 | - foreach ($params as $k => $p) { |
|
242 | - $value = Util::nestedValue($values, $k); |
|
243 | - if ( |
|
244 | - is_scalar($value) || |
|
245 | - ($p['type'] == 'array' && is_array($value) && $value == array_values($value)) || |
|
246 | - is_object($value) && $p['type'] == get_class($value) |
|
247 | - ) { |
|
248 | - $p['value'] = $value; |
|
249 | - } |
|
250 | - static::$validationInfo = $v = new ValidationInfo($p); |
|
251 | - if ($v->from == 'path') { |
|
252 | - continue; |
|
253 | - } |
|
254 | - if (!empty($v->children)) { |
|
255 | - $t = Emmet::make(static::style('fieldset', $m), array('label' => $v->label)); |
|
256 | - foreach ($v->children as $n => $c) { |
|
257 | - $value = Util::nestedValue($v->value, $n); |
|
258 | - if ( |
|
259 | - is_scalar($value) || |
|
260 | - ($c['type'] == 'array' && is_array($value) && $value == array_values($value)) || |
|
261 | - is_object($value) && $c['type'] == get_class($value) |
|
262 | - ) { |
|
263 | - $c['value'] = $value; |
|
264 | - } |
|
265 | - static::$validationInfo = $vc = new ValidationInfo($c); |
|
266 | - if ($vc->from == 'path') { |
|
267 | - continue; |
|
268 | - } |
|
269 | - $vc->name = $v->name . '[' . $vc->name . ']'; |
|
270 | - $t [] = static::field($vc, $dataOnly); |
|
271 | - } |
|
272 | - $r[] = $t; |
|
273 | - static::$validationInfo = null; |
|
274 | - } else { |
|
275 | - $f = static::field($v, $dataOnly); |
|
276 | - $r [] = $f; |
|
277 | - } |
|
278 | - static::$validationInfo = null; |
|
279 | - } |
|
280 | - return $r; |
|
281 | - } |
|
235 | + public static function fields($dataOnly = false) |
|
236 | + { |
|
237 | + $m = static::$info->metadata; |
|
238 | + $params = $m['param']; |
|
239 | + $values = static::$info->parameters; |
|
240 | + $r = array(); |
|
241 | + foreach ($params as $k => $p) { |
|
242 | + $value = Util::nestedValue($values, $k); |
|
243 | + if ( |
|
244 | + is_scalar($value) || |
|
245 | + ($p['type'] == 'array' && is_array($value) && $value == array_values($value)) || |
|
246 | + is_object($value) && $p['type'] == get_class($value) |
|
247 | + ) { |
|
248 | + $p['value'] = $value; |
|
249 | + } |
|
250 | + static::$validationInfo = $v = new ValidationInfo($p); |
|
251 | + if ($v->from == 'path') { |
|
252 | + continue; |
|
253 | + } |
|
254 | + if (!empty($v->children)) { |
|
255 | + $t = Emmet::make(static::style('fieldset', $m), array('label' => $v->label)); |
|
256 | + foreach ($v->children as $n => $c) { |
|
257 | + $value = Util::nestedValue($v->value, $n); |
|
258 | + if ( |
|
259 | + is_scalar($value) || |
|
260 | + ($c['type'] == 'array' && is_array($value) && $value == array_values($value)) || |
|
261 | + is_object($value) && $c['type'] == get_class($value) |
|
262 | + ) { |
|
263 | + $c['value'] = $value; |
|
264 | + } |
|
265 | + static::$validationInfo = $vc = new ValidationInfo($c); |
|
266 | + if ($vc->from == 'path') { |
|
267 | + continue; |
|
268 | + } |
|
269 | + $vc->name = $v->name . '[' . $vc->name . ']'; |
|
270 | + $t [] = static::field($vc, $dataOnly); |
|
271 | + } |
|
272 | + $r[] = $t; |
|
273 | + static::$validationInfo = null; |
|
274 | + } else { |
|
275 | + $f = static::field($v, $dataOnly); |
|
276 | + $r [] = $f; |
|
277 | + } |
|
278 | + static::$validationInfo = null; |
|
279 | + } |
|
280 | + return $r; |
|
281 | + } |
|
282 | 282 | |
283 | - /** |
|
284 | - * @param ValidationInfo $p |
|
285 | - * |
|
286 | - * @param bool $dataOnly |
|
287 | - * |
|
288 | - * @return array|T |
|
289 | - */ |
|
290 | - public static function field(ValidationInfo $p, $dataOnly = false) |
|
291 | - { |
|
292 | - if (is_string($p->value)) { |
|
293 | - //prevent XSS attacks |
|
294 | - $p->value = htmlspecialchars($p->value, ENT_QUOTES | ENT_HTML401, 'UTF-8'); |
|
295 | - } |
|
296 | - $type = $p->field ?: static::guessFieldType($p); |
|
297 | - $tag = in_array($type, static::$inputTypes) |
|
298 | - ? 'input' : $type; |
|
299 | - $options = array(); |
|
300 | - $name = $p->name; |
|
301 | - $multiple = null; |
|
302 | - if ($p->type == 'array' && $p->contentType != 'associative') { |
|
303 | - $name .= '[]'; |
|
304 | - $multiple = true; |
|
305 | - } |
|
306 | - if ($p->choice) { |
|
307 | - foreach ($p->choice as $i => $choice) { |
|
308 | - $option = array('name' => $name, 'value' => $choice); |
|
309 | - $option['text'] = isset($p->rules['select'][$i]) |
|
310 | - ? $p->rules['select'][$i] |
|
311 | - : $choice; |
|
312 | - if ($choice == $p->value) { |
|
313 | - $option['selected'] = true; |
|
314 | - } |
|
315 | - $options[] = $option; |
|
316 | - } |
|
317 | - } elseif ($p->type == 'boolean' || $p->type == 'bool') { |
|
318 | - if (Text::beginsWith($type, 'radio') || Text::beginsWith($type, 'select')) { |
|
319 | - $options[] = array( |
|
320 | - 'name' => $p->name, |
|
321 | - 'text' => ' Yes ', |
|
322 | - 'value' => 'true' |
|
323 | - ); |
|
324 | - $options[] = array( |
|
325 | - 'name' => $p->name, |
|
326 | - 'text' => ' No ', |
|
327 | - 'value' => 'false' |
|
328 | - ); |
|
329 | - if ($p->value || $p->default) { |
|
330 | - $options[0]['selected'] = true; |
|
331 | - } |
|
332 | - } else { //checkbox |
|
333 | - $r = array( |
|
334 | - 'tag' => $tag, |
|
335 | - 'name' => $name, |
|
336 | - 'type' => $type, |
|
337 | - 'label' => $p->label, |
|
338 | - 'value' => 'true', |
|
339 | - 'default' => $p->default, |
|
340 | - ); |
|
341 | - $r['text'] = 'Yes'; |
|
342 | - if ($p->default) { |
|
343 | - $r['selected'] = true; |
|
344 | - } |
|
345 | - if (isset($p->rules)) { |
|
346 | - $r += $p->rules; |
|
347 | - } |
|
348 | - } |
|
349 | - } |
|
350 | - if (empty($r)) { |
|
351 | - $r = array( |
|
352 | - 'tag' => $tag, |
|
353 | - 'name' => $name, |
|
354 | - 'type' => $type, |
|
355 | - 'label' => $p->label, |
|
356 | - 'value' => $p->value, |
|
357 | - 'default' => $p->default, |
|
358 | - 'options' => & $options, |
|
359 | - 'multiple' => $multiple, |
|
360 | - ); |
|
361 | - if (isset($p->rules)) { |
|
362 | - $r += $p->rules; |
|
363 | - } |
|
364 | - } |
|
365 | - if ($type == 'file') { |
|
366 | - static::$fileUpload = true; |
|
367 | - if (empty($r['accept'])) { |
|
368 | - $r['accept'] = implode(', ', UploadFormat::$allowedMimeTypes); |
|
369 | - } |
|
370 | - } |
|
371 | - if (!empty(Validator::$exceptions[$name]) && static::$info->url == Scope::get('Restler')->url) { |
|
372 | - $r['error'] = 'has-error'; |
|
373 | - $r['message'] = Validator::$exceptions[$p->name]->getMessage(); |
|
374 | - } |
|
283 | + /** |
|
284 | + * @param ValidationInfo $p |
|
285 | + * |
|
286 | + * @param bool $dataOnly |
|
287 | + * |
|
288 | + * @return array|T |
|
289 | + */ |
|
290 | + public static function field(ValidationInfo $p, $dataOnly = false) |
|
291 | + { |
|
292 | + if (is_string($p->value)) { |
|
293 | + //prevent XSS attacks |
|
294 | + $p->value = htmlspecialchars($p->value, ENT_QUOTES | ENT_HTML401, 'UTF-8'); |
|
295 | + } |
|
296 | + $type = $p->field ?: static::guessFieldType($p); |
|
297 | + $tag = in_array($type, static::$inputTypes) |
|
298 | + ? 'input' : $type; |
|
299 | + $options = array(); |
|
300 | + $name = $p->name; |
|
301 | + $multiple = null; |
|
302 | + if ($p->type == 'array' && $p->contentType != 'associative') { |
|
303 | + $name .= '[]'; |
|
304 | + $multiple = true; |
|
305 | + } |
|
306 | + if ($p->choice) { |
|
307 | + foreach ($p->choice as $i => $choice) { |
|
308 | + $option = array('name' => $name, 'value' => $choice); |
|
309 | + $option['text'] = isset($p->rules['select'][$i]) |
|
310 | + ? $p->rules['select'][$i] |
|
311 | + : $choice; |
|
312 | + if ($choice == $p->value) { |
|
313 | + $option['selected'] = true; |
|
314 | + } |
|
315 | + $options[] = $option; |
|
316 | + } |
|
317 | + } elseif ($p->type == 'boolean' || $p->type == 'bool') { |
|
318 | + if (Text::beginsWith($type, 'radio') || Text::beginsWith($type, 'select')) { |
|
319 | + $options[] = array( |
|
320 | + 'name' => $p->name, |
|
321 | + 'text' => ' Yes ', |
|
322 | + 'value' => 'true' |
|
323 | + ); |
|
324 | + $options[] = array( |
|
325 | + 'name' => $p->name, |
|
326 | + 'text' => ' No ', |
|
327 | + 'value' => 'false' |
|
328 | + ); |
|
329 | + if ($p->value || $p->default) { |
|
330 | + $options[0]['selected'] = true; |
|
331 | + } |
|
332 | + } else { //checkbox |
|
333 | + $r = array( |
|
334 | + 'tag' => $tag, |
|
335 | + 'name' => $name, |
|
336 | + 'type' => $type, |
|
337 | + 'label' => $p->label, |
|
338 | + 'value' => 'true', |
|
339 | + 'default' => $p->default, |
|
340 | + ); |
|
341 | + $r['text'] = 'Yes'; |
|
342 | + if ($p->default) { |
|
343 | + $r['selected'] = true; |
|
344 | + } |
|
345 | + if (isset($p->rules)) { |
|
346 | + $r += $p->rules; |
|
347 | + } |
|
348 | + } |
|
349 | + } |
|
350 | + if (empty($r)) { |
|
351 | + $r = array( |
|
352 | + 'tag' => $tag, |
|
353 | + 'name' => $name, |
|
354 | + 'type' => $type, |
|
355 | + 'label' => $p->label, |
|
356 | + 'value' => $p->value, |
|
357 | + 'default' => $p->default, |
|
358 | + 'options' => & $options, |
|
359 | + 'multiple' => $multiple, |
|
360 | + ); |
|
361 | + if (isset($p->rules)) { |
|
362 | + $r += $p->rules; |
|
363 | + } |
|
364 | + } |
|
365 | + if ($type == 'file') { |
|
366 | + static::$fileUpload = true; |
|
367 | + if (empty($r['accept'])) { |
|
368 | + $r['accept'] = implode(', ', UploadFormat::$allowedMimeTypes); |
|
369 | + } |
|
370 | + } |
|
371 | + if (!empty(Validator::$exceptions[$name]) && static::$info->url == Scope::get('Restler')->url) { |
|
372 | + $r['error'] = 'has-error'; |
|
373 | + $r['message'] = Validator::$exceptions[$p->name]->getMessage(); |
|
374 | + } |
|
375 | 375 | |
376 | - if (true === $p->required) { |
|
377 | - $r['required'] = 'required'; |
|
378 | - } |
|
379 | - if (isset($p->rules['autofocus'])) { |
|
380 | - $r['autofocus'] = 'autofocus'; |
|
381 | - } |
|
382 | - /* |
|
376 | + if (true === $p->required) { |
|
377 | + $r['required'] = 'required'; |
|
378 | + } |
|
379 | + if (isset($p->rules['autofocus'])) { |
|
380 | + $r['autofocus'] = 'autofocus'; |
|
381 | + } |
|
382 | + /* |
|
383 | 383 | echo "<pre>"; |
384 | 384 | print_r($r); |
385 | 385 | echo "</pre>"; |
386 | 386 | */ |
387 | - if ($dataOnly) { |
|
388 | - return $r; |
|
389 | - } |
|
390 | - if (isset($p->rules['form'])) { |
|
391 | - return Emmet::make($p->rules['form'], $r); |
|
392 | - } |
|
393 | - $m = static::$info->metadata; |
|
394 | - $t = Emmet::make(static::style($type, $m, $p->type) ?: static::style($tag, $m, $p->type), $r); |
|
395 | - return $t; |
|
396 | - } |
|
387 | + if ($dataOnly) { |
|
388 | + return $r; |
|
389 | + } |
|
390 | + if (isset($p->rules['form'])) { |
|
391 | + return Emmet::make($p->rules['form'], $r); |
|
392 | + } |
|
393 | + $m = static::$info->metadata; |
|
394 | + $t = Emmet::make(static::style($type, $m, $p->type) ?: static::style($tag, $m, $p->type), $r); |
|
395 | + return $t; |
|
396 | + } |
|
397 | 397 | |
398 | - protected static function guessFieldType(ValidationInfo $p, $type = 'type') |
|
399 | - { |
|
400 | - if (in_array($p->$type, static::$inputTypes)) { |
|
401 | - return $p->$type; |
|
402 | - } |
|
403 | - if ($p->choice) { |
|
404 | - return $p->type == 'array' ? 'checkbox' : 'select'; |
|
405 | - } |
|
406 | - switch ($p->$type) { |
|
407 | - case 'boolean': |
|
408 | - return 'radio'; |
|
409 | - case 'int': |
|
410 | - case 'number': |
|
411 | - case 'float': |
|
412 | - return 'number'; |
|
413 | - case 'array': |
|
414 | - return static::guessFieldType($p, 'contentType'); |
|
415 | - } |
|
416 | - if ($p->name == 'password') { |
|
417 | - return 'password'; |
|
418 | - } |
|
419 | - return 'text'; |
|
420 | - } |
|
398 | + protected static function guessFieldType(ValidationInfo $p, $type = 'type') |
|
399 | + { |
|
400 | + if (in_array($p->$type, static::$inputTypes)) { |
|
401 | + return $p->$type; |
|
402 | + } |
|
403 | + if ($p->choice) { |
|
404 | + return $p->type == 'array' ? 'checkbox' : 'select'; |
|
405 | + } |
|
406 | + switch ($p->$type) { |
|
407 | + case 'boolean': |
|
408 | + return 'radio'; |
|
409 | + case 'int': |
|
410 | + case 'number': |
|
411 | + case 'float': |
|
412 | + return 'number'; |
|
413 | + case 'array': |
|
414 | + return static::guessFieldType($p, 'contentType'); |
|
415 | + } |
|
416 | + if ($p->name == 'password') { |
|
417 | + return 'password'; |
|
418 | + } |
|
419 | + return 'text'; |
|
420 | + } |
|
421 | 421 | |
422 | - /** |
|
423 | - * Get the form key |
|
424 | - * |
|
425 | - * @param string $method http method for form key |
|
426 | - * @param string $action relative path from the web root. When set to null |
|
427 | - * it uses the current api method's path |
|
428 | - * |
|
429 | - * @return string generated form key |
|
430 | - */ |
|
431 | - public static function key($method = 'POST', $action = null) |
|
432 | - { |
|
433 | - if (is_null($action)) { |
|
434 | - $action = Scope::get('Restler')->url; |
|
435 | - } |
|
436 | - $target = "$method $action"; |
|
437 | - if (empty(static::$key[$target])) { |
|
438 | - static::$key[$target] = md5($target . User::getIpAddress() . uniqid(mt_rand())); |
|
439 | - } |
|
440 | - $_SESSION[static::FORM_KEY] = static::$key; |
|
441 | - return static::$key[$target]; |
|
442 | - } |
|
422 | + /** |
|
423 | + * Get the form key |
|
424 | + * |
|
425 | + * @param string $method http method for form key |
|
426 | + * @param string $action relative path from the web root. When set to null |
|
427 | + * it uses the current api method's path |
|
428 | + * |
|
429 | + * @return string generated form key |
|
430 | + */ |
|
431 | + public static function key($method = 'POST', $action = null) |
|
432 | + { |
|
433 | + if (is_null($action)) { |
|
434 | + $action = Scope::get('Restler')->url; |
|
435 | + } |
|
436 | + $target = "$method $action"; |
|
437 | + if (empty(static::$key[$target])) { |
|
438 | + static::$key[$target] = md5($target . User::getIpAddress() . uniqid(mt_rand())); |
|
439 | + } |
|
440 | + $_SESSION[static::FORM_KEY] = static::$key; |
|
441 | + return static::$key[$target]; |
|
442 | + } |
|
443 | 443 | |
444 | - /** |
|
445 | - * Access verification method. |
|
446 | - * |
|
447 | - * API access will be denied when this method returns false |
|
448 | - * |
|
449 | - * @return boolean true when api access is allowed false otherwise |
|
450 | - * |
|
451 | - * @throws RestException 403 security violation |
|
452 | - */ |
|
453 | - public function __isAllowed() |
|
454 | - { |
|
455 | - if (session_id() == '') { |
|
456 | - session_start(); |
|
457 | - } |
|
458 | - /** @var Restler $restler */ |
|
459 | - $restler = $this->restler; |
|
460 | - $url = $restler->url; |
|
461 | - foreach (static::$excludedPaths as $exclude) { |
|
462 | - if (empty($exclude)) { |
|
463 | - if ($url == $exclude) { |
|
464 | - return true; |
|
465 | - } |
|
466 | - } elseif (Text::beginsWith($url, $exclude)) { |
|
467 | - return true; |
|
468 | - } |
|
469 | - } |
|
470 | - $check = static::$filterFormRequestsOnly |
|
471 | - ? $restler->requestFormat instanceof UrlEncodedFormat || $restler->requestFormat instanceof UploadFormat |
|
472 | - : true; |
|
473 | - if (!empty($_POST) && $check) { |
|
474 | - if ( |
|
475 | - isset($_POST[static::FORM_KEY]) && |
|
476 | - ($target = Util::getRequestMethod() . ' ' . $restler->url) && |
|
477 | - isset($_SESSION[static::FORM_KEY][$target]) && |
|
478 | - $_POST[static::FORM_KEY] == $_SESSION[static::FORM_KEY][$target] |
|
479 | - ) { |
|
480 | - return true; |
|
481 | - } |
|
482 | - throw new RestException(403, 'Insecure form submission'); |
|
483 | - } |
|
484 | - return true; |
|
485 | - } |
|
444 | + /** |
|
445 | + * Access verification method. |
|
446 | + * |
|
447 | + * API access will be denied when this method returns false |
|
448 | + * |
|
449 | + * @return boolean true when api access is allowed false otherwise |
|
450 | + * |
|
451 | + * @throws RestException 403 security violation |
|
452 | + */ |
|
453 | + public function __isAllowed() |
|
454 | + { |
|
455 | + if (session_id() == '') { |
|
456 | + session_start(); |
|
457 | + } |
|
458 | + /** @var Restler $restler */ |
|
459 | + $restler = $this->restler; |
|
460 | + $url = $restler->url; |
|
461 | + foreach (static::$excludedPaths as $exclude) { |
|
462 | + if (empty($exclude)) { |
|
463 | + if ($url == $exclude) { |
|
464 | + return true; |
|
465 | + } |
|
466 | + } elseif (Text::beginsWith($url, $exclude)) { |
|
467 | + return true; |
|
468 | + } |
|
469 | + } |
|
470 | + $check = static::$filterFormRequestsOnly |
|
471 | + ? $restler->requestFormat instanceof UrlEncodedFormat || $restler->requestFormat instanceof UploadFormat |
|
472 | + : true; |
|
473 | + if (!empty($_POST) && $check) { |
|
474 | + if ( |
|
475 | + isset($_POST[static::FORM_KEY]) && |
|
476 | + ($target = Util::getRequestMethod() . ' ' . $restler->url) && |
|
477 | + isset($_SESSION[static::FORM_KEY][$target]) && |
|
478 | + $_POST[static::FORM_KEY] == $_SESSION[static::FORM_KEY][$target] |
|
479 | + ) { |
|
480 | + return true; |
|
481 | + } |
|
482 | + throw new RestException(403, 'Insecure form submission'); |
|
483 | + } |
|
484 | + return true; |
|
485 | + } |
|
486 | 486 | } |
@@ -135,7 +135,7 @@ discard block |
||
135 | 135 | $info = false; |
136 | 136 | } |
137 | 137 | if (!$info) { |
138 | - throw new RestException(500, 'invalid action path for form `' . $method . ' ' . $action . '`'); |
|
138 | + throw new RestException(500, 'invalid action path for form `'.$method.' '.$action.'`'); |
|
139 | 139 | } |
140 | 140 | static::$info = $info; |
141 | 141 | $m = $info->metadata; |
@@ -144,7 +144,7 @@ discard block |
||
144 | 144 | if (empty(Defaults::$httpMethodOverrideProperty)) { |
145 | 145 | throw new RestException( |
146 | 146 | 500, |
147 | - 'Forms require `Defaults::\$httpMethodOverrideProperty`' . |
|
147 | + 'Forms require `Defaults::\$httpMethodOverrideProperty`'. |
|
148 | 148 | "for supporting HTTP $method" |
149 | 149 | ); |
150 | 150 | } |
@@ -195,7 +195,7 @@ discard block |
||
195 | 195 | } |
196 | 196 | $r[] = $s; |
197 | 197 | $t = array( |
198 | - 'action' => $restler->getBaseUrl() . '/' . rtrim($action, '/'), |
|
198 | + 'action' => $restler->getBaseUrl().'/'.rtrim($action, '/'), |
|
199 | 199 | 'method' => $method, |
200 | 200 | ); |
201 | 201 | if (static::$fileUpload) { |
@@ -221,8 +221,8 @@ discard block |
||
221 | 221 | if (isset($metadata[CommentParser::$embeddedDataName][$name])) { |
222 | 222 | return $metadata[CommentParser::$embeddedDataName][$name]; |
223 | 223 | } |
224 | - $style = static::$style . '::' . $name; |
|
225 | - $typedStyle = $style . '_' . $type; |
|
224 | + $style = static::$style.'::'.$name; |
|
225 | + $typedStyle = $style.'_'.$type; |
|
226 | 226 | if (defined($typedStyle)) { |
227 | 227 | return constant($typedStyle); |
228 | 228 | } |
@@ -266,7 +266,7 @@ discard block |
||
266 | 266 | if ($vc->from == 'path') { |
267 | 267 | continue; |
268 | 268 | } |
269 | - $vc->name = $v->name . '[' . $vc->name . ']'; |
|
269 | + $vc->name = $v->name.'['.$vc->name.']'; |
|
270 | 270 | $t [] = static::field($vc, $dataOnly); |
271 | 271 | } |
272 | 272 | $r[] = $t; |
@@ -435,7 +435,7 @@ discard block |
||
435 | 435 | } |
436 | 436 | $target = "$method $action"; |
437 | 437 | if (empty(static::$key[$target])) { |
438 | - static::$key[$target] = md5($target . User::getIpAddress() . uniqid(mt_rand())); |
|
438 | + static::$key[$target] = md5($target.User::getIpAddress().uniqid(mt_rand())); |
|
439 | 439 | } |
440 | 440 | $_SESSION[static::FORM_KEY] = static::$key; |
441 | 441 | return static::$key[$target]; |
@@ -473,7 +473,7 @@ discard block |
||
473 | 473 | if (!empty($_POST) && $check) { |
474 | 474 | if ( |
475 | 475 | isset($_POST[static::FORM_KEY]) && |
476 | - ($target = Util::getRequestMethod() . ' ' . $restler->url) && |
|
476 | + ($target = Util::getRequestMethod().' '.$restler->url) && |
|
477 | 477 | isset($_SESSION[static::FORM_KEY][$target]) && |
478 | 478 | $_POST[static::FORM_KEY] == $_SESSION[static::FORM_KEY][$target] |
479 | 479 | ) { |
@@ -12,367 +12,367 @@ discard block |
||
12 | 12 | */ |
13 | 13 | class Emmet |
14 | 14 | { |
15 | - const DELIMITERS = '.#*>+^[=" ]{$@-#}'; |
|
15 | + const DELIMITERS = '.#*>+^[=" ]{$@-#}'; |
|
16 | 16 | |
17 | - /** |
|
18 | - * Create the needed tag hierarchy from emmet string |
|
19 | - * |
|
20 | - * @param string $string |
|
21 | - * |
|
22 | - * @param array|string $data |
|
23 | - * |
|
24 | - * @return array|T |
|
25 | - */ |
|
26 | - public static function make($string, $data = null) |
|
27 | - { |
|
28 | - if (!strlen($string)) |
|
29 | - return array(); |
|
17 | + /** |
|
18 | + * Create the needed tag hierarchy from emmet string |
|
19 | + * |
|
20 | + * @param string $string |
|
21 | + * |
|
22 | + * @param array|string $data |
|
23 | + * |
|
24 | + * @return array|T |
|
25 | + */ |
|
26 | + public static function make($string, $data = null) |
|
27 | + { |
|
28 | + if (!strlen($string)) |
|
29 | + return array(); |
|
30 | 30 | |
31 | - $implicitTag = |
|
32 | - function () use (& $tag) { |
|
33 | - if (empty($tag->tag)) { |
|
34 | - switch ($tag->parent->tag) { |
|
35 | - case 'ul': |
|
36 | - case 'ol': |
|
37 | - $tag->tag = 'li'; |
|
38 | - break; |
|
39 | - case 'em': |
|
40 | - $tag->tag = 'span'; |
|
41 | - break; |
|
42 | - case 'table': |
|
43 | - case 'tbody': |
|
44 | - case 'thead': |
|
45 | - case 'tfoot': |
|
46 | - $tag->tag = 'tr'; |
|
47 | - break; |
|
48 | - case 'tr': |
|
49 | - $tag->tag = 'td'; |
|
50 | - break; |
|
51 | - case 'select': |
|
52 | - case 'optgroup': |
|
53 | - $tag->tag = 'option'; |
|
54 | - break; |
|
55 | - default: |
|
56 | - $tag->tag = 'div'; |
|
57 | - } |
|
58 | - } |
|
59 | - }; |
|
31 | + $implicitTag = |
|
32 | + function () use (& $tag) { |
|
33 | + if (empty($tag->tag)) { |
|
34 | + switch ($tag->parent->tag) { |
|
35 | + case 'ul': |
|
36 | + case 'ol': |
|
37 | + $tag->tag = 'li'; |
|
38 | + break; |
|
39 | + case 'em': |
|
40 | + $tag->tag = 'span'; |
|
41 | + break; |
|
42 | + case 'table': |
|
43 | + case 'tbody': |
|
44 | + case 'thead': |
|
45 | + case 'tfoot': |
|
46 | + $tag->tag = 'tr'; |
|
47 | + break; |
|
48 | + case 'tr': |
|
49 | + $tag->tag = 'td'; |
|
50 | + break; |
|
51 | + case 'select': |
|
52 | + case 'optgroup': |
|
53 | + $tag->tag = 'option'; |
|
54 | + break; |
|
55 | + default: |
|
56 | + $tag->tag = 'div'; |
|
57 | + } |
|
58 | + } |
|
59 | + }; |
|
60 | 60 | |
61 | - $parseText = |
|
62 | - function ( |
|
63 | - $text, $round, $total, $data, $delimiter = null |
|
64 | - ) |
|
65 | - use ( |
|
66 | - & $tokens, & $tag |
|
67 | - ) { |
|
68 | - $digits = 0; |
|
69 | - if ($delimiter == null) |
|
70 | - $delimiter = array( |
|
71 | - '.' => true, |
|
72 | - '#' => true, |
|
73 | - '*' => true, |
|
74 | - '>' => true, |
|
75 | - '+' => true, |
|
76 | - '^' => true, |
|
77 | - '[' => true, |
|
78 | - ']' => true, |
|
79 | - '=' => true, |
|
80 | - ); |
|
81 | - while (!empty($tokens) && |
|
82 | - !isset($delimiter[$t = array_shift($tokens)])) { |
|
83 | - while ('$' === $t) { |
|
84 | - $digits++; |
|
85 | - $t = array_shift($tokens); |
|
86 | - } |
|
87 | - if ($digits) { |
|
88 | - $negative = false; |
|
89 | - $offset = 0; |
|
90 | - if ('@' == $t) { |
|
91 | - if ('-' == ($t = array_shift($tokens))) { |
|
92 | - $negative = true; |
|
93 | - if (is_numeric(reset($tokens))) { |
|
94 | - $offset = array_shift($tokens); |
|
95 | - } |
|
96 | - } elseif (is_numeric($t)) { |
|
97 | - $offset = $t; |
|
98 | - } else { |
|
99 | - array_unshift($tokens, $t); |
|
100 | - } |
|
101 | - } elseif ('#' == ($h = array_shift($tokens))) { |
|
102 | - if (!empty($t)) { |
|
103 | - $data = Util::nestedValue($data, $t); |
|
104 | - if (is_null($data)) { |
|
105 | - return null; |
|
106 | - } |
|
107 | - } |
|
108 | - if (is_numeric($data)) { |
|
109 | - $text .= sprintf("%0{$digits}d", (int)$data); |
|
110 | - } elseif (is_string($data)) { |
|
111 | - $text .= $data; |
|
112 | - } |
|
113 | - $digits = 0; |
|
114 | - continue; |
|
115 | - } else { |
|
116 | - array_unshift($tokens, $t, $h); |
|
117 | - } |
|
118 | - if ($negative) { |
|
119 | - $n = $total + 1 - $round + $offset; |
|
120 | - } else { |
|
121 | - $n = $round + $offset; |
|
122 | - } |
|
123 | - $text .= sprintf("%0{$digits}d", $n); |
|
124 | - $digits = 0; |
|
125 | - } else { |
|
126 | - $text .= $t; |
|
127 | - } |
|
128 | - } |
|
129 | - if (isset($t)) |
|
130 | - array_unshift($tokens, $t); |
|
131 | - return $text; |
|
132 | - }; |
|
61 | + $parseText = |
|
62 | + function ( |
|
63 | + $text, $round, $total, $data, $delimiter = null |
|
64 | + ) |
|
65 | + use ( |
|
66 | + & $tokens, & $tag |
|
67 | + ) { |
|
68 | + $digits = 0; |
|
69 | + if ($delimiter == null) |
|
70 | + $delimiter = array( |
|
71 | + '.' => true, |
|
72 | + '#' => true, |
|
73 | + '*' => true, |
|
74 | + '>' => true, |
|
75 | + '+' => true, |
|
76 | + '^' => true, |
|
77 | + '[' => true, |
|
78 | + ']' => true, |
|
79 | + '=' => true, |
|
80 | + ); |
|
81 | + while (!empty($tokens) && |
|
82 | + !isset($delimiter[$t = array_shift($tokens)])) { |
|
83 | + while ('$' === $t) { |
|
84 | + $digits++; |
|
85 | + $t = array_shift($tokens); |
|
86 | + } |
|
87 | + if ($digits) { |
|
88 | + $negative = false; |
|
89 | + $offset = 0; |
|
90 | + if ('@' == $t) { |
|
91 | + if ('-' == ($t = array_shift($tokens))) { |
|
92 | + $negative = true; |
|
93 | + if (is_numeric(reset($tokens))) { |
|
94 | + $offset = array_shift($tokens); |
|
95 | + } |
|
96 | + } elseif (is_numeric($t)) { |
|
97 | + $offset = $t; |
|
98 | + } else { |
|
99 | + array_unshift($tokens, $t); |
|
100 | + } |
|
101 | + } elseif ('#' == ($h = array_shift($tokens))) { |
|
102 | + if (!empty($t)) { |
|
103 | + $data = Util::nestedValue($data, $t); |
|
104 | + if (is_null($data)) { |
|
105 | + return null; |
|
106 | + } |
|
107 | + } |
|
108 | + if (is_numeric($data)) { |
|
109 | + $text .= sprintf("%0{$digits}d", (int)$data); |
|
110 | + } elseif (is_string($data)) { |
|
111 | + $text .= $data; |
|
112 | + } |
|
113 | + $digits = 0; |
|
114 | + continue; |
|
115 | + } else { |
|
116 | + array_unshift($tokens, $t, $h); |
|
117 | + } |
|
118 | + if ($negative) { |
|
119 | + $n = $total + 1 - $round + $offset; |
|
120 | + } else { |
|
121 | + $n = $round + $offset; |
|
122 | + } |
|
123 | + $text .= sprintf("%0{$digits}d", $n); |
|
124 | + $digits = 0; |
|
125 | + } else { |
|
126 | + $text .= $t; |
|
127 | + } |
|
128 | + } |
|
129 | + if (isset($t)) |
|
130 | + array_unshift($tokens, $t); |
|
131 | + return $text; |
|
132 | + }; |
|
133 | 133 | |
134 | - $parseAttributes = |
|
135 | - function (Callable $self, $round, $total, $data) |
|
136 | - use (& $tokens, & $tag, $parseText) { |
|
137 | - $a = $parseText( |
|
138 | - '', $round, $total, $data |
|
139 | - ); |
|
140 | - if (is_null($a)) |
|
141 | - return; |
|
142 | - if ('=' == ($v = array_shift($tokens))) { |
|
143 | - //value |
|
144 | - if ('"' == ($v = array_shift($tokens))) { |
|
145 | - $text = ''; |
|
146 | - $tag->$a($parseText( |
|
147 | - $text, $round, $total, $data, |
|
148 | - array('"' => true) |
|
149 | - )); |
|
150 | - } else { |
|
151 | - array_unshift($tokens, $v); |
|
152 | - $text = ''; |
|
153 | - $tag->$a($parseText( |
|
154 | - $text, $round, $total, $data, |
|
155 | - array(' ' => true, ']' => true) |
|
156 | - )); |
|
157 | - } |
|
158 | - if (' ' == ($v = array_shift($tokens))) { |
|
159 | - $self($self, $round, $total, $data); |
|
160 | - } |
|
161 | - } elseif (']' == $v) { |
|
162 | - //end |
|
163 | - $tag->$a(''); |
|
164 | - return; |
|
165 | - } elseif (' ' == $v) { |
|
166 | - $tag->$a(''); |
|
167 | - $self($self, $round, $total, $data); |
|
168 | - } |
|
169 | - }; |
|
134 | + $parseAttributes = |
|
135 | + function (Callable $self, $round, $total, $data) |
|
136 | + use (& $tokens, & $tag, $parseText) { |
|
137 | + $a = $parseText( |
|
138 | + '', $round, $total, $data |
|
139 | + ); |
|
140 | + if (is_null($a)) |
|
141 | + return; |
|
142 | + if ('=' == ($v = array_shift($tokens))) { |
|
143 | + //value |
|
144 | + if ('"' == ($v = array_shift($tokens))) { |
|
145 | + $text = ''; |
|
146 | + $tag->$a($parseText( |
|
147 | + $text, $round, $total, $data, |
|
148 | + array('"' => true) |
|
149 | + )); |
|
150 | + } else { |
|
151 | + array_unshift($tokens, $v); |
|
152 | + $text = ''; |
|
153 | + $tag->$a($parseText( |
|
154 | + $text, $round, $total, $data, |
|
155 | + array(' ' => true, ']' => true) |
|
156 | + )); |
|
157 | + } |
|
158 | + if (' ' == ($v = array_shift($tokens))) { |
|
159 | + $self($self, $round, $total, $data); |
|
160 | + } |
|
161 | + } elseif (']' == $v) { |
|
162 | + //end |
|
163 | + $tag->$a(''); |
|
164 | + return; |
|
165 | + } elseif (' ' == $v) { |
|
166 | + $tag->$a(''); |
|
167 | + $self($self, $round, $total, $data); |
|
168 | + } |
|
169 | + }; |
|
170 | 170 | |
171 | - $tokens = static::tokenize($string); |
|
172 | - $tag = new T(array_shift($tokens)); |
|
173 | - $parent = $root = new T; |
|
171 | + $tokens = static::tokenize($string); |
|
172 | + $tag = new T(array_shift($tokens)); |
|
173 | + $parent = $root = new T; |
|
174 | 174 | |
175 | - $parse = |
|
176 | - function ( |
|
177 | - Callable $self, $round = 1, $total = 1 |
|
178 | - ) |
|
179 | - use ( |
|
180 | - & $tokens, & $parent, & $tag, & $data, |
|
181 | - $parseAttributes, $implicitTag, $parseText |
|
182 | - ) { |
|
183 | - $offsetTokens = null; |
|
184 | - $parent[] = $tag; |
|
185 | - $isInChild = false; |
|
186 | - while ($tokens) { |
|
187 | - switch (array_shift($tokens)) { |
|
188 | - //class |
|
189 | - case '.': |
|
190 | - $offsetTokens = array_values($tokens); |
|
191 | - array_unshift($offsetTokens, '.'); |
|
192 | - $implicitTag(); |
|
193 | - $e = array_filter(explode(' ', $tag->class)); |
|
194 | - $e[] = $parseText('', $round, $total, $data); |
|
195 | - $tag->class(implode(' ', array_unique($e))); |
|
196 | - break; |
|
197 | - //id |
|
198 | - case '#': |
|
199 | - $offsetTokens = array_values($tokens); |
|
200 | - array_unshift($offsetTokens, '#'); |
|
201 | - $implicitTag(); |
|
202 | - $tag->id( |
|
203 | - $parseText( |
|
204 | - array_shift($tokens), $round, $total, $data |
|
205 | - ) |
|
206 | - ); |
|
207 | - break; |
|
208 | - //attributes |
|
209 | - case '[': |
|
210 | - $offsetTokens = array_values($tokens); |
|
211 | - array_unshift($offsetTokens, '['); |
|
212 | - $implicitTag(); |
|
213 | - $parseAttributes( |
|
214 | - $parseAttributes, $round, $total, $data |
|
215 | - ); |
|
216 | - break; |
|
217 | - //child |
|
218 | - case '{': |
|
219 | - $text = ''; |
|
220 | - $tag[] = $parseText( |
|
221 | - $text, $round, $total, $data, array('}' => true) |
|
222 | - ); |
|
223 | - break; |
|
224 | - case '>': |
|
225 | - $isInChild = true; |
|
226 | - $offsetTokens = null; |
|
227 | - if ('{' == ($t = array_shift($tokens))) { |
|
228 | - array_unshift($tokens, $t); |
|
229 | - $child = new T(); |
|
230 | - $tag[] = $child; |
|
231 | - $parent = $tag; |
|
232 | - $tag = $child; |
|
233 | - } elseif ('[' == $t) { |
|
234 | - array_unshift($tokens, $t); |
|
235 | - } else { |
|
236 | - $child = new T($t); |
|
237 | - $tag[] = $child; |
|
238 | - $parent = $tag; |
|
239 | - $tag = $child; |
|
240 | - } |
|
241 | - break; |
|
242 | - //sibling |
|
243 | - case '+': |
|
244 | - $offsetTokens = null; |
|
245 | - if (!$isInChild && $round != $total) { |
|
246 | - $tokens = array(); |
|
247 | - break; |
|
248 | - } |
|
249 | - if ('{' == ($t = array_shift($tokens))) { |
|
250 | - $tag = $tag->parent; |
|
251 | - array_unshift($tokens, $t); |
|
252 | - break; |
|
253 | - } elseif ('[' == $t) { |
|
254 | - array_unshift($tokens, $t); |
|
255 | - } else { |
|
256 | - $child = new T($t); |
|
257 | - $tag = $tag->parent; |
|
258 | - $tag[] = $child; |
|
259 | - $tag = $child; |
|
260 | - } |
|
261 | - break; |
|
262 | - //sibling of parent |
|
263 | - case '^': |
|
264 | - if ($round != $total) { |
|
265 | - $tokens = array(); |
|
266 | - break; |
|
267 | - } |
|
268 | - $tag = $tag->parent; |
|
269 | - if ($tag->parent) |
|
270 | - $tag = $tag->parent; |
|
271 | - while ('^' == ($t = array_shift($tokens))) { |
|
272 | - if ($tag->parent) |
|
273 | - $tag = $tag->parent; |
|
274 | - } |
|
275 | - $child = new T($t); |
|
276 | - $tag[] = $child; |
|
277 | - $tag = $child; |
|
278 | - break; |
|
279 | - //clone |
|
280 | - case '*': |
|
281 | - $times = array_shift($tokens); |
|
282 | - $removeCount = 2; |
|
283 | - $delimiter = array( |
|
284 | - '.' => true, |
|
285 | - '#' => true, |
|
286 | - '*' => true, |
|
287 | - '>' => true, |
|
288 | - '+' => true, |
|
289 | - '^' => true, |
|
290 | - '[' => true, |
|
291 | - ']' => true, |
|
292 | - '=' => true, |
|
293 | - ); |
|
294 | - if (!is_numeric($times)) { |
|
295 | - if (is_string($times)) { |
|
296 | - if (!isset($delimiter[$times])) { |
|
297 | - $data = Util::nestedValue($data, $times) |
|
298 | - ? : $data; |
|
299 | - } else { |
|
300 | - array_unshift($tokens, $times); |
|
301 | - $removeCount = 1; |
|
302 | - } |
|
303 | - } |
|
304 | - $indexed = array_values($data); |
|
305 | - $times = is_array($data) && $indexed == $data |
|
306 | - ? count($data) : 0; |
|
307 | - } |
|
308 | - $source = $tag; |
|
309 | - if (!empty($offsetTokens)) { |
|
310 | - if (false !== strpos($source->class, ' ')) { |
|
311 | - $class = explode(' ', $source->class); |
|
312 | - array_pop($class); |
|
313 | - $class = implode(' ', $class); |
|
314 | - } else { |
|
315 | - $class = null; |
|
316 | - } |
|
317 | - $tag->class($class); |
|
318 | - $star = array_search('*', $offsetTokens); |
|
319 | - array_splice($offsetTokens, $star, $removeCount); |
|
320 | - $remainingTokens = $offsetTokens; |
|
321 | - } else { |
|
322 | - $remainingTokens = $tokens; |
|
323 | - } |
|
324 | - $source->parent = null; |
|
325 | - $sourceData = $data; |
|
326 | - $currentParent = $parent; |
|
327 | - for ($i = 1; $i <= $times; $i++) { |
|
328 | - $tag = clone $source; |
|
329 | - $parent = $currentParent; |
|
330 | - $data = is_array($sourceData) |
|
331 | - && isset($sourceData[$i - 1]) |
|
332 | - ? $sourceData[$i - 1] |
|
333 | - : @(string)$sourceData; |
|
334 | - $tokens = array_values($remainingTokens); |
|
335 | - $self($self, $i, $times); |
|
336 | - } |
|
337 | - $round = 1; |
|
338 | - $offsetTokens = null; |
|
339 | - $tag = $source; |
|
340 | - $tokens = array(); //$remainingTokens; |
|
341 | - break; |
|
342 | - } |
|
343 | - } |
|
344 | - }; |
|
345 | - $parse($parse); |
|
346 | - return count($root) == 1 ? $root[0] : $root; |
|
347 | - } |
|
175 | + $parse = |
|
176 | + function ( |
|
177 | + Callable $self, $round = 1, $total = 1 |
|
178 | + ) |
|
179 | + use ( |
|
180 | + & $tokens, & $parent, & $tag, & $data, |
|
181 | + $parseAttributes, $implicitTag, $parseText |
|
182 | + ) { |
|
183 | + $offsetTokens = null; |
|
184 | + $parent[] = $tag; |
|
185 | + $isInChild = false; |
|
186 | + while ($tokens) { |
|
187 | + switch (array_shift($tokens)) { |
|
188 | + //class |
|
189 | + case '.': |
|
190 | + $offsetTokens = array_values($tokens); |
|
191 | + array_unshift($offsetTokens, '.'); |
|
192 | + $implicitTag(); |
|
193 | + $e = array_filter(explode(' ', $tag->class)); |
|
194 | + $e[] = $parseText('', $round, $total, $data); |
|
195 | + $tag->class(implode(' ', array_unique($e))); |
|
196 | + break; |
|
197 | + //id |
|
198 | + case '#': |
|
199 | + $offsetTokens = array_values($tokens); |
|
200 | + array_unshift($offsetTokens, '#'); |
|
201 | + $implicitTag(); |
|
202 | + $tag->id( |
|
203 | + $parseText( |
|
204 | + array_shift($tokens), $round, $total, $data |
|
205 | + ) |
|
206 | + ); |
|
207 | + break; |
|
208 | + //attributes |
|
209 | + case '[': |
|
210 | + $offsetTokens = array_values($tokens); |
|
211 | + array_unshift($offsetTokens, '['); |
|
212 | + $implicitTag(); |
|
213 | + $parseAttributes( |
|
214 | + $parseAttributes, $round, $total, $data |
|
215 | + ); |
|
216 | + break; |
|
217 | + //child |
|
218 | + case '{': |
|
219 | + $text = ''; |
|
220 | + $tag[] = $parseText( |
|
221 | + $text, $round, $total, $data, array('}' => true) |
|
222 | + ); |
|
223 | + break; |
|
224 | + case '>': |
|
225 | + $isInChild = true; |
|
226 | + $offsetTokens = null; |
|
227 | + if ('{' == ($t = array_shift($tokens))) { |
|
228 | + array_unshift($tokens, $t); |
|
229 | + $child = new T(); |
|
230 | + $tag[] = $child; |
|
231 | + $parent = $tag; |
|
232 | + $tag = $child; |
|
233 | + } elseif ('[' == $t) { |
|
234 | + array_unshift($tokens, $t); |
|
235 | + } else { |
|
236 | + $child = new T($t); |
|
237 | + $tag[] = $child; |
|
238 | + $parent = $tag; |
|
239 | + $tag = $child; |
|
240 | + } |
|
241 | + break; |
|
242 | + //sibling |
|
243 | + case '+': |
|
244 | + $offsetTokens = null; |
|
245 | + if (!$isInChild && $round != $total) { |
|
246 | + $tokens = array(); |
|
247 | + break; |
|
248 | + } |
|
249 | + if ('{' == ($t = array_shift($tokens))) { |
|
250 | + $tag = $tag->parent; |
|
251 | + array_unshift($tokens, $t); |
|
252 | + break; |
|
253 | + } elseif ('[' == $t) { |
|
254 | + array_unshift($tokens, $t); |
|
255 | + } else { |
|
256 | + $child = new T($t); |
|
257 | + $tag = $tag->parent; |
|
258 | + $tag[] = $child; |
|
259 | + $tag = $child; |
|
260 | + } |
|
261 | + break; |
|
262 | + //sibling of parent |
|
263 | + case '^': |
|
264 | + if ($round != $total) { |
|
265 | + $tokens = array(); |
|
266 | + break; |
|
267 | + } |
|
268 | + $tag = $tag->parent; |
|
269 | + if ($tag->parent) |
|
270 | + $tag = $tag->parent; |
|
271 | + while ('^' == ($t = array_shift($tokens))) { |
|
272 | + if ($tag->parent) |
|
273 | + $tag = $tag->parent; |
|
274 | + } |
|
275 | + $child = new T($t); |
|
276 | + $tag[] = $child; |
|
277 | + $tag = $child; |
|
278 | + break; |
|
279 | + //clone |
|
280 | + case '*': |
|
281 | + $times = array_shift($tokens); |
|
282 | + $removeCount = 2; |
|
283 | + $delimiter = array( |
|
284 | + '.' => true, |
|
285 | + '#' => true, |
|
286 | + '*' => true, |
|
287 | + '>' => true, |
|
288 | + '+' => true, |
|
289 | + '^' => true, |
|
290 | + '[' => true, |
|
291 | + ']' => true, |
|
292 | + '=' => true, |
|
293 | + ); |
|
294 | + if (!is_numeric($times)) { |
|
295 | + if (is_string($times)) { |
|
296 | + if (!isset($delimiter[$times])) { |
|
297 | + $data = Util::nestedValue($data, $times) |
|
298 | + ? : $data; |
|
299 | + } else { |
|
300 | + array_unshift($tokens, $times); |
|
301 | + $removeCount = 1; |
|
302 | + } |
|
303 | + } |
|
304 | + $indexed = array_values($data); |
|
305 | + $times = is_array($data) && $indexed == $data |
|
306 | + ? count($data) : 0; |
|
307 | + } |
|
308 | + $source = $tag; |
|
309 | + if (!empty($offsetTokens)) { |
|
310 | + if (false !== strpos($source->class, ' ')) { |
|
311 | + $class = explode(' ', $source->class); |
|
312 | + array_pop($class); |
|
313 | + $class = implode(' ', $class); |
|
314 | + } else { |
|
315 | + $class = null; |
|
316 | + } |
|
317 | + $tag->class($class); |
|
318 | + $star = array_search('*', $offsetTokens); |
|
319 | + array_splice($offsetTokens, $star, $removeCount); |
|
320 | + $remainingTokens = $offsetTokens; |
|
321 | + } else { |
|
322 | + $remainingTokens = $tokens; |
|
323 | + } |
|
324 | + $source->parent = null; |
|
325 | + $sourceData = $data; |
|
326 | + $currentParent = $parent; |
|
327 | + for ($i = 1; $i <= $times; $i++) { |
|
328 | + $tag = clone $source; |
|
329 | + $parent = $currentParent; |
|
330 | + $data = is_array($sourceData) |
|
331 | + && isset($sourceData[$i - 1]) |
|
332 | + ? $sourceData[$i - 1] |
|
333 | + : @(string)$sourceData; |
|
334 | + $tokens = array_values($remainingTokens); |
|
335 | + $self($self, $i, $times); |
|
336 | + } |
|
337 | + $round = 1; |
|
338 | + $offsetTokens = null; |
|
339 | + $tag = $source; |
|
340 | + $tokens = array(); //$remainingTokens; |
|
341 | + break; |
|
342 | + } |
|
343 | + } |
|
344 | + }; |
|
345 | + $parse($parse); |
|
346 | + return count($root) == 1 ? $root[0] : $root; |
|
347 | + } |
|
348 | 348 | |
349 | - public static function tokenize($string) |
|
350 | - { |
|
351 | - $r = array(); |
|
352 | - $f = strtok($string, static::DELIMITERS); |
|
353 | - $pos = 0; |
|
354 | - do { |
|
355 | - $start = $pos; |
|
356 | - $pos = strpos($string, $f, $start); |
|
357 | - $tokens = array(); |
|
358 | - for ($i = $start; $i < $pos; $i++) { |
|
359 | - $token = $string[$i]; |
|
360 | - if (('#' == $token || '.' == $token) && |
|
361 | - (!empty($tokens) || $i == 0) |
|
362 | - ) { |
|
363 | - $r[] = ''; |
|
364 | - } |
|
365 | - $r[] = $tokens[] = $token; |
|
366 | - } |
|
367 | - $pos += strlen($f); |
|
368 | - $r[] = $f; |
|
369 | - } while (false != ($f = strtok(static::DELIMITERS))); |
|
370 | - for ($i = $pos; $i < strlen($string); $i++) { |
|
371 | - $token = $string[$i]; |
|
372 | - $r[] = $tokens[] = $token; |
|
373 | - } |
|
374 | - return $r; |
|
375 | - /* sample output produced by ".row*3>.col*3" |
|
349 | + public static function tokenize($string) |
|
350 | + { |
|
351 | + $r = array(); |
|
352 | + $f = strtok($string, static::DELIMITERS); |
|
353 | + $pos = 0; |
|
354 | + do { |
|
355 | + $start = $pos; |
|
356 | + $pos = strpos($string, $f, $start); |
|
357 | + $tokens = array(); |
|
358 | + for ($i = $start; $i < $pos; $i++) { |
|
359 | + $token = $string[$i]; |
|
360 | + if (('#' == $token || '.' == $token) && |
|
361 | + (!empty($tokens) || $i == 0) |
|
362 | + ) { |
|
363 | + $r[] = ''; |
|
364 | + } |
|
365 | + $r[] = $tokens[] = $token; |
|
366 | + } |
|
367 | + $pos += strlen($f); |
|
368 | + $r[] = $f; |
|
369 | + } while (false != ($f = strtok(static::DELIMITERS))); |
|
370 | + for ($i = $pos; $i < strlen($string); $i++) { |
|
371 | + $token = $string[$i]; |
|
372 | + $r[] = $tokens[] = $token; |
|
373 | + } |
|
374 | + return $r; |
|
375 | + /* sample output produced by ".row*3>.col*3" |
|
376 | 376 | [0] => div |
377 | 377 | [1] => . |
378 | 378 | [2] => row |
@@ -385,5 +385,5 @@ discard block |
||
385 | 385 | [9] => * |
386 | 386 | [10] => 4 |
387 | 387 | */ |
388 | - } |
|
388 | + } |
|
389 | 389 | } |
390 | 390 | \ No newline at end of file |
@@ -29,7 +29,7 @@ discard block |
||
29 | 29 | return array(); |
30 | 30 | |
31 | 31 | $implicitTag = |
32 | - function () use (& $tag) { |
|
32 | + function() use (& $tag) { |
|
33 | 33 | if (empty($tag->tag)) { |
34 | 34 | switch ($tag->parent->tag) { |
35 | 35 | case 'ul': |
@@ -59,7 +59,7 @@ discard block |
||
59 | 59 | }; |
60 | 60 | |
61 | 61 | $parseText = |
62 | - function ( |
|
62 | + function( |
|
63 | 63 | $text, $round, $total, $data, $delimiter = null |
64 | 64 | ) |
65 | 65 | use ( |
@@ -106,7 +106,7 @@ discard block |
||
106 | 106 | } |
107 | 107 | } |
108 | 108 | if (is_numeric($data)) { |
109 | - $text .= sprintf("%0{$digits}d", (int)$data); |
|
109 | + $text .= sprintf("%0{$digits}d", (int) $data); |
|
110 | 110 | } elseif (is_string($data)) { |
111 | 111 | $text .= $data; |
112 | 112 | } |
@@ -132,7 +132,7 @@ discard block |
||
132 | 132 | }; |
133 | 133 | |
134 | 134 | $parseAttributes = |
135 | - function (Callable $self, $round, $total, $data) |
|
135 | + function(Callable $self, $round, $total, $data) |
|
136 | 136 | use (& $tokens, & $tag, $parseText) { |
137 | 137 | $a = $parseText( |
138 | 138 | '', $round, $total, $data |
@@ -173,7 +173,7 @@ discard block |
||
173 | 173 | $parent = $root = new T; |
174 | 174 | |
175 | 175 | $parse = |
176 | - function ( |
|
176 | + function( |
|
177 | 177 | Callable $self, $round = 1, $total = 1 |
178 | 178 | ) |
179 | 179 | use ( |
@@ -295,7 +295,7 @@ discard block |
||
295 | 295 | if (is_string($times)) { |
296 | 296 | if (!isset($delimiter[$times])) { |
297 | 297 | $data = Util::nestedValue($data, $times) |
298 | - ? : $data; |
|
298 | + ?: $data; |
|
299 | 299 | } else { |
300 | 300 | array_unshift($tokens, $times); |
301 | 301 | $removeCount = 1; |
@@ -330,7 +330,7 @@ discard block |
||
330 | 330 | $data = is_array($sourceData) |
331 | 331 | && isset($sourceData[$i - 1]) |
332 | 332 | ? $sourceData[$i - 1] |
333 | - : @(string)$sourceData; |
|
333 | + : @(string) $sourceData; |
|
334 | 334 | $tokens = array_values($remainingTokens); |
335 | 335 | $self($self, $i, $times); |
336 | 336 | } |
@@ -25,8 +25,9 @@ discard block |
||
25 | 25 | */ |
26 | 26 | public static function make($string, $data = null) |
27 | 27 | { |
28 | - if (!strlen($string)) |
|
29 | - return array(); |
|
28 | + if (!strlen($string)) { |
|
29 | + return array(); |
|
30 | + } |
|
30 | 31 | |
31 | 32 | $implicitTag = |
32 | 33 | function () use (& $tag) { |
@@ -66,8 +67,8 @@ discard block |
||
66 | 67 | & $tokens, & $tag |
67 | 68 | ) { |
68 | 69 | $digits = 0; |
69 | - if ($delimiter == null) |
|
70 | - $delimiter = array( |
|
70 | + if ($delimiter == null) { |
|
71 | + $delimiter = array( |
|
71 | 72 | '.' => true, |
72 | 73 | '#' => true, |
73 | 74 | '*' => true, |
@@ -78,6 +79,7 @@ discard block |
||
78 | 79 | ']' => true, |
79 | 80 | '=' => true, |
80 | 81 | ); |
82 | + } |
|
81 | 83 | while (!empty($tokens) && |
82 | 84 | !isset($delimiter[$t = array_shift($tokens)])) { |
83 | 85 | while ('$' === $t) { |
@@ -126,8 +128,9 @@ discard block |
||
126 | 128 | $text .= $t; |
127 | 129 | } |
128 | 130 | } |
129 | - if (isset($t)) |
|
130 | - array_unshift($tokens, $t); |
|
131 | + if (isset($t)) { |
|
132 | + array_unshift($tokens, $t); |
|
133 | + } |
|
131 | 134 | return $text; |
132 | 135 | }; |
133 | 136 | |
@@ -137,8 +140,9 @@ discard block |
||
137 | 140 | $a = $parseText( |
138 | 141 | '', $round, $total, $data |
139 | 142 | ); |
140 | - if (is_null($a)) |
|
141 | - return; |
|
143 | + if (is_null($a)) { |
|
144 | + return; |
|
145 | + } |
|
142 | 146 | if ('=' == ($v = array_shift($tokens))) { |
143 | 147 | //value |
144 | 148 | if ('"' == ($v = array_shift($tokens))) { |
@@ -266,11 +270,13 @@ discard block |
||
266 | 270 | break; |
267 | 271 | } |
268 | 272 | $tag = $tag->parent; |
269 | - if ($tag->parent) |
|
270 | - $tag = $tag->parent; |
|
273 | + if ($tag->parent) { |
|
274 | + $tag = $tag->parent; |
|
275 | + } |
|
271 | 276 | while ('^' == ($t = array_shift($tokens))) { |
272 | - if ($tag->parent) |
|
273 | - $tag = $tag->parent; |
|
277 | + if ($tag->parent) { |
|
278 | + $tag = $tag->parent; |
|
279 | + } |
|
274 | 280 | } |
275 | 281 | $child = new T($t); |
276 | 282 | $tag[] = $child; |
@@ -132,7 +132,7 @@ discard block |
||
132 | 132 | }; |
133 | 133 | |
134 | 134 | $parseAttributes = |
135 | - function (Callable $self, $round, $total, $data) |
|
135 | + function (callable $self, $round, $total, $data) |
|
136 | 136 | use (& $tokens, & $tag, $parseText) { |
137 | 137 | $a = $parseText( |
138 | 138 | '', $round, $total, $data |
@@ -174,7 +174,7 @@ discard block |
||
174 | 174 | |
175 | 175 | $parse = |
176 | 176 | function ( |
177 | - Callable $self, $round = 1, $total = 1 |
|
177 | + callable $self, $round = 1, $total = 1 |
|
178 | 178 | ) |
179 | 179 | use ( |
180 | 180 | & $tokens, & $parent, & $tag, & $data, |
@@ -19,160 +19,160 @@ |
||
19 | 19 | */ |
20 | 20 | class RateLimit implements iFilter, iUseAuthentication |
21 | 21 | { |
22 | - /** |
|
23 | - * @var \Luracast\Restler\Restler; |
|
24 | - */ |
|
25 | - public $restler; |
|
26 | - /** |
|
27 | - * @var int |
|
28 | - */ |
|
29 | - public static $usagePerUnit = 1200; |
|
30 | - /** |
|
31 | - * @var int |
|
32 | - */ |
|
33 | - public static $authenticatedUsagePerUnit = 5000; |
|
34 | - /** |
|
35 | - * @var string |
|
36 | - */ |
|
37 | - public static $unit = 'hour'; |
|
38 | - /** |
|
39 | - * @var string group the current api belongs to |
|
40 | - */ |
|
41 | - public static $group = 'common'; |
|
22 | + /** |
|
23 | + * @var \Luracast\Restler\Restler; |
|
24 | + */ |
|
25 | + public $restler; |
|
26 | + /** |
|
27 | + * @var int |
|
28 | + */ |
|
29 | + public static $usagePerUnit = 1200; |
|
30 | + /** |
|
31 | + * @var int |
|
32 | + */ |
|
33 | + public static $authenticatedUsagePerUnit = 5000; |
|
34 | + /** |
|
35 | + * @var string |
|
36 | + */ |
|
37 | + public static $unit = 'hour'; |
|
38 | + /** |
|
39 | + * @var string group the current api belongs to |
|
40 | + */ |
|
41 | + public static $group = 'common'; |
|
42 | 42 | |
43 | - protected static $units = array( |
|
44 | - 'second' => 1, |
|
45 | - 'minute' => 60, |
|
46 | - 'hour' => 3600, // 60*60 seconds |
|
47 | - 'day' => 86400, // 60*60*24 seconds |
|
48 | - 'week' => 604800, // 60*60*24*7 seconds |
|
49 | - 'month' => 2592000, // 60*60*24*30 seconds |
|
50 | - ); |
|
43 | + protected static $units = array( |
|
44 | + 'second' => 1, |
|
45 | + 'minute' => 60, |
|
46 | + 'hour' => 3600, // 60*60 seconds |
|
47 | + 'day' => 86400, // 60*60*24 seconds |
|
48 | + 'week' => 604800, // 60*60*24*7 seconds |
|
49 | + 'month' => 2592000, // 60*60*24*30 seconds |
|
50 | + ); |
|
51 | 51 | |
52 | - /** |
|
53 | - * @var array all paths beginning with any of the following will be excluded |
|
54 | - * from documentation |
|
55 | - */ |
|
56 | - public static $excludedPaths = array('explorer'); |
|
52 | + /** |
|
53 | + * @var array all paths beginning with any of the following will be excluded |
|
54 | + * from documentation |
|
55 | + */ |
|
56 | + public static $excludedPaths = array('explorer'); |
|
57 | 57 | |
58 | 58 | |
59 | - /** |
|
60 | - * @param string $unit |
|
61 | - * @param int $usagePerUnit |
|
62 | - * @param int $authenticatedUsagePerUnit set it to false to give unlimited access |
|
63 | - * |
|
64 | - * @throws \InvalidArgumentException |
|
65 | - * @return void |
|
66 | - */ |
|
67 | - public static function setLimit( |
|
68 | - $unit, $usagePerUnit, $authenticatedUsagePerUnit = null |
|
69 | - ) |
|
70 | - { |
|
71 | - static::$unit = $unit; |
|
72 | - static::$usagePerUnit = $usagePerUnit; |
|
73 | - static::$authenticatedUsagePerUnit = |
|
74 | - is_null($authenticatedUsagePerUnit) ? $usagePerUnit : $authenticatedUsagePerUnit; |
|
75 | - } |
|
59 | + /** |
|
60 | + * @param string $unit |
|
61 | + * @param int $usagePerUnit |
|
62 | + * @param int $authenticatedUsagePerUnit set it to false to give unlimited access |
|
63 | + * |
|
64 | + * @throws \InvalidArgumentException |
|
65 | + * @return void |
|
66 | + */ |
|
67 | + public static function setLimit( |
|
68 | + $unit, $usagePerUnit, $authenticatedUsagePerUnit = null |
|
69 | + ) |
|
70 | + { |
|
71 | + static::$unit = $unit; |
|
72 | + static::$usagePerUnit = $usagePerUnit; |
|
73 | + static::$authenticatedUsagePerUnit = |
|
74 | + is_null($authenticatedUsagePerUnit) ? $usagePerUnit : $authenticatedUsagePerUnit; |
|
75 | + } |
|
76 | 76 | |
77 | - public function __isAllowed() |
|
78 | - { |
|
79 | - if (static::$authenticatedUsagePerUnit |
|
80 | - == static::$usagePerUnit |
|
81 | - ) return $this->check(); |
|
82 | - return null; |
|
83 | - } |
|
77 | + public function __isAllowed() |
|
78 | + { |
|
79 | + if (static::$authenticatedUsagePerUnit |
|
80 | + == static::$usagePerUnit |
|
81 | + ) return $this->check(); |
|
82 | + return null; |
|
83 | + } |
|
84 | 84 | |
85 | - public function __setAuthenticationStatus($isAuthenticated = false) |
|
86 | - { |
|
87 | - header('X-Auth-Status: ' . ($isAuthenticated ? 'true' : 'false')); |
|
88 | - $this->check($isAuthenticated); |
|
89 | - } |
|
85 | + public function __setAuthenticationStatus($isAuthenticated = false) |
|
86 | + { |
|
87 | + header('X-Auth-Status: ' . ($isAuthenticated ? 'true' : 'false')); |
|
88 | + $this->check($isAuthenticated); |
|
89 | + } |
|
90 | 90 | |
91 | - private static function validate($unit) |
|
92 | - { |
|
93 | - if (!isset(static::$units[$unit])) |
|
94 | - throw new \InvalidArgumentException( |
|
95 | - 'Rate Limit time unit should be ' |
|
96 | - . implode('|', array_keys(static::$units)) . '.' |
|
97 | - ); |
|
98 | - } |
|
91 | + private static function validate($unit) |
|
92 | + { |
|
93 | + if (!isset(static::$units[$unit])) |
|
94 | + throw new \InvalidArgumentException( |
|
95 | + 'Rate Limit time unit should be ' |
|
96 | + . implode('|', array_keys(static::$units)) . '.' |
|
97 | + ); |
|
98 | + } |
|
99 | 99 | |
100 | - private function check($isAuthenticated = false) |
|
101 | - { |
|
102 | - $path = $this->restler->url; |
|
103 | - foreach (static::$excludedPaths as $exclude) { |
|
104 | - if (empty($exclude) && empty($path)) { |
|
105 | - return true; |
|
106 | - } elseif (0 === strpos($path, $exclude)) { |
|
107 | - return true; |
|
108 | - } |
|
109 | - } |
|
110 | - static::validate(static::$unit); |
|
111 | - $timeUnit = static::$units[static::$unit]; |
|
112 | - $maxPerUnit = $isAuthenticated |
|
113 | - ? static::$authenticatedUsagePerUnit |
|
114 | - : static::$usagePerUnit; |
|
115 | - if ($maxPerUnit) { |
|
116 | - $user = Defaults::$userIdentifierClass; |
|
117 | - if (!method_exists($user, 'getUniqueIdentifier')) { |
|
118 | - throw new \UnexpectedValueException('`Defaults::$userIdentifierClass` must implement `iIdentifyUser` interface'); |
|
119 | - } |
|
120 | - $id = "RateLimit_" . $maxPerUnit . '_per_' . static::$unit |
|
121 | - . '_for_' . static::$group |
|
122 | - . '_' . $user::getUniqueIdentifier(); |
|
123 | - $lastRequest = $this->restler->cache->get($id, true) |
|
124 | - ? : array('time' => 0, 'used' => 0); |
|
125 | - $time = $lastRequest['time']; |
|
126 | - $diff = time() - $time; # in seconds |
|
127 | - $used = $lastRequest['used']; |
|
100 | + private function check($isAuthenticated = false) |
|
101 | + { |
|
102 | + $path = $this->restler->url; |
|
103 | + foreach (static::$excludedPaths as $exclude) { |
|
104 | + if (empty($exclude) && empty($path)) { |
|
105 | + return true; |
|
106 | + } elseif (0 === strpos($path, $exclude)) { |
|
107 | + return true; |
|
108 | + } |
|
109 | + } |
|
110 | + static::validate(static::$unit); |
|
111 | + $timeUnit = static::$units[static::$unit]; |
|
112 | + $maxPerUnit = $isAuthenticated |
|
113 | + ? static::$authenticatedUsagePerUnit |
|
114 | + : static::$usagePerUnit; |
|
115 | + if ($maxPerUnit) { |
|
116 | + $user = Defaults::$userIdentifierClass; |
|
117 | + if (!method_exists($user, 'getUniqueIdentifier')) { |
|
118 | + throw new \UnexpectedValueException('`Defaults::$userIdentifierClass` must implement `iIdentifyUser` interface'); |
|
119 | + } |
|
120 | + $id = "RateLimit_" . $maxPerUnit . '_per_' . static::$unit |
|
121 | + . '_for_' . static::$group |
|
122 | + . '_' . $user::getUniqueIdentifier(); |
|
123 | + $lastRequest = $this->restler->cache->get($id, true) |
|
124 | + ? : array('time' => 0, 'used' => 0); |
|
125 | + $time = $lastRequest['time']; |
|
126 | + $diff = time() - $time; # in seconds |
|
127 | + $used = $lastRequest['used']; |
|
128 | 128 | |
129 | - header("X-RateLimit-Limit: $maxPerUnit per " . static::$unit); |
|
130 | - if ($diff >= $timeUnit) { |
|
131 | - $used = 1; |
|
132 | - $time = time(); |
|
133 | - } elseif ($used >= $maxPerUnit) { |
|
134 | - header("X-RateLimit-Remaining: 0"); |
|
135 | - $wait = $timeUnit - $diff; |
|
136 | - sleep(1); |
|
137 | - throw new RestException(429, |
|
138 | - 'Rate limit of ' . $maxPerUnit . ' request' . |
|
139 | - ($maxPerUnit > 1 ? 's' : '') . ' per ' |
|
140 | - . static::$unit . ' exceeded. Please wait for ' |
|
141 | - . static::duration($wait) . '.' |
|
142 | - ); |
|
143 | - } else { |
|
144 | - $used++; |
|
145 | - } |
|
146 | - $remainingPerUnit = $maxPerUnit - $used; |
|
147 | - header("X-RateLimit-Remaining: $remainingPerUnit"); |
|
148 | - $this->restler->cache->set($id, |
|
149 | - array('time' => $time, 'used' => $used)); |
|
150 | - } |
|
151 | - return true; |
|
152 | - } |
|
129 | + header("X-RateLimit-Limit: $maxPerUnit per " . static::$unit); |
|
130 | + if ($diff >= $timeUnit) { |
|
131 | + $used = 1; |
|
132 | + $time = time(); |
|
133 | + } elseif ($used >= $maxPerUnit) { |
|
134 | + header("X-RateLimit-Remaining: 0"); |
|
135 | + $wait = $timeUnit - $diff; |
|
136 | + sleep(1); |
|
137 | + throw new RestException(429, |
|
138 | + 'Rate limit of ' . $maxPerUnit . ' request' . |
|
139 | + ($maxPerUnit > 1 ? 's' : '') . ' per ' |
|
140 | + . static::$unit . ' exceeded. Please wait for ' |
|
141 | + . static::duration($wait) . '.' |
|
142 | + ); |
|
143 | + } else { |
|
144 | + $used++; |
|
145 | + } |
|
146 | + $remainingPerUnit = $maxPerUnit - $used; |
|
147 | + header("X-RateLimit-Remaining: $remainingPerUnit"); |
|
148 | + $this->restler->cache->set($id, |
|
149 | + array('time' => $time, 'used' => $used)); |
|
150 | + } |
|
151 | + return true; |
|
152 | + } |
|
153 | 153 | |
154 | - private function duration($secs) |
|
155 | - { |
|
156 | - $units = array( |
|
157 | - 'week' => (int)($secs / 86400 / 7), |
|
158 | - 'day' => $secs / 86400 % 7, |
|
159 | - 'hour' => $secs / 3600 % 24, |
|
160 | - 'minute' => $secs / 60 % 60, |
|
161 | - 'second' => $secs % 60); |
|
154 | + private function duration($secs) |
|
155 | + { |
|
156 | + $units = array( |
|
157 | + 'week' => (int)($secs / 86400 / 7), |
|
158 | + 'day' => $secs / 86400 % 7, |
|
159 | + 'hour' => $secs / 3600 % 24, |
|
160 | + 'minute' => $secs / 60 % 60, |
|
161 | + 'second' => $secs % 60); |
|
162 | 162 | |
163 | - $ret = array(); |
|
163 | + $ret = array(); |
|
164 | 164 | |
165 | - //$unit = 'days'; |
|
166 | - foreach ($units as $k => $v) { |
|
167 | - if ($v > 0) { |
|
168 | - $ret[] = $v > 1 ? "$v {$k}s" : "$v $k"; |
|
169 | - //$unit = $k; |
|
170 | - } |
|
171 | - } |
|
172 | - $i = count($ret) - 1; |
|
173 | - if ($i) { |
|
174 | - $ret[$i] = 'and ' . $ret[$i]; |
|
175 | - } |
|
176 | - return implode(' ', $ret); //." $unit."; |
|
177 | - } |
|
165 | + //$unit = 'days'; |
|
166 | + foreach ($units as $k => $v) { |
|
167 | + if ($v > 0) { |
|
168 | + $ret[] = $v > 1 ? "$v {$k}s" : "$v $k"; |
|
169 | + //$unit = $k; |
|
170 | + } |
|
171 | + } |
|
172 | + $i = count($ret) - 1; |
|
173 | + if ($i) { |
|
174 | + $ret[$i] = 'and ' . $ret[$i]; |
|
175 | + } |
|
176 | + return implode(' ', $ret); //." $unit."; |
|
177 | + } |
|
178 | 178 | } |
179 | 179 | \ No newline at end of file |
@@ -84,7 +84,7 @@ discard block |
||
84 | 84 | |
85 | 85 | public function __setAuthenticationStatus($isAuthenticated = false) |
86 | 86 | { |
87 | - header('X-Auth-Status: ' . ($isAuthenticated ? 'true' : 'false')); |
|
87 | + header('X-Auth-Status: '.($isAuthenticated ? 'true' : 'false')); |
|
88 | 88 | $this->check($isAuthenticated); |
89 | 89 | } |
90 | 90 | |
@@ -93,7 +93,7 @@ discard block |
||
93 | 93 | if (!isset(static::$units[$unit])) |
94 | 94 | throw new \InvalidArgumentException( |
95 | 95 | 'Rate Limit time unit should be ' |
96 | - . implode('|', array_keys(static::$units)) . '.' |
|
96 | + . implode('|', array_keys(static::$units)).'.' |
|
97 | 97 | ); |
98 | 98 | } |
99 | 99 | |
@@ -117,16 +117,16 @@ discard block |
||
117 | 117 | if (!method_exists($user, 'getUniqueIdentifier')) { |
118 | 118 | throw new \UnexpectedValueException('`Defaults::$userIdentifierClass` must implement `iIdentifyUser` interface'); |
119 | 119 | } |
120 | - $id = "RateLimit_" . $maxPerUnit . '_per_' . static::$unit |
|
121 | - . '_for_' . static::$group |
|
122 | - . '_' . $user::getUniqueIdentifier(); |
|
120 | + $id = "RateLimit_".$maxPerUnit.'_per_'.static::$unit |
|
121 | + . '_for_'.static::$group |
|
122 | + . '_'.$user::getUniqueIdentifier(); |
|
123 | 123 | $lastRequest = $this->restler->cache->get($id, true) |
124 | - ? : array('time' => 0, 'used' => 0); |
|
124 | + ?: array('time' => 0, 'used' => 0); |
|
125 | 125 | $time = $lastRequest['time']; |
126 | 126 | $diff = time() - $time; # in seconds |
127 | 127 | $used = $lastRequest['used']; |
128 | 128 | |
129 | - header("X-RateLimit-Limit: $maxPerUnit per " . static::$unit); |
|
129 | + header("X-RateLimit-Limit: $maxPerUnit per ".static::$unit); |
|
130 | 130 | if ($diff >= $timeUnit) { |
131 | 131 | $used = 1; |
132 | 132 | $time = time(); |
@@ -135,10 +135,10 @@ discard block |
||
135 | 135 | $wait = $timeUnit - $diff; |
136 | 136 | sleep(1); |
137 | 137 | throw new RestException(429, |
138 | - 'Rate limit of ' . $maxPerUnit . ' request' . |
|
139 | - ($maxPerUnit > 1 ? 's' : '') . ' per ' |
|
140 | - . static::$unit . ' exceeded. Please wait for ' |
|
141 | - . static::duration($wait) . '.' |
|
138 | + 'Rate limit of '.$maxPerUnit.' request'. |
|
139 | + ($maxPerUnit > 1 ? 's' : '').' per ' |
|
140 | + . static::$unit.' exceeded. Please wait for ' |
|
141 | + . static::duration($wait).'.' |
|
142 | 142 | ); |
143 | 143 | } else { |
144 | 144 | $used++; |
@@ -154,7 +154,7 @@ discard block |
||
154 | 154 | private function duration($secs) |
155 | 155 | { |
156 | 156 | $units = array( |
157 | - 'week' => (int)($secs / 86400 / 7), |
|
157 | + 'week' => (int) ($secs / 86400 / 7), |
|
158 | 158 | 'day' => $secs / 86400 % 7, |
159 | 159 | 'hour' => $secs / 3600 % 24, |
160 | 160 | 'minute' => $secs / 60 % 60, |
@@ -171,7 +171,7 @@ discard block |
||
171 | 171 | } |
172 | 172 | $i = count($ret) - 1; |
173 | 173 | if ($i) { |
174 | - $ret[$i] = 'and ' . $ret[$i]; |
|
174 | + $ret[$i] = 'and '.$ret[$i]; |
|
175 | 175 | } |
176 | 176 | return implode(' ', $ret); //." $unit."; |
177 | 177 | } |
@@ -78,7 +78,9 @@ discard block |
||
78 | 78 | { |
79 | 79 | if (static::$authenticatedUsagePerUnit |
80 | 80 | == static::$usagePerUnit |
81 | - ) return $this->check(); |
|
81 | + ) { |
|
82 | + return $this->check(); |
|
83 | + } |
|
82 | 84 | return null; |
83 | 85 | } |
84 | 86 | |
@@ -90,11 +92,12 @@ discard block |
||
90 | 92 | |
91 | 93 | private static function validate($unit) |
92 | 94 | { |
93 | - if (!isset(static::$units[$unit])) |
|
94 | - throw new \InvalidArgumentException( |
|
95 | + if (!isset(static::$units[$unit])) { |
|
96 | + throw new \InvalidArgumentException( |
|
95 | 97 | 'Rate Limit time unit should be ' |
96 | 98 | . implode('|', array_keys(static::$units)) . '.' |
97 | 99 | ); |
100 | + } |
|
98 | 101 | } |
99 | 102 | |
100 | 103 | private function check($isAuthenticated = false) |
@@ -21,28 +21,28 @@ |
||
21 | 21 | */ |
22 | 22 | class AmfFormat extends DependentFormat |
23 | 23 | { |
24 | - const MIME = 'application/x-amf'; |
|
25 | - const EXTENSION = 'amf'; |
|
24 | + const MIME = 'application/x-amf'; |
|
25 | + const EXTENSION = 'amf'; |
|
26 | 26 | |
27 | - const PACKAGE_NAME = 'zendframework/zendamf:dev-master'; |
|
28 | - const EXTERNAL_CLASS = 'ZendAmf\\Parser\\Amf3\\Deserializer'; |
|
27 | + const PACKAGE_NAME = 'zendframework/zendamf:dev-master'; |
|
28 | + const EXTERNAL_CLASS = 'ZendAmf\\Parser\\Amf3\\Deserializer'; |
|
29 | 29 | |
30 | - public function encode($data, $humanReadable = false) |
|
31 | - { |
|
30 | + public function encode($data, $humanReadable = false) |
|
31 | + { |
|
32 | 32 | |
33 | - $stream = new OutputStream(); |
|
34 | - $serializer = new Serializer($stream); |
|
35 | - $serializer->writeTypeMarker($data); |
|
33 | + $stream = new OutputStream(); |
|
34 | + $serializer = new Serializer($stream); |
|
35 | + $serializer->writeTypeMarker($data); |
|
36 | 36 | |
37 | - return $stream->getStream(); |
|
38 | - } |
|
37 | + return $stream->getStream(); |
|
38 | + } |
|
39 | 39 | |
40 | - public function decode($data) |
|
41 | - { |
|
42 | - $stream = new InputStream(substr($data, 1)); |
|
43 | - $deserializer = new Deserializer($stream); |
|
40 | + public function decode($data) |
|
41 | + { |
|
42 | + $stream = new InputStream(substr($data, 1)); |
|
43 | + $deserializer = new Deserializer($stream); |
|
44 | 44 | |
45 | - return $deserializer->readTypeMarker(); |
|
46 | - } |
|
45 | + return $deserializer->readTypeMarker(); |
|
46 | + } |
|
47 | 47 | } |
48 | 48 |
@@ -14,127 +14,127 @@ |
||
14 | 14 | */ |
15 | 15 | abstract class Format implements iFormat |
16 | 16 | { |
17 | - /** |
|
18 | - * override in the extending class |
|
19 | - */ |
|
20 | - const MIME = 'text/plain'; |
|
21 | - /** |
|
22 | - * override in the extending class |
|
23 | - */ |
|
24 | - const EXTENSION = 'txt'; |
|
17 | + /** |
|
18 | + * override in the extending class |
|
19 | + */ |
|
20 | + const MIME = 'text/plain'; |
|
21 | + /** |
|
22 | + * override in the extending class |
|
23 | + */ |
|
24 | + const EXTENSION = 'txt'; |
|
25 | 25 | |
26 | - /** |
|
27 | - * @var string charset encoding defaults to UTF8 |
|
28 | - */ |
|
29 | - protected $charset='utf-8'; |
|
26 | + /** |
|
27 | + * @var string charset encoding defaults to UTF8 |
|
28 | + */ |
|
29 | + protected $charset='utf-8'; |
|
30 | 30 | |
31 | - /** |
|
32 | - * Injected at runtime |
|
33 | - * |
|
34 | - * @var \Luracast\Restler\Restler |
|
35 | - */ |
|
36 | - public $restler; |
|
31 | + /** |
|
32 | + * Injected at runtime |
|
33 | + * |
|
34 | + * @var \Luracast\Restler\Restler |
|
35 | + */ |
|
36 | + public $restler; |
|
37 | 37 | |
38 | - /** |
|
39 | - * Get MIME type => Extension mappings as an associative array |
|
40 | - * |
|
41 | - * @return array list of mime strings for the format |
|
42 | - * @example array('application/json'=>'json'); |
|
43 | - */ |
|
44 | - public function getMIMEMap() |
|
45 | - { |
|
46 | - return array( |
|
47 | - static::MIME => static::EXTENSION |
|
48 | - ); |
|
49 | - } |
|
38 | + /** |
|
39 | + * Get MIME type => Extension mappings as an associative array |
|
40 | + * |
|
41 | + * @return array list of mime strings for the format |
|
42 | + * @example array('application/json'=>'json'); |
|
43 | + */ |
|
44 | + public function getMIMEMap() |
|
45 | + { |
|
46 | + return array( |
|
47 | + static::MIME => static::EXTENSION |
|
48 | + ); |
|
49 | + } |
|
50 | 50 | |
51 | - /** |
|
52 | - * Set the selected MIME type |
|
53 | - * |
|
54 | - * @param string $mime |
|
55 | - * MIME type |
|
56 | - */ |
|
57 | - public function setMIME($mime) |
|
58 | - { |
|
59 | - //do nothing |
|
60 | - } |
|
51 | + /** |
|
52 | + * Set the selected MIME type |
|
53 | + * |
|
54 | + * @param string $mime |
|
55 | + * MIME type |
|
56 | + */ |
|
57 | + public function setMIME($mime) |
|
58 | + { |
|
59 | + //do nothing |
|
60 | + } |
|
61 | 61 | |
62 | - /** |
|
63 | - * Content-Type field of the HTTP header can send a charset |
|
64 | - * parameter in the HTTP header to specify the character |
|
65 | - * encoding of the document. |
|
66 | - * This information is passed |
|
67 | - * here so that Format class can encode data accordingly |
|
68 | - * Format class may choose to ignore this and use its |
|
69 | - * default character set. |
|
70 | - * |
|
71 | - * @param string $charset |
|
72 | - * Example utf-8 |
|
73 | - */ |
|
74 | - public function setCharset($charset) |
|
75 | - { |
|
76 | - $this->charset = $charset; |
|
77 | - } |
|
62 | + /** |
|
63 | + * Content-Type field of the HTTP header can send a charset |
|
64 | + * parameter in the HTTP header to specify the character |
|
65 | + * encoding of the document. |
|
66 | + * This information is passed |
|
67 | + * here so that Format class can encode data accordingly |
|
68 | + * Format class may choose to ignore this and use its |
|
69 | + * default character set. |
|
70 | + * |
|
71 | + * @param string $charset |
|
72 | + * Example utf-8 |
|
73 | + */ |
|
74 | + public function setCharset($charset) |
|
75 | + { |
|
76 | + $this->charset = $charset; |
|
77 | + } |
|
78 | 78 | |
79 | - /** |
|
80 | - * Content-Type accepted by the Format class |
|
81 | - * |
|
82 | - * @return string $charset Example utf-8 |
|
83 | - */ |
|
84 | - public function getCharset() |
|
85 | - { |
|
86 | - return $this->charset; |
|
87 | - } |
|
79 | + /** |
|
80 | + * Content-Type accepted by the Format class |
|
81 | + * |
|
82 | + * @return string $charset Example utf-8 |
|
83 | + */ |
|
84 | + public function getCharset() |
|
85 | + { |
|
86 | + return $this->charset; |
|
87 | + } |
|
88 | 88 | |
89 | - /** |
|
90 | - * Get selected MIME type |
|
91 | - */ |
|
92 | - public function getMIME() |
|
93 | - { |
|
94 | - return static::MIME; |
|
95 | - } |
|
89 | + /** |
|
90 | + * Get selected MIME type |
|
91 | + */ |
|
92 | + public function getMIME() |
|
93 | + { |
|
94 | + return static::MIME; |
|
95 | + } |
|
96 | 96 | |
97 | - /** |
|
98 | - * Set the selected file extension |
|
99 | - * |
|
100 | - * @param string $extension |
|
101 | - * file extension |
|
102 | - */ |
|
103 | - public function setExtension($extension) |
|
104 | - { |
|
105 | - //do nothing; |
|
106 | - } |
|
97 | + /** |
|
98 | + * Set the selected file extension |
|
99 | + * |
|
100 | + * @param string $extension |
|
101 | + * file extension |
|
102 | + */ |
|
103 | + public function setExtension($extension) |
|
104 | + { |
|
105 | + //do nothing; |
|
106 | + } |
|
107 | 107 | |
108 | - /** |
|
109 | - * Get the selected file extension |
|
110 | - * |
|
111 | - * @return string file extension |
|
112 | - */ |
|
113 | - public function getExtension() |
|
114 | - { |
|
115 | - return static::EXTENSION; |
|
116 | - } |
|
108 | + /** |
|
109 | + * Get the selected file extension |
|
110 | + * |
|
111 | + * @return string file extension |
|
112 | + */ |
|
113 | + public function getExtension() |
|
114 | + { |
|
115 | + return static::EXTENSION; |
|
116 | + } |
|
117 | 117 | |
118 | - /** |
|
119 | - * @return boolean is parsing the request supported? |
|
120 | - */ |
|
121 | - public function isReadable() |
|
122 | - { |
|
123 | - return true; |
|
124 | - } |
|
118 | + /** |
|
119 | + * @return boolean is parsing the request supported? |
|
120 | + */ |
|
121 | + public function isReadable() |
|
122 | + { |
|
123 | + return true; |
|
124 | + } |
|
125 | 125 | |
126 | - /** |
|
127 | - * @return boolean is composing response supported? |
|
128 | - */ |
|
129 | - public function isWritable() |
|
130 | - { |
|
131 | - return true; |
|
132 | - } |
|
126 | + /** |
|
127 | + * @return boolean is composing response supported? |
|
128 | + */ |
|
129 | + public function isWritable() |
|
130 | + { |
|
131 | + return true; |
|
132 | + } |
|
133 | 133 | |
134 | - public function __toString() |
|
135 | - { |
|
136 | - return $this->getExtension(); |
|
137 | - } |
|
134 | + public function __toString() |
|
135 | + { |
|
136 | + return $this->getExtension(); |
|
137 | + } |
|
138 | 138 | |
139 | 139 | } |
140 | 140 |
@@ -26,7 +26,7 @@ |
||
26 | 26 | /** |
27 | 27 | * @var string charset encoding defaults to UTF8 |
28 | 28 | */ |
29 | - protected $charset='utf-8'; |
|
29 | + protected $charset = 'utf-8'; |
|
30 | 30 | |
31 | 31 | /** |
32 | 32 | * Injected at runtime |
@@ -20,162 +20,162 @@ |
||
20 | 20 | class CsvFormat extends Format implements iDecodeStream |
21 | 21 | { |
22 | 22 | |
23 | - const MIME = 'text/csv'; |
|
24 | - const EXTENSION = 'csv'; |
|
25 | - public static $delimiter = ','; |
|
26 | - public static $enclosure = '"'; |
|
27 | - public static $escape = '\\'; |
|
28 | - public static $haveHeaders = null; |
|
29 | - |
|
30 | - /** |
|
31 | - * Encode the given data in the csv format |
|
32 | - * |
|
33 | - * @param array $data |
|
34 | - * resulting data that needs to |
|
35 | - * be encoded in the given format |
|
36 | - * @param boolean $humanReadable |
|
37 | - * set to TRUE when restler |
|
38 | - * is not running in production mode. Formatter has to |
|
39 | - * make the encoded output more human readable |
|
40 | - * |
|
41 | - * @return string encoded string |
|
42 | - * |
|
43 | - * @throws RestException 500 on unsupported data |
|
44 | - */ |
|
45 | - public function encode($data, $humanReadable = false) |
|
46 | - { |
|
47 | - $char = Obj::$separatorChar; |
|
48 | - Obj::$separatorChar = false; |
|
49 | - $data = Obj::toArray($data); |
|
50 | - Obj::$separatorChar = $char; |
|
51 | - if (is_array($data) && array_values($data) == $data) { |
|
52 | - //if indexed array |
|
53 | - $lines = array(); |
|
54 | - $row = array_shift($data); |
|
55 | - if (array_values($row) != $row) { |
|
56 | - $lines[] = static::putRow(array_keys($row)); |
|
57 | - } |
|
58 | - $lines[] = static::putRow(array_values($row)); |
|
59 | - foreach ($data as $row) { |
|
60 | - $lines[] = static::putRow(array_values($row)); |
|
61 | - } |
|
62 | - return implode(PHP_EOL, $lines) . PHP_EOL; |
|
63 | - } |
|
64 | - throw new RestException( |
|
65 | - 500, |
|
66 | - 'Unsupported data for ' . strtoupper(static::EXTENSION) . ' format' |
|
67 | - ); |
|
68 | - } |
|
69 | - |
|
70 | - protected static function putRow($data) |
|
71 | - { |
|
72 | - $fp = fopen('php://temp', 'r+'); |
|
73 | - fputcsv($fp, $data, static::$delimiter, static::$enclosure); |
|
74 | - rewind($fp); |
|
75 | - $data = fread($fp, 1048576); |
|
76 | - fclose($fp); |
|
77 | - return rtrim($data, PHP_EOL); |
|
78 | - } |
|
79 | - |
|
80 | - /** |
|
81 | - * Decode the given data from the csv format |
|
82 | - * |
|
83 | - * @param string $data |
|
84 | - * data sent from client to |
|
85 | - * the api in the given format. |
|
86 | - * |
|
87 | - * @return array associative array of the parsed data |
|
88 | - */ |
|
89 | - public function decode($data) |
|
90 | - { |
|
91 | - $decoded = array(); |
|
92 | - |
|
93 | - if (empty($data)) { |
|
94 | - return $decoded; |
|
95 | - } |
|
96 | - |
|
97 | - $lines = array_filter(explode(PHP_EOL, $data)); |
|
98 | - |
|
99 | - $keys = false; |
|
100 | - $row = static::getRow(array_shift($lines)); |
|
101 | - |
|
102 | - if (is_null(static::$haveHeaders)) { |
|
103 | - //try to guess with the given data |
|
104 | - static::$haveHeaders = !count(array_filter($row, 'is_numeric')); |
|
105 | - } |
|
106 | - |
|
107 | - static::$haveHeaders ? $keys = $row : $decoded[] = $row; |
|
108 | - |
|
109 | - while (($row = static::getRow(array_shift($lines), $keys)) !== FALSE) |
|
110 | - $decoded [] = $row; |
|
111 | - |
|
112 | - $char = Obj::$separatorChar; |
|
113 | - Obj::$separatorChar = false; |
|
114 | - $decoded = Obj::toArray($decoded); |
|
115 | - Obj::$separatorChar = $char; |
|
116 | - return $decoded; |
|
117 | - } |
|
118 | - |
|
119 | - protected static function getRow($data, $keys = false) |
|
120 | - { |
|
121 | - if (empty($data)) { |
|
122 | - return false; |
|
123 | - } |
|
124 | - $line = str_getcsv( |
|
125 | - $data, |
|
126 | - static::$delimiter, |
|
127 | - static::$enclosure, |
|
128 | - static::$escape |
|
129 | - ); |
|
130 | - |
|
131 | - $row = array(); |
|
132 | - foreach ($line as $key => $value) { |
|
133 | - if (is_numeric($value)) |
|
134 | - $value = floatval($value); |
|
135 | - if ($keys) { |
|
136 | - if (isset($keys [$key])) |
|
137 | - $row [$keys [$key]] = $value; |
|
138 | - } else { |
|
139 | - $row [$key] = $value; |
|
140 | - } |
|
141 | - } |
|
142 | - if ($keys) { |
|
143 | - for ($i = count($row); $i < count($keys); $i++) { |
|
144 | - $row[$keys[$i]] = null; |
|
145 | - } |
|
146 | - } |
|
147 | - return $row; |
|
148 | - } |
|
149 | - |
|
150 | - /** |
|
151 | - * Decode the given data stream |
|
152 | - * |
|
153 | - * @param string $stream A stream resource with data |
|
154 | - * sent from client to the api |
|
155 | - * in the given format. |
|
156 | - * |
|
157 | - * @return array associative array of the parsed data |
|
158 | - */ |
|
159 | - public function decodeStream($stream) |
|
160 | - { |
|
161 | - $decoded = array(); |
|
162 | - |
|
163 | - $keys = false; |
|
164 | - $row = static::getRow(stream_get_line($stream, 0, PHP_EOL)); |
|
165 | - if (is_null(static::$haveHeaders)) { |
|
166 | - //try to guess with the given data |
|
167 | - static::$haveHeaders = !count(array_filter($row, 'is_numeric')); |
|
168 | - } |
|
169 | - |
|
170 | - static::$haveHeaders ? $keys = $row : $decoded[] = $row; |
|
171 | - |
|
172 | - while (($row = static::getRow(stream_get_line($stream, 0, PHP_EOL), $keys)) !== FALSE) |
|
173 | - $decoded [] = $row; |
|
174 | - |
|
175 | - $char = Obj::$separatorChar; |
|
176 | - Obj::$separatorChar = false; |
|
177 | - $decoded = Obj::toArray($decoded); |
|
178 | - Obj::$separatorChar = $char; |
|
179 | - return $decoded; |
|
180 | - } |
|
23 | + const MIME = 'text/csv'; |
|
24 | + const EXTENSION = 'csv'; |
|
25 | + public static $delimiter = ','; |
|
26 | + public static $enclosure = '"'; |
|
27 | + public static $escape = '\\'; |
|
28 | + public static $haveHeaders = null; |
|
29 | + |
|
30 | + /** |
|
31 | + * Encode the given data in the csv format |
|
32 | + * |
|
33 | + * @param array $data |
|
34 | + * resulting data that needs to |
|
35 | + * be encoded in the given format |
|
36 | + * @param boolean $humanReadable |
|
37 | + * set to TRUE when restler |
|
38 | + * is not running in production mode. Formatter has to |
|
39 | + * make the encoded output more human readable |
|
40 | + * |
|
41 | + * @return string encoded string |
|
42 | + * |
|
43 | + * @throws RestException 500 on unsupported data |
|
44 | + */ |
|
45 | + public function encode($data, $humanReadable = false) |
|
46 | + { |
|
47 | + $char = Obj::$separatorChar; |
|
48 | + Obj::$separatorChar = false; |
|
49 | + $data = Obj::toArray($data); |
|
50 | + Obj::$separatorChar = $char; |
|
51 | + if (is_array($data) && array_values($data) == $data) { |
|
52 | + //if indexed array |
|
53 | + $lines = array(); |
|
54 | + $row = array_shift($data); |
|
55 | + if (array_values($row) != $row) { |
|
56 | + $lines[] = static::putRow(array_keys($row)); |
|
57 | + } |
|
58 | + $lines[] = static::putRow(array_values($row)); |
|
59 | + foreach ($data as $row) { |
|
60 | + $lines[] = static::putRow(array_values($row)); |
|
61 | + } |
|
62 | + return implode(PHP_EOL, $lines) . PHP_EOL; |
|
63 | + } |
|
64 | + throw new RestException( |
|
65 | + 500, |
|
66 | + 'Unsupported data for ' . strtoupper(static::EXTENSION) . ' format' |
|
67 | + ); |
|
68 | + } |
|
69 | + |
|
70 | + protected static function putRow($data) |
|
71 | + { |
|
72 | + $fp = fopen('php://temp', 'r+'); |
|
73 | + fputcsv($fp, $data, static::$delimiter, static::$enclosure); |
|
74 | + rewind($fp); |
|
75 | + $data = fread($fp, 1048576); |
|
76 | + fclose($fp); |
|
77 | + return rtrim($data, PHP_EOL); |
|
78 | + } |
|
79 | + |
|
80 | + /** |
|
81 | + * Decode the given data from the csv format |
|
82 | + * |
|
83 | + * @param string $data |
|
84 | + * data sent from client to |
|
85 | + * the api in the given format. |
|
86 | + * |
|
87 | + * @return array associative array of the parsed data |
|
88 | + */ |
|
89 | + public function decode($data) |
|
90 | + { |
|
91 | + $decoded = array(); |
|
92 | + |
|
93 | + if (empty($data)) { |
|
94 | + return $decoded; |
|
95 | + } |
|
96 | + |
|
97 | + $lines = array_filter(explode(PHP_EOL, $data)); |
|
98 | + |
|
99 | + $keys = false; |
|
100 | + $row = static::getRow(array_shift($lines)); |
|
101 | + |
|
102 | + if (is_null(static::$haveHeaders)) { |
|
103 | + //try to guess with the given data |
|
104 | + static::$haveHeaders = !count(array_filter($row, 'is_numeric')); |
|
105 | + } |
|
106 | + |
|
107 | + static::$haveHeaders ? $keys = $row : $decoded[] = $row; |
|
108 | + |
|
109 | + while (($row = static::getRow(array_shift($lines), $keys)) !== FALSE) |
|
110 | + $decoded [] = $row; |
|
111 | + |
|
112 | + $char = Obj::$separatorChar; |
|
113 | + Obj::$separatorChar = false; |
|
114 | + $decoded = Obj::toArray($decoded); |
|
115 | + Obj::$separatorChar = $char; |
|
116 | + return $decoded; |
|
117 | + } |
|
118 | + |
|
119 | + protected static function getRow($data, $keys = false) |
|
120 | + { |
|
121 | + if (empty($data)) { |
|
122 | + return false; |
|
123 | + } |
|
124 | + $line = str_getcsv( |
|
125 | + $data, |
|
126 | + static::$delimiter, |
|
127 | + static::$enclosure, |
|
128 | + static::$escape |
|
129 | + ); |
|
130 | + |
|
131 | + $row = array(); |
|
132 | + foreach ($line as $key => $value) { |
|
133 | + if (is_numeric($value)) |
|
134 | + $value = floatval($value); |
|
135 | + if ($keys) { |
|
136 | + if (isset($keys [$key])) |
|
137 | + $row [$keys [$key]] = $value; |
|
138 | + } else { |
|
139 | + $row [$key] = $value; |
|
140 | + } |
|
141 | + } |
|
142 | + if ($keys) { |
|
143 | + for ($i = count($row); $i < count($keys); $i++) { |
|
144 | + $row[$keys[$i]] = null; |
|
145 | + } |
|
146 | + } |
|
147 | + return $row; |
|
148 | + } |
|
149 | + |
|
150 | + /** |
|
151 | + * Decode the given data stream |
|
152 | + * |
|
153 | + * @param string $stream A stream resource with data |
|
154 | + * sent from client to the api |
|
155 | + * in the given format. |
|
156 | + * |
|
157 | + * @return array associative array of the parsed data |
|
158 | + */ |
|
159 | + public function decodeStream($stream) |
|
160 | + { |
|
161 | + $decoded = array(); |
|
162 | + |
|
163 | + $keys = false; |
|
164 | + $row = static::getRow(stream_get_line($stream, 0, PHP_EOL)); |
|
165 | + if (is_null(static::$haveHeaders)) { |
|
166 | + //try to guess with the given data |
|
167 | + static::$haveHeaders = !count(array_filter($row, 'is_numeric')); |
|
168 | + } |
|
169 | + |
|
170 | + static::$haveHeaders ? $keys = $row : $decoded[] = $row; |
|
171 | + |
|
172 | + while (($row = static::getRow(stream_get_line($stream, 0, PHP_EOL), $keys)) !== FALSE) |
|
173 | + $decoded [] = $row; |
|
174 | + |
|
175 | + $char = Obj::$separatorChar; |
|
176 | + Obj::$separatorChar = false; |
|
177 | + $decoded = Obj::toArray($decoded); |
|
178 | + Obj::$separatorChar = $char; |
|
179 | + return $decoded; |
|
180 | + } |
|
181 | 181 | } |
182 | 182 | \ No newline at end of file |
@@ -59,11 +59,11 @@ |
||
59 | 59 | foreach ($data as $row) { |
60 | 60 | $lines[] = static::putRow(array_values($row)); |
61 | 61 | } |
62 | - return implode(PHP_EOL, $lines) . PHP_EOL; |
|
62 | + return implode(PHP_EOL, $lines).PHP_EOL; |
|
63 | 63 | } |
64 | 64 | throw new RestException( |
65 | 65 | 500, |
66 | - 'Unsupported data for ' . strtoupper(static::EXTENSION) . ' format' |
|
66 | + 'Unsupported data for '.strtoupper(static::EXTENSION).' format' |
|
67 | 67 | ); |
68 | 68 | } |
69 | 69 |
@@ -106,8 +106,9 @@ discard block |
||
106 | 106 | |
107 | 107 | static::$haveHeaders ? $keys = $row : $decoded[] = $row; |
108 | 108 | |
109 | - while (($row = static::getRow(array_shift($lines), $keys)) !== FALSE) |
|
110 | - $decoded [] = $row; |
|
109 | + while (($row = static::getRow(array_shift($lines), $keys)) !== FALSE) { |
|
110 | + $decoded [] = $row; |
|
111 | + } |
|
111 | 112 | |
112 | 113 | $char = Obj::$separatorChar; |
113 | 114 | Obj::$separatorChar = false; |
@@ -130,11 +131,13 @@ discard block |
||
130 | 131 | |
131 | 132 | $row = array(); |
132 | 133 | foreach ($line as $key => $value) { |
133 | - if (is_numeric($value)) |
|
134 | - $value = floatval($value); |
|
134 | + if (is_numeric($value)) { |
|
135 | + $value = floatval($value); |
|
136 | + } |
|
135 | 137 | if ($keys) { |
136 | - if (isset($keys [$key])) |
|
137 | - $row [$keys [$key]] = $value; |
|
138 | + if (isset($keys [$key])) { |
|
139 | + $row [$keys [$key]] = $value; |
|
140 | + } |
|
138 | 141 | } else { |
139 | 142 | $row [$key] = $value; |
140 | 143 | } |
@@ -169,8 +172,9 @@ discard block |
||
169 | 172 | |
170 | 173 | static::$haveHeaders ? $keys = $row : $decoded[] = $row; |
171 | 174 | |
172 | - while (($row = static::getRow(stream_get_line($stream, 0, PHP_EOL), $keys)) !== FALSE) |
|
173 | - $decoded [] = $row; |
|
175 | + while (($row = static::getRow(stream_get_line($stream, 0, PHP_EOL), $keys)) !== FALSE) { |
|
176 | + $decoded [] = $row; |
|
177 | + } |
|
174 | 178 | |
175 | 179 | $char = Obj::$separatorChar; |
176 | 180 | Obj::$separatorChar = false; |
@@ -15,10 +15,10 @@ |
||
15 | 15 | */ |
16 | 16 | class TsvFormat extends CsvFormat |
17 | 17 | { |
18 | - const MIME = 'text/csv'; |
|
19 | - const EXTENSION = 'csv'; |
|
20 | - public static $delimiter = "\t"; |
|
21 | - public static $enclosure = '"'; |
|
22 | - public static $escape = '\\'; |
|
23 | - public static $haveHeaders = null; |
|
18 | + const MIME = 'text/csv'; |
|
19 | + const EXTENSION = 'csv'; |
|
20 | + public static $delimiter = "\t"; |
|
21 | + public static $enclosure = '"'; |
|
22 | + public static $escape = '\\'; |
|
23 | + public static $haveHeaders = null; |
|
24 | 24 | } |
25 | 25 | \ No newline at end of file |
@@ -20,334 +20,334 @@ |
||
20 | 20 | */ |
21 | 21 | class XmlFormat extends Format |
22 | 22 | { |
23 | - const MIME = 'application/xml'; |
|
24 | - const EXTENSION = 'xml'; |
|
23 | + const MIME = 'application/xml'; |
|
24 | + const EXTENSION = 'xml'; |
|
25 | 25 | |
26 | - // ================================================================== |
|
27 | - // |
|
28 | - // Properties related to reading/parsing/decoding xml |
|
29 | - // |
|
30 | - // ------------------------------------------------------------------ |
|
31 | - public static $importSettingsFromXml = false; |
|
32 | - public static $parseAttributes = true; |
|
33 | - public static $parseNamespaces = true; |
|
34 | - public static $parseTextNodeAsProperty = true; |
|
26 | + // ================================================================== |
|
27 | + // |
|
28 | + // Properties related to reading/parsing/decoding xml |
|
29 | + // |
|
30 | + // ------------------------------------------------------------------ |
|
31 | + public static $importSettingsFromXml = false; |
|
32 | + public static $parseAttributes = true; |
|
33 | + public static $parseNamespaces = true; |
|
34 | + public static $parseTextNodeAsProperty = true; |
|
35 | 35 | |
36 | - // ================================================================== |
|
37 | - // |
|
38 | - // Properties related to writing/encoding xml |
|
39 | - // |
|
40 | - // ------------------------------------------------------------------ |
|
41 | - public static $useTextNodeProperty = true; |
|
42 | - public static $useNamespaces = true; |
|
43 | - public static $cdataNames = array(); |
|
36 | + // ================================================================== |
|
37 | + // |
|
38 | + // Properties related to writing/encoding xml |
|
39 | + // |
|
40 | + // ------------------------------------------------------------------ |
|
41 | + public static $useTextNodeProperty = true; |
|
42 | + public static $useNamespaces = true; |
|
43 | + public static $cdataNames = array(); |
|
44 | 44 | |
45 | - // ================================================================== |
|
46 | - // |
|
47 | - // Common Properties |
|
48 | - // |
|
49 | - // ------------------------------------------------------------------ |
|
50 | - public static $attributeNames = array(); |
|
51 | - public static $textNodeName = 'text'; |
|
52 | - public static $namespaces = array(); |
|
53 | - public static $namespacedProperties = array(); |
|
54 | - /** |
|
55 | - * Default name for the root node. |
|
56 | - * |
|
57 | - * @var string $rootNodeName |
|
58 | - */ |
|
59 | - public static $rootName = 'response'; |
|
60 | - public static $defaultTagName = 'item'; |
|
45 | + // ================================================================== |
|
46 | + // |
|
47 | + // Common Properties |
|
48 | + // |
|
49 | + // ------------------------------------------------------------------ |
|
50 | + public static $attributeNames = array(); |
|
51 | + public static $textNodeName = 'text'; |
|
52 | + public static $namespaces = array(); |
|
53 | + public static $namespacedProperties = array(); |
|
54 | + /** |
|
55 | + * Default name for the root node. |
|
56 | + * |
|
57 | + * @var string $rootNodeName |
|
58 | + */ |
|
59 | + public static $rootName = 'response'; |
|
60 | + public static $defaultTagName = 'item'; |
|
61 | 61 | |
62 | - /** |
|
63 | - * When you decode an XML its structure is copied to the static vars |
|
64 | - * we can use this function to echo them out and then copy paste inside |
|
65 | - * our service methods |
|
66 | - * |
|
67 | - * @return string PHP source code to reproduce the configuration |
|
68 | - */ |
|
69 | - public static function exportCurrentSettings() |
|
70 | - { |
|
71 | - $s = 'XmlFormat::$rootName = "' . (self::$rootName) . "\";\n"; |
|
72 | - $s .= 'XmlFormat::$attributeNames = ' . |
|
73 | - (var_export(self::$attributeNames, true)) . ";\n"; |
|
74 | - $s .= 'XmlFormat::$defaultTagName = "' . |
|
75 | - self::$defaultTagName . "\";\n"; |
|
76 | - $s .= 'XmlFormat::$parseAttributes = ' . |
|
77 | - (self::$parseAttributes ? 'true' : 'false') . ";\n"; |
|
78 | - $s .= 'XmlFormat::$parseNamespaces = ' . |
|
79 | - (self::$parseNamespaces ? 'true' : 'false') . ";\n"; |
|
80 | - if (self::$parseNamespaces) { |
|
81 | - $s .= 'XmlFormat::$namespaces = ' . |
|
82 | - (var_export(self::$namespaces, true)) . ";\n"; |
|
83 | - $s .= 'XmlFormat::$namespacedProperties = ' . |
|
84 | - (var_export(self::$namespacedProperties, true)) . ";\n"; |
|
85 | - } |
|
62 | + /** |
|
63 | + * When you decode an XML its structure is copied to the static vars |
|
64 | + * we can use this function to echo them out and then copy paste inside |
|
65 | + * our service methods |
|
66 | + * |
|
67 | + * @return string PHP source code to reproduce the configuration |
|
68 | + */ |
|
69 | + public static function exportCurrentSettings() |
|
70 | + { |
|
71 | + $s = 'XmlFormat::$rootName = "' . (self::$rootName) . "\";\n"; |
|
72 | + $s .= 'XmlFormat::$attributeNames = ' . |
|
73 | + (var_export(self::$attributeNames, true)) . ";\n"; |
|
74 | + $s .= 'XmlFormat::$defaultTagName = "' . |
|
75 | + self::$defaultTagName . "\";\n"; |
|
76 | + $s .= 'XmlFormat::$parseAttributes = ' . |
|
77 | + (self::$parseAttributes ? 'true' : 'false') . ";\n"; |
|
78 | + $s .= 'XmlFormat::$parseNamespaces = ' . |
|
79 | + (self::$parseNamespaces ? 'true' : 'false') . ";\n"; |
|
80 | + if (self::$parseNamespaces) { |
|
81 | + $s .= 'XmlFormat::$namespaces = ' . |
|
82 | + (var_export(self::$namespaces, true)) . ";\n"; |
|
83 | + $s .= 'XmlFormat::$namespacedProperties = ' . |
|
84 | + (var_export(self::$namespacedProperties, true)) . ";\n"; |
|
85 | + } |
|
86 | 86 | |
87 | - return $s; |
|
88 | - } |
|
87 | + return $s; |
|
88 | + } |
|
89 | 89 | |
90 | - public function encode($data, $humanReadable = false) |
|
91 | - { |
|
92 | - $data = Obj::toArray($data); |
|
93 | - $xml = new XMLWriter(); |
|
94 | - $xml->openMemory(); |
|
95 | - $xml->startDocument('1.0', $this->charset); |
|
96 | - if ($humanReadable) { |
|
97 | - $xml->setIndent(true); |
|
98 | - $xml->setIndentString(' '); |
|
99 | - } |
|
100 | - static::$useNamespaces && isset(static::$namespacedProperties[static::$rootName]) |
|
101 | - ? |
|
102 | - $xml->startElementNs( |
|
103 | - static::$namespacedProperties[static::$rootName], |
|
104 | - static::$rootName, |
|
105 | - static::$namespaces[static::$namespacedProperties[static::$rootName]] |
|
106 | - ) |
|
107 | - : |
|
108 | - $xml->startElement(static::$rootName); |
|
109 | - if (static::$useNamespaces) { |
|
110 | - foreach (static::$namespaces as $prefix => $ns) { |
|
111 | - if (isset(static::$namespacedProperties[static::$rootName]) |
|
112 | - && static::$namespacedProperties[static::$rootName] == $prefix |
|
113 | - ) { |
|
114 | - continue; |
|
115 | - } |
|
116 | - $prefix = 'xmlns' . (empty($prefix) ? '' : ':' . $prefix); |
|
117 | - $xml->writeAttribute($prefix, $ns); |
|
118 | - } |
|
119 | - } |
|
120 | - $this->write($xml, $data, static::$rootName); |
|
121 | - $xml->endElement(); |
|
90 | + public function encode($data, $humanReadable = false) |
|
91 | + { |
|
92 | + $data = Obj::toArray($data); |
|
93 | + $xml = new XMLWriter(); |
|
94 | + $xml->openMemory(); |
|
95 | + $xml->startDocument('1.0', $this->charset); |
|
96 | + if ($humanReadable) { |
|
97 | + $xml->setIndent(true); |
|
98 | + $xml->setIndentString(' '); |
|
99 | + } |
|
100 | + static::$useNamespaces && isset(static::$namespacedProperties[static::$rootName]) |
|
101 | + ? |
|
102 | + $xml->startElementNs( |
|
103 | + static::$namespacedProperties[static::$rootName], |
|
104 | + static::$rootName, |
|
105 | + static::$namespaces[static::$namespacedProperties[static::$rootName]] |
|
106 | + ) |
|
107 | + : |
|
108 | + $xml->startElement(static::$rootName); |
|
109 | + if (static::$useNamespaces) { |
|
110 | + foreach (static::$namespaces as $prefix => $ns) { |
|
111 | + if (isset(static::$namespacedProperties[static::$rootName]) |
|
112 | + && static::$namespacedProperties[static::$rootName] == $prefix |
|
113 | + ) { |
|
114 | + continue; |
|
115 | + } |
|
116 | + $prefix = 'xmlns' . (empty($prefix) ? '' : ':' . $prefix); |
|
117 | + $xml->writeAttribute($prefix, $ns); |
|
118 | + } |
|
119 | + } |
|
120 | + $this->write($xml, $data, static::$rootName); |
|
121 | + $xml->endElement(); |
|
122 | 122 | |
123 | - return $xml->outputMemory(); |
|
124 | - } |
|
123 | + return $xml->outputMemory(); |
|
124 | + } |
|
125 | 125 | |
126 | - public function write(XMLWriter $xml, $data, $parent) |
|
127 | - { |
|
128 | - $text = array(); |
|
129 | - if (is_array($data)) { |
|
130 | - if (static::$useTextNodeProperty && isset($data[static::$textNodeName])) { |
|
131 | - $text [] = $data[static::$textNodeName]; |
|
132 | - unset($data[static::$textNodeName]); |
|
133 | - } |
|
134 | - $attributes = array_flip(static::$attributeNames); |
|
135 | - //make sure we deal with attributes first |
|
136 | - $temp = array(); |
|
137 | - foreach ($data as $key => $value) { |
|
138 | - if (isset($attributes[$key])) { |
|
139 | - $temp[$key] = $data[$key]; |
|
140 | - unset($data[$key]); |
|
141 | - } |
|
142 | - } |
|
143 | - $data = array_merge($temp, $data); |
|
144 | - foreach ($data as $key => $value) { |
|
145 | - if (is_numeric($key)) { |
|
146 | - if (!is_array($value)) { |
|
147 | - $text [] = $value; |
|
148 | - continue; |
|
149 | - } |
|
150 | - $key = static::$defaultTagName; |
|
151 | - } |
|
152 | - $useNS = static::$useNamespaces |
|
153 | - && !empty(static::$namespacedProperties[$key]) |
|
154 | - && false === strpos($key, ':'); |
|
155 | - if (is_array($value)) { |
|
156 | - $useNS |
|
157 | - ? $xml->startElementNs( |
|
158 | - static::$namespacedProperties[$key], |
|
159 | - $key, |
|
160 | - null |
|
161 | - ) |
|
162 | - : $xml->startElement($key); |
|
163 | - $this->write($xml, $value, $key); |
|
164 | - $xml->endElement(); |
|
165 | - continue; |
|
166 | - } elseif (is_bool($value)) { |
|
167 | - $value = $value ? 'true' : 'false'; |
|
168 | - } |
|
169 | - if (isset($attributes[$key])) { |
|
170 | - $xml->writeAttribute($useNS ? static::$namespacedProperties[$key] . ':' . $key : $key, $value); |
|
171 | - } else { |
|
172 | - $useNS |
|
173 | - ? |
|
174 | - $xml->startElementNs( |
|
175 | - static::$namespacedProperties[$key], |
|
176 | - $key, |
|
177 | - null |
|
178 | - ) |
|
179 | - : $xml->startElement($key); |
|
180 | - $this->write($xml, $value, $key); |
|
181 | - $xml->endElement(); |
|
182 | - } |
|
183 | - } |
|
184 | - } else { |
|
185 | - $text [] = (string)$data; |
|
186 | - } |
|
187 | - if (!empty($text)) { |
|
188 | - if (count($text) == 1) { |
|
189 | - in_array($parent, static::$cdataNames) |
|
190 | - ? $xml->writeCdata(implode('', $text)) |
|
191 | - : $xml->text(implode('', $text)); |
|
192 | - } else { |
|
193 | - foreach ($text as $t) { |
|
194 | - $xml->writeElement(static::$textNodeName, $t); |
|
195 | - } |
|
196 | - } |
|
197 | - } |
|
198 | - } |
|
126 | + public function write(XMLWriter $xml, $data, $parent) |
|
127 | + { |
|
128 | + $text = array(); |
|
129 | + if (is_array($data)) { |
|
130 | + if (static::$useTextNodeProperty && isset($data[static::$textNodeName])) { |
|
131 | + $text [] = $data[static::$textNodeName]; |
|
132 | + unset($data[static::$textNodeName]); |
|
133 | + } |
|
134 | + $attributes = array_flip(static::$attributeNames); |
|
135 | + //make sure we deal with attributes first |
|
136 | + $temp = array(); |
|
137 | + foreach ($data as $key => $value) { |
|
138 | + if (isset($attributes[$key])) { |
|
139 | + $temp[$key] = $data[$key]; |
|
140 | + unset($data[$key]); |
|
141 | + } |
|
142 | + } |
|
143 | + $data = array_merge($temp, $data); |
|
144 | + foreach ($data as $key => $value) { |
|
145 | + if (is_numeric($key)) { |
|
146 | + if (!is_array($value)) { |
|
147 | + $text [] = $value; |
|
148 | + continue; |
|
149 | + } |
|
150 | + $key = static::$defaultTagName; |
|
151 | + } |
|
152 | + $useNS = static::$useNamespaces |
|
153 | + && !empty(static::$namespacedProperties[$key]) |
|
154 | + && false === strpos($key, ':'); |
|
155 | + if (is_array($value)) { |
|
156 | + $useNS |
|
157 | + ? $xml->startElementNs( |
|
158 | + static::$namespacedProperties[$key], |
|
159 | + $key, |
|
160 | + null |
|
161 | + ) |
|
162 | + : $xml->startElement($key); |
|
163 | + $this->write($xml, $value, $key); |
|
164 | + $xml->endElement(); |
|
165 | + continue; |
|
166 | + } elseif (is_bool($value)) { |
|
167 | + $value = $value ? 'true' : 'false'; |
|
168 | + } |
|
169 | + if (isset($attributes[$key])) { |
|
170 | + $xml->writeAttribute($useNS ? static::$namespacedProperties[$key] . ':' . $key : $key, $value); |
|
171 | + } else { |
|
172 | + $useNS |
|
173 | + ? |
|
174 | + $xml->startElementNs( |
|
175 | + static::$namespacedProperties[$key], |
|
176 | + $key, |
|
177 | + null |
|
178 | + ) |
|
179 | + : $xml->startElement($key); |
|
180 | + $this->write($xml, $value, $key); |
|
181 | + $xml->endElement(); |
|
182 | + } |
|
183 | + } |
|
184 | + } else { |
|
185 | + $text [] = (string)$data; |
|
186 | + } |
|
187 | + if (!empty($text)) { |
|
188 | + if (count($text) == 1) { |
|
189 | + in_array($parent, static::$cdataNames) |
|
190 | + ? $xml->writeCdata(implode('', $text)) |
|
191 | + : $xml->text(implode('', $text)); |
|
192 | + } else { |
|
193 | + foreach ($text as $t) { |
|
194 | + $xml->writeElement(static::$textNodeName, $t); |
|
195 | + } |
|
196 | + } |
|
197 | + } |
|
198 | + } |
|
199 | 199 | |
200 | - public function decode($data) |
|
201 | - { |
|
202 | - try { |
|
203 | - if ($data == '') { |
|
204 | - return array(); |
|
205 | - } |
|
206 | - libxml_use_internal_errors(true); |
|
207 | - libxml_disable_entity_loader(true); |
|
208 | - $xml = simplexml_load_string($data, |
|
209 | - "SimpleXMLElement", LIBXML_NOBLANKS | LIBXML_NOCDATA | LIBXML_COMPACT); |
|
210 | - if (false === $xml) { |
|
211 | - $error = libxml_get_last_error(); |
|
212 | - throw new RestException(400, 'Malformed XML. ' |
|
213 | - . trim($error->message, "\r\n") . ' at line ' . $error->line); |
|
214 | - } |
|
215 | - libxml_clear_errors(); |
|
216 | - if (static::$importSettingsFromXml) { |
|
217 | - static::$attributeNames = array(); |
|
218 | - static::$namespacedProperties = array(); |
|
219 | - static::$namespaces = array(); |
|
220 | - static::$rootName = $xml->getName(); |
|
221 | - $namespaces = $xml->getNamespaces(); |
|
222 | - if (count($namespaces)) { |
|
223 | - $p = strpos($data, $xml->getName()); |
|
224 | - if ($p && $data[$p - 1] == ':') { |
|
225 | - $s = strpos($data, '<') + 1; |
|
226 | - $prefix = substr($data, $s, $p - $s - 1); |
|
227 | - static::$namespacedProperties[static::$rootName] = $prefix; |
|
228 | - } |
|
229 | - } |
|
230 | - } |
|
231 | - $data = $this->read($xml); |
|
232 | - if (count($data) == 1 && isset($data[static::$textNodeName])) { |
|
233 | - $data = $data[static::$textNodeName]; |
|
234 | - } |
|
200 | + public function decode($data) |
|
201 | + { |
|
202 | + try { |
|
203 | + if ($data == '') { |
|
204 | + return array(); |
|
205 | + } |
|
206 | + libxml_use_internal_errors(true); |
|
207 | + libxml_disable_entity_loader(true); |
|
208 | + $xml = simplexml_load_string($data, |
|
209 | + "SimpleXMLElement", LIBXML_NOBLANKS | LIBXML_NOCDATA | LIBXML_COMPACT); |
|
210 | + if (false === $xml) { |
|
211 | + $error = libxml_get_last_error(); |
|
212 | + throw new RestException(400, 'Malformed XML. ' |
|
213 | + . trim($error->message, "\r\n") . ' at line ' . $error->line); |
|
214 | + } |
|
215 | + libxml_clear_errors(); |
|
216 | + if (static::$importSettingsFromXml) { |
|
217 | + static::$attributeNames = array(); |
|
218 | + static::$namespacedProperties = array(); |
|
219 | + static::$namespaces = array(); |
|
220 | + static::$rootName = $xml->getName(); |
|
221 | + $namespaces = $xml->getNamespaces(); |
|
222 | + if (count($namespaces)) { |
|
223 | + $p = strpos($data, $xml->getName()); |
|
224 | + if ($p && $data[$p - 1] == ':') { |
|
225 | + $s = strpos($data, '<') + 1; |
|
226 | + $prefix = substr($data, $s, $p - $s - 1); |
|
227 | + static::$namespacedProperties[static::$rootName] = $prefix; |
|
228 | + } |
|
229 | + } |
|
230 | + } |
|
231 | + $data = $this->read($xml); |
|
232 | + if (count($data) == 1 && isset($data[static::$textNodeName])) { |
|
233 | + $data = $data[static::$textNodeName]; |
|
234 | + } |
|
235 | 235 | |
236 | - return $data; |
|
237 | - } catch (\RuntimeException $e) { |
|
238 | - throw new RestException(400, |
|
239 | - "Error decoding request. " . $e->getMessage()); |
|
240 | - } |
|
241 | - } |
|
236 | + return $data; |
|
237 | + } catch (\RuntimeException $e) { |
|
238 | + throw new RestException(400, |
|
239 | + "Error decoding request. " . $e->getMessage()); |
|
240 | + } |
|
241 | + } |
|
242 | 242 | |
243 | - public function read(SimpleXMLElement $xml, $namespaces = null) |
|
244 | - { |
|
245 | - $r = array(); |
|
246 | - $text = (string)$xml; |
|
243 | + public function read(SimpleXMLElement $xml, $namespaces = null) |
|
244 | + { |
|
245 | + $r = array(); |
|
246 | + $text = (string)$xml; |
|
247 | 247 | |
248 | - if (static::$parseAttributes) { |
|
249 | - $attributes = $xml->attributes(); |
|
250 | - foreach ($attributes as $key => $value) { |
|
251 | - if (static::$importSettingsFromXml |
|
252 | - && !in_array($key, static::$attributeNames) |
|
253 | - ) { |
|
254 | - static::$attributeNames[] = $key; |
|
255 | - } |
|
256 | - $r[$key] = static::setType((string)$value); |
|
257 | - } |
|
258 | - } |
|
259 | - $children = $xml->children(); |
|
260 | - foreach ($children as $key => $value) { |
|
261 | - if ($key == static::$defaultTagName) { |
|
262 | - $r[] = $this->read($value); |
|
263 | - } elseif (isset($r[$key])) { |
|
264 | - if (is_array($r[$key])) { |
|
265 | - if ($r[$key] != array_values($r[$key])) { |
|
266 | - $r[$key] = array($r[$key]); |
|
267 | - } |
|
268 | - } else { |
|
269 | - $r[$key] = array($r[$key]); |
|
270 | - } |
|
271 | - $r[$key][] = $this->read($value, $namespaces); |
|
272 | - } else { |
|
273 | - $r[$key] = $this->read($value); |
|
274 | - } |
|
275 | - } |
|
248 | + if (static::$parseAttributes) { |
|
249 | + $attributes = $xml->attributes(); |
|
250 | + foreach ($attributes as $key => $value) { |
|
251 | + if (static::$importSettingsFromXml |
|
252 | + && !in_array($key, static::$attributeNames) |
|
253 | + ) { |
|
254 | + static::$attributeNames[] = $key; |
|
255 | + } |
|
256 | + $r[$key] = static::setType((string)$value); |
|
257 | + } |
|
258 | + } |
|
259 | + $children = $xml->children(); |
|
260 | + foreach ($children as $key => $value) { |
|
261 | + if ($key == static::$defaultTagName) { |
|
262 | + $r[] = $this->read($value); |
|
263 | + } elseif (isset($r[$key])) { |
|
264 | + if (is_array($r[$key])) { |
|
265 | + if ($r[$key] != array_values($r[$key])) { |
|
266 | + $r[$key] = array($r[$key]); |
|
267 | + } |
|
268 | + } else { |
|
269 | + $r[$key] = array($r[$key]); |
|
270 | + } |
|
271 | + $r[$key][] = $this->read($value, $namespaces); |
|
272 | + } else { |
|
273 | + $r[$key] = $this->read($value); |
|
274 | + } |
|
275 | + } |
|
276 | 276 | |
277 | - if (static::$parseNamespaces) { |
|
278 | - if (is_null($namespaces)) { |
|
279 | - $namespaces = $xml->getDocNamespaces(true); |
|
280 | - } |
|
281 | - foreach ($namespaces as $prefix => $ns) { |
|
282 | - static::$namespaces[$prefix] = $ns; |
|
283 | - if (static::$parseAttributes) { |
|
284 | - $attributes = $xml->attributes($ns); |
|
285 | - foreach ($attributes as $key => $value) { |
|
286 | - if (isset($r[$key])) { |
|
287 | - $key = "{$prefix}:$key"; |
|
288 | - } |
|
289 | - if (static::$importSettingsFromXml |
|
290 | - && !in_array($key, static::$attributeNames) |
|
291 | - ) { |
|
292 | - static::$namespacedProperties[$key] = $prefix; |
|
293 | - static::$attributeNames[] = $key; |
|
294 | - } |
|
295 | - $r[$key] = static::setType((string)$value); |
|
296 | - } |
|
297 | - } |
|
298 | - $children = $xml->children($ns); |
|
299 | - foreach ($children as $key => $value) { |
|
300 | - if (static::$importSettingsFromXml) { |
|
301 | - static::$namespacedProperties[$key] = $prefix; |
|
302 | - } |
|
303 | - if (isset($r[$key])) { |
|
304 | - if (is_array($r[$key])) { |
|
305 | - if ($r[$key] != array_values($r[$key])) { |
|
306 | - $r[$key] = array($r[$key]); |
|
307 | - } |
|
308 | - } else { |
|
309 | - $r[$key] = array($r[$key]); |
|
310 | - } |
|
311 | - $r[$key][] = $this->read($value, $namespaces); |
|
312 | - } else { |
|
313 | - $r[$key] = $this->read($value, $namespaces); |
|
314 | - } |
|
315 | - } |
|
316 | - } |
|
317 | - } |
|
277 | + if (static::$parseNamespaces) { |
|
278 | + if (is_null($namespaces)) { |
|
279 | + $namespaces = $xml->getDocNamespaces(true); |
|
280 | + } |
|
281 | + foreach ($namespaces as $prefix => $ns) { |
|
282 | + static::$namespaces[$prefix] = $ns; |
|
283 | + if (static::$parseAttributes) { |
|
284 | + $attributes = $xml->attributes($ns); |
|
285 | + foreach ($attributes as $key => $value) { |
|
286 | + if (isset($r[$key])) { |
|
287 | + $key = "{$prefix}:$key"; |
|
288 | + } |
|
289 | + if (static::$importSettingsFromXml |
|
290 | + && !in_array($key, static::$attributeNames) |
|
291 | + ) { |
|
292 | + static::$namespacedProperties[$key] = $prefix; |
|
293 | + static::$attributeNames[] = $key; |
|
294 | + } |
|
295 | + $r[$key] = static::setType((string)$value); |
|
296 | + } |
|
297 | + } |
|
298 | + $children = $xml->children($ns); |
|
299 | + foreach ($children as $key => $value) { |
|
300 | + if (static::$importSettingsFromXml) { |
|
301 | + static::$namespacedProperties[$key] = $prefix; |
|
302 | + } |
|
303 | + if (isset($r[$key])) { |
|
304 | + if (is_array($r[$key])) { |
|
305 | + if ($r[$key] != array_values($r[$key])) { |
|
306 | + $r[$key] = array($r[$key]); |
|
307 | + } |
|
308 | + } else { |
|
309 | + $r[$key] = array($r[$key]); |
|
310 | + } |
|
311 | + $r[$key][] = $this->read($value, $namespaces); |
|
312 | + } else { |
|
313 | + $r[$key] = $this->read($value, $namespaces); |
|
314 | + } |
|
315 | + } |
|
316 | + } |
|
317 | + } |
|
318 | 318 | |
319 | - if (empty($text) && $text !== '0') { |
|
320 | - if (empty($r)) { |
|
321 | - return null; |
|
322 | - } |
|
323 | - } else { |
|
324 | - empty($r) |
|
325 | - ? $r = static::setType($text) |
|
326 | - : ( |
|
327 | - static::$parseTextNodeAsProperty |
|
328 | - ? $r[static::$textNodeName] = static::setType($text) |
|
329 | - : $r[] = static::setType($text) |
|
330 | - ); |
|
331 | - } |
|
319 | + if (empty($text) && $text !== '0') { |
|
320 | + if (empty($r)) { |
|
321 | + return null; |
|
322 | + } |
|
323 | + } else { |
|
324 | + empty($r) |
|
325 | + ? $r = static::setType($text) |
|
326 | + : ( |
|
327 | + static::$parseTextNodeAsProperty |
|
328 | + ? $r[static::$textNodeName] = static::setType($text) |
|
329 | + : $r[] = static::setType($text) |
|
330 | + ); |
|
331 | + } |
|
332 | 332 | |
333 | - return $r; |
|
334 | - } |
|
333 | + return $r; |
|
334 | + } |
|
335 | 335 | |
336 | - public static function setType($value) |
|
337 | - { |
|
338 | - if (empty($value) && $value !== '0') { |
|
339 | - return null; |
|
340 | - } |
|
341 | - if ($value == 'true') { |
|
342 | - return true; |
|
343 | - } |
|
344 | - if ($value == 'false') { |
|
345 | - return true; |
|
346 | - } |
|
347 | - if (is_numeric($value)) { |
|
348 | - return 0 + $value; |
|
349 | - } |
|
336 | + public static function setType($value) |
|
337 | + { |
|
338 | + if (empty($value) && $value !== '0') { |
|
339 | + return null; |
|
340 | + } |
|
341 | + if ($value == 'true') { |
|
342 | + return true; |
|
343 | + } |
|
344 | + if ($value == 'false') { |
|
345 | + return true; |
|
346 | + } |
|
347 | + if (is_numeric($value)) { |
|
348 | + return 0 + $value; |
|
349 | + } |
|
350 | 350 | |
351 | - return $value; |
|
352 | - } |
|
351 | + return $value; |
|
352 | + } |
|
353 | 353 | } |
@@ -68,20 +68,20 @@ discard block |
||
68 | 68 | */ |
69 | 69 | public static function exportCurrentSettings() |
70 | 70 | { |
71 | - $s = 'XmlFormat::$rootName = "' . (self::$rootName) . "\";\n"; |
|
72 | - $s .= 'XmlFormat::$attributeNames = ' . |
|
73 | - (var_export(self::$attributeNames, true)) . ";\n"; |
|
74 | - $s .= 'XmlFormat::$defaultTagName = "' . |
|
75 | - self::$defaultTagName . "\";\n"; |
|
76 | - $s .= 'XmlFormat::$parseAttributes = ' . |
|
77 | - (self::$parseAttributes ? 'true' : 'false') . ";\n"; |
|
78 | - $s .= 'XmlFormat::$parseNamespaces = ' . |
|
79 | - (self::$parseNamespaces ? 'true' : 'false') . ";\n"; |
|
71 | + $s = 'XmlFormat::$rootName = "'.(self::$rootName)."\";\n"; |
|
72 | + $s .= 'XmlFormat::$attributeNames = '. |
|
73 | + (var_export(self::$attributeNames, true)).";\n"; |
|
74 | + $s .= 'XmlFormat::$defaultTagName = "'. |
|
75 | + self::$defaultTagName."\";\n"; |
|
76 | + $s .= 'XmlFormat::$parseAttributes = '. |
|
77 | + (self::$parseAttributes ? 'true' : 'false').";\n"; |
|
78 | + $s .= 'XmlFormat::$parseNamespaces = '. |
|
79 | + (self::$parseNamespaces ? 'true' : 'false').";\n"; |
|
80 | 80 | if (self::$parseNamespaces) { |
81 | - $s .= 'XmlFormat::$namespaces = ' . |
|
82 | - (var_export(self::$namespaces, true)) . ";\n"; |
|
83 | - $s .= 'XmlFormat::$namespacedProperties = ' . |
|
84 | - (var_export(self::$namespacedProperties, true)) . ";\n"; |
|
81 | + $s .= 'XmlFormat::$namespaces = '. |
|
82 | + (var_export(self::$namespaces, true)).";\n"; |
|
83 | + $s .= 'XmlFormat::$namespacedProperties = '. |
|
84 | + (var_export(self::$namespacedProperties, true)).";\n"; |
|
85 | 85 | } |
86 | 86 | |
87 | 87 | return $s; |
@@ -113,7 +113,7 @@ discard block |
||
113 | 113 | ) { |
114 | 114 | continue; |
115 | 115 | } |
116 | - $prefix = 'xmlns' . (empty($prefix) ? '' : ':' . $prefix); |
|
116 | + $prefix = 'xmlns'.(empty($prefix) ? '' : ':'.$prefix); |
|
117 | 117 | $xml->writeAttribute($prefix, $ns); |
118 | 118 | } |
119 | 119 | } |
@@ -167,7 +167,7 @@ discard block |
||
167 | 167 | $value = $value ? 'true' : 'false'; |
168 | 168 | } |
169 | 169 | if (isset($attributes[$key])) { |
170 | - $xml->writeAttribute($useNS ? static::$namespacedProperties[$key] . ':' . $key : $key, $value); |
|
170 | + $xml->writeAttribute($useNS ? static::$namespacedProperties[$key].':'.$key : $key, $value); |
|
171 | 171 | } else { |
172 | 172 | $useNS |
173 | 173 | ? |
@@ -182,7 +182,7 @@ discard block |
||
182 | 182 | } |
183 | 183 | } |
184 | 184 | } else { |
185 | - $text [] = (string)$data; |
|
185 | + $text [] = (string) $data; |
|
186 | 186 | } |
187 | 187 | if (!empty($text)) { |
188 | 188 | if (count($text) == 1) { |
@@ -210,7 +210,7 @@ discard block |
||
210 | 210 | if (false === $xml) { |
211 | 211 | $error = libxml_get_last_error(); |
212 | 212 | throw new RestException(400, 'Malformed XML. ' |
213 | - . trim($error->message, "\r\n") . ' at line ' . $error->line); |
|
213 | + . trim($error->message, "\r\n").' at line '.$error->line); |
|
214 | 214 | } |
215 | 215 | libxml_clear_errors(); |
216 | 216 | if (static::$importSettingsFromXml) { |
@@ -236,14 +236,14 @@ discard block |
||
236 | 236 | return $data; |
237 | 237 | } catch (\RuntimeException $e) { |
238 | 238 | throw new RestException(400, |
239 | - "Error decoding request. " . $e->getMessage()); |
|
239 | + "Error decoding request. ".$e->getMessage()); |
|
240 | 240 | } |
241 | 241 | } |
242 | 242 | |
243 | 243 | public function read(SimpleXMLElement $xml, $namespaces = null) |
244 | 244 | { |
245 | 245 | $r = array(); |
246 | - $text = (string)$xml; |
|
246 | + $text = (string) $xml; |
|
247 | 247 | |
248 | 248 | if (static::$parseAttributes) { |
249 | 249 | $attributes = $xml->attributes(); |
@@ -253,7 +253,7 @@ discard block |
||
253 | 253 | ) { |
254 | 254 | static::$attributeNames[] = $key; |
255 | 255 | } |
256 | - $r[$key] = static::setType((string)$value); |
|
256 | + $r[$key] = static::setType((string) $value); |
|
257 | 257 | } |
258 | 258 | } |
259 | 259 | $children = $xml->children(); |
@@ -292,7 +292,7 @@ discard block |
||
292 | 292 | static::$namespacedProperties[$key] = $prefix; |
293 | 293 | static::$attributeNames[] = $key; |
294 | 294 | } |
295 | - $r[$key] = static::setType((string)$value); |
|
295 | + $r[$key] = static::setType((string) $value); |
|
296 | 296 | } |
297 | 297 | } |
298 | 298 | $children = $xml->children($ns); |