Completed
Push — master ( 1b74d9...fbc723 )
by Martijn
02:22
created

Example::getUserHeight()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 7
nc 1
nop 2
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Api\Rest;
4
5
/*
6
 * @rest\description SwaggerGen 2 Example API
7
 * @rest\title Example API
8
 * @rest\contact http://example.com Arthur D. Author
9
 * @rest\license MIT
10
 * @rest\security api_key apikey X-Api-Authentication header Authenticate using this fancy header
11
 * @rest\require api_key
12
 */
13
14
/**
15
 * @rest\error 404 This is not the resource you are looking for
16
 * @rest\x-http-message 404
17
 * @rest\doc http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html
18
 * RFC2616, sec.10: W3C status code definitions
19
 */
20
function NotFound()
21
{
22
	header("HTTP/1.0 404 Not Found");
23
}
24
25
/**
26
 * This class demonstrates how to use the SwaggerGen PHPDoc comments.
27
 * It is not meant as an example of a solid Rest API or a complete demonstration
28
 * of all SwaggerGen features.
29
 *
30
 * @rest\api Users Get some useful information on users
31
 */
32
class Example
33
{
34
35
	private $data = array();
36
37
	public function __construct()
38
	{
39
		$this->data = json_decode(file_get_contents('example.json'), true);
40
	}
41
42
	public function request($method, $path, $data = null)
43
	{
44
		preg_match_all('/([^\/]+)(?:\/([^\/]+))?/', $path, $matches, PREG_SET_ORDER);
45
46
		$arguments = array();
47
		$methodname = strtolower($method);
48
		foreach ($matches as $match) {
49
			$methodname .= ucfirst($match[1]);
50
			if (empty($match[2])) {
51
				$methodname .= '_data';
52
			} else {
53
				$arguments[] = $match[2];
54
			}
55
		}
56
57
		if (!method_exists($this, $methodname)) {
58
			throw new \Exception('Method Not Allowed', 405); // @rest\errors 405
59
		}
60
61
		$arguments[] = $data;
62
		return call_user_func_array(array($this, $methodname), $arguments);
63
	}
64
65
	/**
66
	 * @rest\endpoint /user
67
	 * @rest\method GET Get a list of all users
68
	 * @rest\require api_key
69
	 * @rest\response 200 array(string)
70
	 */
71
	private function getUser_data()
72
	{
73
		return array_keys($this->data['users']);
74
	}
75
76
	/**
77
	 * @rest\ifdef admin
78
	 * @rest\endpoint /user/{username}
79
	 * @rest\method DELETE Delete a specific user
80
	 * @rest\endif
81
	 */
82
	private function deleteUser($name)
83
	{
84
		// Pretend we're deleting the user.
85
		return array();
86
	}
87
88
	/**
89
	 * @rest\ifdef root
90
	 * @rest\endpoint /user
91
	 * @rest\method DELETE Delete all users
92
	 * @rest\endif
93
	 */
94
	private function deleteAllUsers($name)
95
	{
96
		// Pretend we're deleting all users.
97
		return array();
98
	}
99
100
	/**
101
	 * @rest\endpoint /user/{username}
102
	 * @rest\method GET Get a list of all users
103
	 * @rest\path String username Name of the user
104
	 * @rest\see self::request
105
	 * @todo get "errors" from self::request, without context issues; filter out methods without a @rest\method statement
106
	 */
107
	private function getUser($name)
108
	{
109
		/**
110
		 * @rest\model User
111
		 * @rest\property int age Age of the user in years
112
		 * @rest\example 123
113
		 * @rest\property int height Height of the user in centimeters
114
		 */
115
		//return $this->data['users'][$name]; // @rest\response OK User
116
		return $this->data['users'][$name]; // @rest\response OK object(age:int[0,100>,height:float) User
117
	}
118
119
	private function getUserAge_data($name)
120
	{
121
		return $this->data['users'][$name]['age'];
122
	}
123
124
	private function getUserHeight_data($name)
125
	{
126
		return $this->data['users'][$name]['height'];
127
	}
128
129
	private function getUserHeight($name, $unit)
130
	{
131
		static $units = array(
132
			'meter' => 100,
133
			'inch' => 2.54,
134
			'foot' => 30.48,
135
			'yard' => 91.44,
136
		);
137
138
		return $this->data['users'][$name]['height'] / $units[$unit];
139
	}
140
141
	private function getAge_data()
142
	{
143
		$age = 0;
144
145
		foreach ($this->data['users'] as $user) {
146
			$age += $user['age'];
147
		}
148
149
		return $age / count($this->data['users']);
150
	}
151
152
	/**
153
	 * @rest\endpoint /user/{username}/image
154
	 * @rest\method put
155
	 * @rest\consumes fileform
156
	 * @rest\path String username Name of the user
157
	 * @rest\form file File Image file (PNG, GIF or JPEG) for the user.
158
	 * Any transparency in the file is replaced with a white background color.
159
	 * Files will be cropped to a square image.
160
	 */
161
	private function putUserImage($name, $data)
162
	{
163
		// not implemented
164
		// @rest\see NotFound
165
	}
166
167
	/**
168
	 * @rest\endpoint /user/{username}/ip
169
	 * @rest\method put
170
	 * @rest\path String username Name of the user
171
	 * @rest\form ipv4 IpAddress IP address of the user
172
	 * @rest\response 204
173
	 */
174
	private function putUserIp($name, $data)
175
	{
176
		// not implemented
177
	}
178
179
}
180