/[openfoncier]/trunk/services/REST/services.php
ViewVC logotype

Annotation of /trunk/services/REST/services.php

Parent Directory Parent Directory | Revision Log Revision Log


Revision 516 - (hide annotations)
Wed Oct 17 17:13:43 2012 UTC (12 years, 3 months ago) by mlimic
File size: 6282 byte(s)
save
1 mlimic 515 <?php
2    
3 mlimic 516 /*
4     * The base class for all of the services provided through the REST interface.
5     *
6     * It creates an instance of utils which establish DB connection. It also
7     * contains methods for checking the correctness of the format of the
8     * incomming messages.
9     *
10     * Attributes:
11     * contents : contains the parts of the message received via
12     * a REST request in JSON format. It is an associative array.
13     * mdtry_grps : is used in checking the completeness (validity) of
14     * a request. It can be a 2D array, or a 1D array or a mix. Each element, a group,
15     * of mdtry_grps is treated as an array. One such element contains one or more
16     * labels that are to be found as keys inside of the JSON data send with the
17     * request. If mdtry_grps is not null, then the message contents must match one
18     * of the groups found inside of mdtry_grps in the full, and it must not match
19     * more than one group.
20     * metier_manager : it is one of the xxxManager classes that do the processing
21     * of the data.
22     *
23     * Author: Mirna Limic
24     * Date: 15/10/2012
25     * Follow-up:
26     * Bugs: Unknown
27     *
28     */
29    
30 mlimic 515 class Services {
31    
32 mlimic 516
33     /*
34     * Constructor
35     *
36     * Initializes the contents attribute to an empty array. Contents
37     */
38     protected function __construct() {
39 mlimic 515 if (!defined('REST_REQUEST')) {
40     define('REST_REQUEST', true);
41     }
42 mlimic 516 /** see explanation above */
43 mlimic 515 $this->contents = array();
44 mlimic 516 /** see explanation above */
45 mlimic 515 $this->metier_manager = null;
46     // An array containing sub-arrays (groups) of one or more elements.
47 mlimic 516 // The strings inside a group have to be found inside of the
48     // data received by the request.
49 mlimic 515 $this->mdtry_grps = null;
50     }
51    
52 mlimic 516 /*
53     * Destructor
54     */
55     protected function __destruct() {
56 mlimic 515 if ($this->metier_manager) {
57     unset($this->metier_manager);
58     }
59     }
60    
61    
62 mlimic 516 /*
63     * Verifies that the contents contains non-empty data.
64     * optional: An array of strings for which $this->contents
65     * is allowed to have an empty value
66     * return: true if contents has no disallowed empty values,
67     * false otherwise.
68     */
69     private function dataComplete($optional = null) {
70 mlimic 515 foreach ($this->contents as $key => $value) {
71     if (empty($value)) {
72     // check if the key is optional
73     if ($optional && in_array($key, $optional)) {//||
74     continue;
75     }
76     return false;
77     }
78     }
79     return true;
80     }
81    
82 mlimic 516
83     /*
84     * Verifies if the format of the JSON data received via REST
85     * is valid.
86     * data: the array containing the data received via REST
87     * optional: An array of strings for which $this->contents
88     * is allowed to have an empty value
89     * return: true if contents has no disallowed empty values,
90     * false otherwise.
91     */
92     protected function requestValid($data, $optional = null) {
93 mlimic 515 if (count($data) == 0) {
94     return false;
95     }
96     foreach (array_keys($this->contents) as $elem) {
97     if (isset($data[$elem])) {
98     $this->contents[$elem] = $data[$elem];
99     }
100     }
101     return $this->dataComplete($optional);
102     }
103    
104 mlimic 516
105     /*
106     * Returns the index of the group corresponding to the format
107     * of the JSON data received via REST interface.
108     * data: the array containing the data received via REST
109     * return: index of the group starting at 0.
110     */
111     protected function requestMdtrGroup(&$data) {
112 mlimic 515 //print ' data:';
113     //print_r($data);
114     // case of mandatory groups, i.e. multiple requests possible to the same source
115     if (!$this->mdtry_grps) {
116     return -1;
117     }
118     $lengroups = count($this->mdtry_grps);
119     $group_idx = -1;
120     for ($i = 0; $i < $lengroups; $i++) {
121     $group = $this->mdtry_grps[$i];
122     if (!is_array($group)) {
123     $group = array($group);
124     }
125     //print ' group:';
126     //print_r($group);
127     foreach($group as $elem) {
128     //print ' elem:'.$elem.' ';
129     if (isset($data[$elem]) && !empty($data[$elem])) {
130     //print ' group_idx:'.$group_idx.' ';
131     //print ' $i:'.$i.' ';
132     if ($group_idx != -1 && $group_idx != $i) { // The keys from disjoint groups found in data
133     return -1;
134     }
135     $group_idx = $i;
136     } else { // Not all strings in a group found insidee of data
137     if ($group_idx == $i) {
138     return -1;
139     }
140     }
141     }
142    
143     }
144     //print ' group_idx:'.$group_idx.' ';
145     return $group_idx;
146     }
147    
148 mlimic 516
149     /*
150     * Sends the return http status code.
151     * code: The code to send
152     * msg: The additional error message that can be added to the return
153     */
154     protected function sendHttpCode($code, $msg = '') {
155 mlimic 515 return $this->HTTPSTATUS($code, $msg);
156     }
157    
158    
159     // post test
160     // source: http://stackoverflow.com/questions/7747186/using-luracast-restler-passing-post-vars-as-json-in-body
161     //$request_data is a reserved name in Restler2.
162     //It will pass all the parameters passed as an
163     //associative array
164 mlimic 516 protected function post($request_data) {
165 mlimic 515 return $this->sendHttpCode(400, 'Unsupported.');
166     }
167    
168    
169 mlimic 516 protected function get($id) {
170 mlimic 515 return $this->sendHttpCode(400, 'Unsupported.');
171     }
172    
173    
174 mlimic 516 protected function put($request_data, $id) {
175 mlimic 515 return $this->sendHttpCode(400, 'Unsupported.');
176     }
177    
178    
179 mlimic 516 protected function delete($id = null) {
180 mlimic 515 return $this->sendHttpCode(400, 'Unsupported.');
181     //return array('received' => $request_data);
182     }
183    
184    
185 mlimic 516 private function HTTPStatus($num, $msg) {
186 mlimic 515 $code = $num;
187     if ( !is_numeric($num) ) {
188     $code = intval($num);
189     }
190     $http_protocol = "HTTP/1.0";
191     if(isset($_SERVER['SERVER_PROTOCOL']) && stripos($_SERVER['SERVER_PROTOCOL'],"HTTP") >= 0){
192     $http_protocol = $_SERVER['SERVER_PROTOCOL'];
193     }
194     $http = array(
195     200 => ' 200 OK',
196     201 => ' 201 Created',
197     204 => ' 204 No Content',
198     400 => ' 400 Bad Request',
199     401 => ' 401 Unauthorized',
200     403 => ' 403 Forbidden',
201     404 => ' 404 Not Found',
202     409 => ' 409 Conflict',
203     500 => ' 500 Internal Server Error',
204     );
205    
206     header($http[$code]);
207     $error_msg = $http[$code];
208     if (!empty($msg)) {
209     $error_msg .= ' ('.$msg.')';
210     }
211     return
212     array(
213     'code' => $code,
214     'error' => $error_msg,
215     );
216     }
217    
218     }
219    
220     ?>

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26