Skip to content

Commit

Permalink
Added PATCH verb/method + refactoring (extracted service interface + …
Browse files Browse the repository at this point in the history
…abstracted BaseRestServiceTB.php verb methods) (#1)
  • Loading branch information
whatever22 authored and kstefanini committed Mar 9, 2018
1 parent 9f607f4 commit db1e154
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 3 deletions.
54 changes: 51 additions & 3 deletions BaseRestServiceTB.php
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;
Expand Down Expand Up @@ -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() {
}
Expand All @@ -75,6 +120,9 @@ public function run() {
case "PUT":
$this->put();
break;
case "PATCH":
$this->patch();
break;
case "DELETE":
$this->delete();
break;
Expand Down Expand Up @@ -166,7 +214,7 @@ protected function getParam($name, $default=null, $collection=null) {
return $default;
}
}

/**
* Reads and returns request body contents
*/
Expand Down
10 changes: 10 additions & 0 deletions RestServiceTB.php
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();

}

0 comments on commit db1e154

Please sign in to comment.