1 | <?php |
||
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 |