Skip to content

Commit

Permalink
Collapse 8 near-identical functions in the Rest class into 1.
Browse files Browse the repository at this point in the history
  • Loading branch information
shadlaws committed Jun 15, 2013
1 parent 0c00199 commit e31f396
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 72 deletions.
4 changes: 2 additions & 2 deletions modules/gallery/classes/Gallery/Controller/Rest/Items.php
Expand Up @@ -91,7 +91,7 @@ public static function get_members($id, $params) {
}
} else {
// Members are the standard item collection member list - same as members of root item.
$data = Rest::get_members("item", Item::root()->id, $params);
$data = Rest::resource_func("get_members", "item", Item::root()->id, $params);
}

return $data;
Expand All @@ -111,7 +111,7 @@ public static function post_entity($id, $params) {
throw Rest_Exception::factory(400, array("parent" => "invalid"));
}

return Rest::post_entity("item", $p_id, $params);
return Rest::resource_func("post_entity", "item", $p_id, $params);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion modules/gallery/classes/Gallery/Controller/Rest/Tree.php
Expand Up @@ -70,7 +70,7 @@ static function get_entity($id, $params) {
$data = array();
foreach (array_merge(array($item), iterator_to_array($members)) as $member) {
$url = Rest::url("item", $member->id);
$entity = Rest::get_entity("item", $member->id);
$entity = Rest::resource_func("get_entity", "item", $member->id);

if (isset($params["fields"])) {
// Filter by the specified fields.
Expand Down
16 changes: 8 additions & 8 deletions modules/rest/classes/Rest/Controller/Rest.php
Expand Up @@ -245,7 +245,7 @@ public function action_get() {

if (Arr::get($this->request->query(), "expand_members",
static::$default_params["expand_members"])) {
$members = Rest::get_members($this->rest_type, $this->rest_id, $this->request->query());
$members = Rest::resource_func("get_members", $this->rest_type, $this->rest_id, $this->request->query());
if (!isset($members)) {
// A null members array means the resource has no members function - fire a 400 Bad Request.
throw Rest_Exception::factory(400, array("expand_members" => "not_a_collection"));
Expand All @@ -268,11 +268,11 @@ public function action_put() {
$this->check_method();

if (Arr::get($this->request->post(), "entity")) {
Rest::put_entity($this->rest_type, $this->rest_id, $this->request->post());
Rest::resource_func("put_entity", $this->rest_type, $this->rest_id, $this->request->post());
}

if (Arr::get($this->request->post(), "members")) {
Rest::put_members($this->rest_type, $this->rest_id, $this->request->post());
Rest::resource_func("put_members", $this->rest_type, $this->rest_id, $this->request->post());
}

$put_rels = (array)$this->request->post("relationships");
Expand All @@ -284,7 +284,7 @@ public function action_put() {
throw Rest_Exception::factory(400, array("relationships" => "invalid"));
}

Rest::put_members($actual_rels[$r_key][0], Arr::get($actual_rels[$r_key], 1), (array)$r_params);
Rest::resource_func("put_members", $actual_rels[$r_key][0], Arr::get($actual_rels[$r_key], 1), (array)$r_params);
}
}
}
Expand All @@ -303,13 +303,13 @@ public function action_post() {
$this->check_method();

if (Arr::get($this->request->post(), "entity")) {
$result = Rest::post_entity($this->rest_type, $this->rest_id, $this->request->post());
$result = Rest::resource_func("post_entity", $this->rest_type, $this->rest_id, $this->request->post());
} else {
throw Rest_Exception::factory(400, array("entity" => "required"));
}

if (Arr::get($this->request->post(), "members")) {
Rest::post_members($result[0], $result[1], $this->request->post());
Rest::resource_func("post_members", $result[0], $result[1], $this->request->post());
}

$post_rels = (array)$this->request->post("relationships");
Expand All @@ -321,7 +321,7 @@ public function action_post() {
throw Rest_Exception::factory(400, array("relationships" => "invalid"));
}

Rest::post_members($actual_rels[$r_key][0], Arr::get($actual_rels[$r_key], 1), (array)$r_params);
Rest::resource_func("post_members", $actual_rels[$r_key][0], Arr::get($actual_rels[$r_key], 1), (array)$r_params);
}
}

Expand All @@ -342,7 +342,7 @@ public function action_post() {
public function action_delete() {
$this->check_method();

Rest::delete($this->rest_type, $this->rest_id, $this->request->post());
Rest::resource_func("delete", $this->rest_type, $this->rest_id, $this->request->post());
}

/**
Expand Down
77 changes: 17 additions & 60 deletions modules/rest/classes/Rest/Rest.php
Expand Up @@ -211,62 +211,6 @@ static function url($type, $id=null, $params=array()) {
return $url;
}

/**
* GET a resource's entity.
* @return array entity fields
*/
static function get_entity($type, $id=null, $params=array()) {
return static::_call_rest_func("get_entity", $type, $id, $params);
}

/**
* GET a resource's members.
* @return array type/id/params triads
*/
static function get_members($type, $id=null, $params=array()) {
return static::_call_rest_func("get_members", $type, $id, $params);
}

/**
* PUT a resource's entity.
* @return null
*/
static function put_entity($type, $id=null, $params=array()) {
return static::_call_rest_func("put_entity", $type, $id, $params);
}

/**
* PUT a resource's members.
* @return null
*/
static function put_members($type, $id=null, $params=array()) {
return static::_call_rest_func("put_members", $type, $id, $params);
}

/**
* POST a resource's entity.
* @return array type/id/params/new_flag array (default new_flag is true)
*/
static function post_entity($type, $id=null, $params=array()) {
return static::_call_rest_func("post_entity", $type, $id, $params);
}

/**
* POST a resource's members.
* @return null
*/
static function post_members($type, $id=null, $params=array()) {
return static::_call_rest_func("post_members", $type, $id, $params);
}

/**
* DELETE a resource.
* @return null
*/
static function delete($type, $id=null, $params=array()) {
return static::_call_rest_func("delete", $type, $id, $params);
}

/**
* Find a resource's relationships.
* @return array type/id/params triads
Expand Down Expand Up @@ -306,12 +250,12 @@ static function get_resource($type, $id=null, $params=array()) {

$results["url"] = Rest::url($type, $id, $params);

$data = Rest::get_entity($type, $id, $params);
$data = Rest::resource_func("get_entity", $type, $id, $params);
if (isset($data)) {
$results["entity"] = $data;
}

$data = Rest::get_members($type, $id, $params);
$data = Rest::resource_func("get_members", $type, $id, $params);
if (isset($data)) {
$results["members"] = array();
foreach ($data as $key => $member) {
Expand All @@ -324,7 +268,7 @@ static function get_resource($type, $id=null, $params=array()) {
foreach ($data as $r_key => $rel) {
$results["relationships"][$r_key]["url"] = Rest::url($rel);

$rel_members = Rest::get_members($rel);
$rel_members = Rest::resource_func("get_members", $rel);
if (isset($rel_members)) {
$results["relationships"][$r_key]["members"] = array();
foreach ($rel_members as $key => $member) {
Expand Down Expand Up @@ -392,7 +336,20 @@ static function approve_origin($origin) {
return false;
}

static protected function _call_rest_func($func, $type, $id, $params) {
/**
* Call a function of a specific REST resource. The expected response is
* either an array or null, depending on which function is called.
* get_entity - array of entity fields
* get_members - array of type/id/params triads
* put_entity - null
* put_members - null
* post_entity - array with type/id/params/new_flag of resource (default new_flag is true)
* post_members - null
* delete - null
*
* @return array or null
*/
static function resource_func($func, $type, $id=null, $params=array()) {
if (is_array($type)) {
list ($type, $id, $params) = static::_split_triad($type);
}
Expand Down
2 changes: 1 addition & 1 deletion modules/tag/classes/Tag/Controller/Rest/TagItems.php
Expand Up @@ -135,7 +135,7 @@ function($type, $id, $params) {
* @see Controller_Rest_Tag::delete()
*/
static function delete($id, $params) {
return Rest::delete("tag", $id, $params);
return Rest::resource_func("delete", "tag", $id, $params);
}

/**
Expand Down

0 comments on commit e31f396

Please sign in to comment.