Completed
Push — master ( 522b4d...6246e1 )
by Martijn
09:11
created

Example::getUserHeight()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 7
nc 1
nop 2
dl 0
loc 11
rs 9.4285
c 1
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
	private $data = array();
35
36
	public function __construct()
37
	{
38
		$this->data = json_decode(file_get_contents('example.json'), true);
39
	}
40
41
	public function request($method, $path, $data = null)
42
	{
43
		preg_match_all('/([^\/]+)(?:\/([^\/]+))?/', $path, $matches, PREG_SET_ORDER);
44
45
		$arguments = array();
46
		$methodname = strtolower($method);
47
		foreach ($matches as $match) {
48
			$methodname .= ucfirst($match[1]);
49
			if (empty($match[2])) {
50
				$methodname .= '_data';
51
			} else {
52
				$arguments[] = $match[2];
53
			}
54
		}
55
56
		if (!method_exists($this, $methodname)) {
57
			throw new \Exception('Method Not Allowed', 405); // @rest\errors 405
58
		}
59
60
		$arguments[] = $data;
61
		return call_user_func_array(array($this, $methodname), $arguments);
62
	}
63
64
	/**
65
	 * @rest\endpoint /user
66
	 * @rest\method GET Get a list of all users
67
	 * @rest\require api_key
68
	 * @rest\response 200 array(string)
69
	 */
70
	private function getUser_data()
71
	{
72
		return array_keys($this->data['users']);
73
	}
74
75
	/**
76
	 * @rest\ifdef admin
77
	 * @rest\endpoint /user/{username}
78
	 * @rest\method DELETE Delete a specific user
79
	 * @rest\endif
80
	 */
81
	private function deleteUser($name)
82
	{
83
		// Pretend we're deleting the user.
84
		return array();
85
	}
86
87
	/**
88
	 * @rest\ifdef root
89
	 * @rest\endpoint /user
90
	 * @rest\method DELETE Delete all users
91
	 * @rest\endif
92
	 */
93
	private function deleteAllUsers($name)
94
	{
95
		// Pretend we're deleting all users.
96
		return array();
97
	}
98
99
	/**
100
	 * @rest\endpoint /user/{username}
101
	 * @rest\method GET Get a list of all users
102
	 * @rest\path String username Name of the user
103
	 * @rest\see self::request
104
	 * @todo get "errors" from self::request, without context issues; filter out methods without a @rest\method statement
105
	 */
106
	private function getUser($name)
107
	{
108
		/**
109
		 * @rest\model User
110
		 * @rest\property int age Age of the user in years
111
		 * @rest\property int height Height of the user in centimeters
112
		 */
113
		//return $this->data['users'][$name]; // @rest\response OK User
114
		return $this->data['users'][$name]; // @rest\response OK object(age:int[0,100>,height:float) User
115
	}
116
117
	private function getUserAge_data($name)
118
	{
119
		return $this->data['users'][$name]['age'];
120
	}
121
122
	private function getUserHeight_data($name)
123
	{
124
		return $this->data['users'][$name]['height'];
125
	}
126
127
	private function getUserHeight($name, $unit)
128
	{
129
		static $units = array(
130
			'meter' => 100,
131
			'inch' => 2.54,
132
			'foot' => 30.48,
133
			'yard' => 91.44,
134
		);
135
136
		return $this->data['users'][$name]['height'] / $units[$unit];
137
	}
138
139
	private function getAge_data()
140
	{
141
		$age = 0;
142
143
		foreach ($this->data['users'] as $user) {
144
			$age += $user['age'];
145
		}
146
147
		return $age / count($this->data['users']);
148
	}
149
150
	/**
151
	 * @rest\endpoint /user/{username}/image
152
	 * @rest\method put
153
	 * @rest\consumes fileform
154
	 * @rest\path String username Name of the user
155
	 * @rest\form file File Image file (PNG, GIF or JPEG) for the user.
156
	 * Any transparency in the file is replaced with a white background color.
157
	 * Files will be cropped to a square image.
158
	 */
159
	private function putUserImage($name, $data)
160
	{
161
		// not implemented
162
		// @rest\see NotFound
163
	}
164
165
}
166