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); } } } } }
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; } }
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()); }