-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added PATCH verb/method + refactoring (extracted service interface + …
…abstracted BaseRestServiceTB.php verb methods) (#1)
- Loading branch information
1 parent
9f607f4
commit db1e154
Showing
2 changed files
with
61 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,13 @@ | ||
<?php | ||
|
||
/* | ||
require(dirname(__FILE__) . '/RestServiceTB.php'); | ||
|
||
/* | ||
* Base class for REST services | ||
* @author [email protected] | ||
* @date 08/2015 | ||
*/ | ||
class BaseRestServiceTB { | ||
abstract class BaseRestServiceTB implements RestServiceTB { | ||
|
||
/** Configuration given at construct time */ | ||
protected $config; | ||
|
@@ -55,6 +57,49 @@ public function __construct($config) { | |
$this->init(); | ||
} | ||
|
||
/** | ||
* Responds to an HTTP request issued with the GET method/verb. | ||
* Returns the JSON representation of a single resource (or of all resources) | ||
* depending on wether the resource ID is provided in the URL path (or not). | ||
*/ | ||
abstract protected function get(); | ||
|
||
/** | ||
* Responds to an HTTP request issued with the POST method/verb. | ||
* Creates a new resource built using the POST parameters passed. | ||
*/ | ||
abstract protected function post(); | ||
|
||
/** | ||
* Responds to an HTTP request issued with the PUT method/verb. | ||
* Updates all parameters of the resource identified by the ID provided in | ||
* the URL path if it exists (using the parameters passed). Responds with a | ||
* 404 HTTP response if not. | ||
*/ | ||
abstract protected function put(); | ||
|
||
/** | ||
* Responds to an HTTP request issued with the PATCH method/verb. | ||
* Updates parts of the parameters of the resource identified by the ID | ||
* provided in the URL path if it exists (using the parameters passed). | ||
* Responds with a 404 HTTP response if not. | ||
*/ | ||
abstract protected function patch(); | ||
|
||
/** | ||
* Responds to an HTTP request issued with the DELETE method/verb. | ||
* Removes the resource identified by the ID provided in the URL path if it | ||
* exists. Responds with a 404 HTTP response if not. | ||
*/ | ||
abstract protected function delete(); | ||
|
||
/** | ||
* Responds to an HTTP request issued with the OPTIONS method/verb. | ||
* Responds with a 200 status with an 'Allow' header listing the HTTP methods | ||
* that may be used on this resource. | ||
*/ | ||
abstract protected function options(); | ||
|
||
/** Post-constructor adjustments */ | ||
protected function init() { | ||
} | ||
|
@@ -75,6 +120,9 @@ public function run() { | |
case "PUT": | ||
$this->put(); | ||
break; | ||
case "PATCH": | ||
$this->patch(); | ||
break; | ||
case "DELETE": | ||
$this->delete(); | ||
break; | ||
|
@@ -166,7 +214,7 @@ protected function getParam($name, $default=null, $collection=null) { | |
return $default; | ||
} | ||
} | ||
|
||
/** | ||
* Reads and returns request body contents | ||
*/ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
/* | ||
* Base interface for REST services. | ||
*/ | ||
interface RestServiceTB { | ||
|
||
public function run(); | ||
|
||
} |