Exemplo n.º 1
0
 public function __construct($request_path, $request_params, $request_method, $return_type)
 {
     $function = array('class_name' => __NAMESPACE__, 'method_name' => __METHOD__);
     //validating the return type and set default if not found
     if (array_contains($return_type, Constants::get('allowed_return_types') == false)) {
         $return_type = Constants::get('default_return_type');
     } else {
         $this->return_type = set_default($return_type, Constants::get('allowed_return_types'));
     }
     //validate if request path is valid else throw error
     if (is_ready($request_path) == false) {
         $error = Tool::prepare('Request path is invalid, unable to process routing request.', 'Request path is null, verify that index router has parsed the information correctly.', __LINE__, $this->return_type, Constants::get('default_error_code'));
         Tool::error($function, $error, false);
     } else {
         enforce_inputs(array($request_path, 'array', null, null, false), $this->return_type);
         $this->original_path = $request_path;
         $this->request_path = $request_path;
     }
     //validate if request method is valid, else set as default (post takes precendence if both are used)
     $this->request_method = set_default($request_method, Constants::get('default_http_method'));
     $allowed_http_methods = Constants::get('allowed_http_methods');
     if (array_contains($request_method, $allowed_http_methods, false) == false) {
         $request_method = Constants::get('default_http_method');
     }
     $this->request_method = strtolower($request_method);
     //add post params to class if exist
     $this->request_params = $request_params;
     //normalise request_path
     if (is_ready(end($this->request_path)) == false) {
         array_pop($this->request_path);
     }
 }
Exemplo n.º 2
0
 public static function generate_statement($statement_type, $list, $list_type, $tables, $conditions, $return_type = 'json')
 {
     //sets list to select all if not provided
     $list = set_default($list, '*');
     $conditions = set_default($conditions, '');
     //retrieve limits for shorthand
     $zero_minimium = Limits::get('zero_minimium');
     $text_limit = Limits::get('text_limit');
     //validate statement type
     $statement_types = Constants::get('statement_types');
     enforce_inputs(array($statement_type, 'string', $statement_types, null, false), array($list, 'array', null, null, true), array($list_type, 'string', Constants::get('statement_list_types'), null, false), array($tables, 'string', $text_limit['min'], $text_limit['max'], false), array($conditions, 'string', $zero_minimium, $text_limit['max'], true), $return_type);
     //conditions are optional
     $statement = '';
     if (compare_string($statement, 'SELECT', false)) {
         $statement = format_select($list, $list_type, $tables, $conditions);
     } else {
         if (compare_string($statement, 'INSERT', false)) {
             $statement = format_insert($list, $list_type, $tables, $conditions);
         } else {
             if (compare_string($statement, 'UPDATE', false)) {
                 $statement = format_update($list, $tables, $list_type, $conditions);
             } else {
                 if (compare_string($statement, 'DELETE', false)) {
                     $statement = format_delete($list, $tables, $list_type, $conditions);
                 }
             }
         }
     }
 }
Exemplo n.º 3
0
 public static function write($http_status_code, $data, $parent_tag, $return_type = 'json')
 {
     $function = array('class_name' => __NAMESPACE__, 'method_name' => __METHOD__);
     //setting default values to the input parameters
     $http_status_code = set_default($http_status_code, 200);
     $data = set_default($data, array());
     $parent_tag = set_default($parent_tag, 'datas');
     $return_type = set_default($return_type, Constants::get('default_return_type'));
     //retrieve required limits from tools/constants/limits
     $text_limit = Limits::get('text_limit');
     //ensuring that inputs are validated against the array list of requirements $enforcement contains the results of the validation and message of the error if any.
     $enforcement = enforce_inputs(array($data, 'string:array', null, null, false), array($parent_tag, 'string', $text_limit['min'], $text_limit['max'], false), array($return_type, 'string', Constants::get('allowed_return_types'), null, false));
     $http_status_codes = Constants::get('http_status_codes');
     if (isset($http_status_codes[$http_status_code])) {
         //validates if the $http_status_code provided is in the list
         //check if data type is an array
         if (!is_array($data)) {
             $error = Tool::prepare('Data provided must be an array.', '', __LINE__, $return_type, Constants::get('default_error_code'));
             Tool::error($function, $error, false);
         }
         //formats the data return with the appropriate http status code.
         header('HTTP/1.0 ' . $http_status_code . ' ' . $http_status_codes[$http_status_code], true, $http_status_code);
         if (compare_string($return_type, Constants::get('xml'))) {
             //checks if the user requested data to be returned in xml (by default json)
             header('Content-type: text/xml');
             echo '<' . $parent_tag . '>';
             foreach ($data as $index => $post) {
                 if (is_array($post)) {
                     foreach ($post as $key => $value) {
                         echo '<', $key, '>';
                         if (is_array($value)) {
                             foreach ($value as $tag => $val) {
                                 echo '<', $tag, '>', htmlentities($val), '</', $tag, '>';
                             }
                         }
                         echo '</', $key, '>';
                     }
                 } else {
                     echo '<' . $index . '>' . $post . '</' . $index . '>';
                 }
             }
             echo '</' . $parent_tag . '>';
         } else {
             //header('Content-type: application/json; charset=UTF-8');
             echo json_encode(array($parent_tag => $data));
         }
     } else {
         Writer::write(400, 'Invalid HTTP status code.', Constants::get('error_tag'), $return_type);
         return;
     }
 }
Exemplo n.º 4
0
 public function create_accessor($param, $post_params)
 {
     //define tags for parent tags and child tag
     $parent_tag = 'results';
     $child_tag = 'result';
     $id_limit = Limits::get('id_limit');
     $param[0] = set_default($param[0], '');
     $_id = set_default($post_params['_id'], $param[0]);
     enforce_inputs(array($_id, 'numeric', $id_limit['min'], $id_limit['max'], false), $this->route->get_return_type());
 }
Exemplo n.º 5
0
 * However, the min, max can be replaced by an array list in either with the other as null, it will be able to validate if it exist in the list passed in.
*/
echo '<hr/>Signature: function enforce_inputs() <br/>';
echo 'enforce_inputs(): ' . enforce_inputs() . '<br/>';
//echo 'enforce_inputs(array($data, "string:array", null, null, false)) ' . (enforce_inputs(array($data, 'string:array', null, null, false))) . ' - empty variable <br/><br/>';
$data = 123;
echo '$data = 123 <br/><br/>';
echo ' enforce_inputs(array($data, "xxxx", null, null, false)) - invalid type ' . ' <br/><br/>';
echo ' enforce_inputs(array($data, "numeric", "b", "a", false)) - invalid min max <br/><br/>';
echo ' enforce_inputs(array($data, "numeric", null, 5, false)) - validate only one side <br/><br/>';
echo ' enforce_inputs(array($data, "numeric", null, null, false)) - no validation for min/max <br/><br/>';
echo ' enforce_inputs(array($data, "numeric", array("a"), null, false)) - setting validation list (left) <br/><br/>';
echo ' enforce_inputs(array($data, "numeric", null, array("b"), false)) - setting validation list (right) <br/><br/>';
echo ' enforce_inputs(array($data, "numeric", array("a"), array("b"), false)) - setting validation list (both), min take precendence <br/><br/>';
echo ' enforce_inputs(array($data, "numeric", "x", array("a"), false)) - array must be on left <br/><br/>';
echo ' enforce_inputs(array($data, "numeric", array("a"), "x", false)) - array must be on left <br/><br/>';
echo ' enforce_inputs(array($data, "numeric", null, "x", false)) - array must be on left <br/><br/>';
echo ' enforce_inputs(array($data, "numeric", 5, 3, false)) - min more than max <br/><br/>';
echo ' enforce_inputs(array($data, "numeric", null, null, 10)) - nullable must be boolean <br/><br/>';
echo ' enforce_inputs(array($data, "string", null, null, 10)) - Variable not a string <br/><br/>';
echo " enforce_inputs(array({$data}, 'xxxx', null, null, false), 'json'); - takes in the return type <br/><br/>";
echo " enforce_inputs(array({$data}, 'xxxx', null, null, false), 'xml'); - takes in the return type <br/><br/>";
echo " enforce_inputs(array({$data}, 'xxxx', null, null, false)); - return type not given as last variable, default json <br/><br/>";
echo " enforce_inputs(array({$data}, 'xxxx', null, null, false), 'xxxx'); - return type not given as last variable, default json <br/><br/>";
echo " enforce_inputs(array({$data}, 'xxxx', null, null, false), 'xxxx'); - invalid return type default json used <br/><br/>";
echo " enforce_inputs(array({$data}, 'string', 0, 15, true), 'xxxx'); - if minimum is 0, nullable must be set to true <br/><br/>";
$data = '123';
enforce_inputs(array($data, 'xxxx', null, null, false), 'json');
?>