Skip to content

Instantly share code, notes, and snippets.

View geraintluff's full-sized avatar

Geraint geraintluff

View GitHub Profile
<?php
require_once('json-utils.php');
abstract class DbData {
abstract public function loadFromRow($row);
public static function match($template) {
$params = array();
$pathInfo = $_SERVER['PATH_INFO'];
@geraintluff
geraintluff / match.php
Last active December 11, 2015 06:39
Extracts variables from $_SERVER['PATH_INFO'] using templates: /test/{id}/comments/{commentId}
function matchUriTemplate($template, $pathInfo=NULL, $stripQuery=TRUE) {
$params = array();
if ($pathInfo == NULL) {
$pathInfo = $_SERVER['PATH_INFO'];
}
if ($stripQuery) {
$pos = strpos($pathInfo, "?");
if ($pos !== FALSE) {
$pathInfo = substr($pathInfo, 0, $pos);
}
@geraintluff
geraintluff / error-schema.json
Last active September 7, 2022 01:27
A small set of useful functions for writing JSON APIs in PHP.
{
"title": "Error (HTTP)",
"type": "object",
"properties": {
"statusCode": {"type": "integer"},
"statusText": {"type": "string"},
"message": {"type": "string"},
"data": {"title": "Supplementary data"}
},
"required": ["statusCode", "statusText", "message"]
Array.prototype.unique = function () {
// make sure we have a unique string that is not a defined key in any object
var uniqueString = "" + Math.random();
for (var i = 0; i < this.length; i++) {
if (typeof this[i] == "object" && this[i][uniqueString] != undefined) {
uniqueString = "" + Math.random();
i = -1;
continue;
}
}
@geraintluff
geraintluff / data.json
Last active December 10, 2015 19:58 — forked from anonymous/data.json
{
"id": 123,
"title": "Example data",
"actions": {
"confirmAddress": "/item/123/confirmAddress",
"edit": "/item/123",
"addComment": "/item/123/comments"
}
}