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

Diff of /trunk/services/REST/services.php

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 515 by mlimic, Wed Oct 17 16:22:13 2012 UTC revision 516 by mlimic, Wed Oct 17 17:13:43 2012 UTC
# Line 1  Line 1 
1  <?php  <?php
2    
3    /*
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      class Services {      class Services {
31                                    
32          function __construct() {                  
33                    /*
34                     * Constructor
35                     *
36                     * Initializes the contents attribute to an empty array. Contents
37                     */
38            protected function __construct() {
39                          if (!defined('REST_REQUEST')) {                          if (!defined('REST_REQUEST')) {
40                                  define('REST_REQUEST', true);                                  define('REST_REQUEST', true);
41                          }                          }
42                            /** see explanation above */
43              $this->contents = array();              $this->contents = array();
44                            /** see explanation above */
45                          $this->metier_manager = null;                          $this->metier_manager = null;
46                          // An array containing sub-arrays (groups) of one or more elements.                          // An array containing sub-arrays (groups) of one or more elements.
47                          //      The strings inside each group have to be found inside of the                          //      The strings inside a group have to be found inside of the
48                          //      data received by the request. Equally, there can't be                          //      data received by the request.
49                          $this->mdtry_grps = null;                          $this->mdtry_grps = null;
50          }          }
51                                    
52                                    /*
53          function __destruct() {                   * Destructor
54                     */
55            protected function __destruct() {
56                          if ($this->metier_manager) {                          if ($this->metier_manager) {
57                                  unset($this->metier_manager);                                  unset($this->metier_manager);
58                          }                          }
59          }          }
60    
61                                    
62                  function dataComplete($optional = null) {                  /*
63                          //print 'optional:';                   * Verifies that the contents contains non-empty data.
64                          //print_r($optional);                   * 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                          foreach ($this->contents as $key => $value) {                          foreach ($this->contents as $key => $value) {
                                 //print 'key:'.$key.'  ';  
71                                  if (empty($value)) {                                  if (empty($value)) {
72                                          // check if the key is optional                                          // check if the key is optional
73                                          if ($optional && in_array($key, $optional)) {//||                                          if ($optional && in_array($key, $optional)) {//||
                                                 //print 'for key: '.$key;  
74                                                  continue;                                                  continue;
75                                          }                                          }
                                           
                                         //print 'returning false';  
76                                          return false;                                          return false;
77                                  }                                  }
78                          }                          }
                           
                         //print ' returning true ';  
79                          return true;                          return true;
80                  }                  }
81    
82                    
83                  function requestValid($data, $optional = null) {                  /*
84                          //print 'data:';                   * Verifies if the format of the JSON data received via REST
85                          //print_r($data);                   * 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                          if (count($data) == 0) {                          if (count($data) == 0) {
94                              return false;                              return false;
95                          }                          }
96                          foreach (array_keys($this->contents) as $elem) {                          foreach (array_keys($this->contents) as $elem) {
                                 //print ' key:'.$elem.'  ';  
97                              if (isset($data[$elem])) {                              if (isset($data[$elem])) {
98                                  $this->contents[$elem] = $data[$elem];                                  $this->contents[$elem] = $data[$elem];
99                              }                                  }    
100                          }                          }
                         //print 'CONTENTS:';  
                         //print_r($this->contents);  
101                          return $this->dataComplete($optional);                          return $this->dataComplete($optional);
102              }              }
103    
104                  function requestMdtrGroup(&$data) {                  
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                          //print '  data:';                          //print '  data:';
113                          //print_r($data);                          //print_r($data);
114                          // case of mandatory groups, i.e. multiple requests possible to the same source                          // case of mandatory groups, i.e. multiple requests possible to the same source
# Line 98  Line 145 
145                          return $group_idx;                          return $group_idx;
146                  }                  }
147                                    
148                  function sendHttpCode($code, $msg = '') {                  
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                          return $this->HTTPSTATUS($code, $msg);                          return $this->HTTPSTATUS($code, $msg);
156                  }                  }
157                                    
# Line 108  Line 161 
161                  //$request_data is a reserved name in Restler2.                  //$request_data is a reserved name in Restler2.
162          //It will pass all the parameters passed as an          //It will pass all the parameters passed as an
163          //associative array          //associative array
164          function post($request_data) {          protected function post($request_data) {
165              return $this->sendHttpCode(400, 'Unsupported.');              return $this->sendHttpCode(400, 'Unsupported.');
166          }          }
167                                    
168    
169          function get($id) {          protected function get($id) {
170                          return $this->sendHttpCode(400, 'Unsupported.');                          return $this->sendHttpCode(400, 'Unsupported.');
171          }          }
172                                    
173                                    
174          function put($request_data, $id) {          protected function put($request_data, $id) {
175              return $this->sendHttpCode(400, 'Unsupported.');              return $this->sendHttpCode(400, 'Unsupported.');
176          }          }
177                                    
178                                    
179          function delete($id = null) {          protected function delete($id = null) {
180              return $this->sendHttpCode(400, 'Unsupported.');              return $this->sendHttpCode(400, 'Unsupported.');
181              //return array('received' => $request_data);              //return array('received' => $request_data);
182          }            }  
183    
184    
185                  function HTTPStatus($num, $msg) {                  private function HTTPStatus($num, $msg) {
186                      $code = $num;                      $code = $num;
187                      if ( !is_numeric($num) ) {                      if ( !is_numeric($num) ) {
188                          $code = intval($num);                          $code = intval($num);

Legend:
Removed from v.515  
changed lines
  Added in v.516

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26