protected function _handle_deletion_error(Exception $err) { $message = $err->getMessage(); if (strpos($message, 'results in no database records')) { $error = array('code' => 'NOT_FOUND', 'detail' => $message); $res = JsonResHandler::render(array($error), 404); } trigger_error($message, E_USER_ERROR); }
public function render($search_params = array()) { $collection_class = $this->get_collection_class(); $collection = new $collection_class(); // fetch collection $collection->fetch($search_params); $include = $this->get_include(); $data = array(); // add all of the data from each model in the collection $collection->each(function ($model) use(&$data, $include) { $data[] = $this->dashKeys($model->get_linked_data($include)); }); $res = JsonResHandler::render($data, 200); }
public function create_new_model($data = array()) { $data = $this->underscoreKeys($data); // ensure that all of the fields actually exist $this->_validate_data($data); // next, since we know all of the fields actually exist, sanitize the // data $data = $this->_sanitize_data($data); // finally, create this new model $class_name = $this->get_model_class(); $model = new $class_name(); // iterate through all of the posted data, setting it on the model object foreach ($data as $property => $value) { $model->{$property} = $value; } // save this new object and render the result $model->save(); $res = JsonResHandler::render($this->dashKeys($model->get_user_friendly_data()), 200); }
public function sanitize_model_identifer($identifier = null) { $model = $this->get_model_object(); // if this was not an array of search params, assume it was the primary // identifier for this table and convert it to an array if (!is_array($identifier)) { $primary_key = $model->get_db_primary_key(); $identifier = array($primary_key => $identifier); } // now sanitize each value in the array foreach ($identifier as $field => $value) { // ensure this is a valid db field if (!$model->is_valid_db_field($field)) { $error = array('code' => 'INVALID_MODEL_IDENTIFIER_FIELD', 'detail' => 'An invalid model identifier was specified; "' . $field . '" does not exist within the specified database schema'); $res = JsonResHandler::render(array($error), 400); } $identifier[$field] = $model->sanitize_db_field_value($field, $value); } return $identifier; }
public function update_model($criteria) { $criteria = $this->underscoreKeys($criteria); // ensure that all of the fields actually exist $result = $this->validate_data($criteria); if ($result !== true) { JsonResHandler::render(array($result), 400); return; } // next, since we know all of the fields actually exist, sanitize the // data $criteria = $this->_sanitize_data($criteria); // finally, create this new model $class_name = $this->get_model_class(); $primary_key = $this->get_model_object()->get_db_primary_key(); try { // retrieve the existing model $model = new $class_name(array($primary_key => $criteria[$primary_key])); } catch (Exception $e) { $message = $e->getMessage(); if (strpos($message, 'results in no database records')) { $error = array('code' => 'NOT_FOUND', 'detail' => $message); JsonResHandler::render(array($error), 404); return; } trigger_error($message, E_USER_ERROR); return; } // iterate through all of the posted data, setting it on the model object foreach ($criteria as $property => $value) { if ($property === $primary_key) { continue; } $model->{$property} = $value; } // save this new object and render the result $model->save(); JsonResHandler::render($this->dashKeys($model->get_user_friendly_data()), 200); }
/** * Retrieves the internal data for this model & model data from linked tables * * @return array|mixed */ public function get_linked_data(array $includes) { if (is_array($includes) && count($includes) && count($this->_linked_tables)) { return $this->fetch_link_data($includes); } else { if (count($includes) && !count($this->_linked_tables)) { // if includes are passed and there isn no link table data, then throw an error // - jsonapi $json_error = array('code' => '400 Bad Request', 'detail' => 'This endpoint does not support ?include.'); JsonResHandler::render(array($json_error), 400); } else { return $this->get_data(); } } }
protected static function _send_404() { $error = array('code' => 'NOT_FOUND', 'detail' => 'The endpoint that you have requested could not be found.'); $res = JsonResHandler::render(array($error), 404); exit; }
protected function _handle_url_validation_error(Exception $err, $modelName, $identifier) { $message = $err->getMessage(); if (strpos($message, 'results in no database records')) { $error = array('code' => 'NOT_FOUND', 'detail' => $message . ' for ' . $modelName . ' with id ' . $identifier); $res = JsonResHandler::render(array($error), 404); } trigger_error($message, E_USER_ERROR); }