Skip to content

Instantly share code, notes, and snippets.

View geraintluff's full-sized avatar

Geraint geraintluff

View GitHub Profile
@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"
}
}
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 / 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"]
@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);
}
<?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'];
{
"properties": {
"someProperty": {
"id": "http://other.site/schema",
"type": "array",
"items": {"$ref": "itemSchema"}
}
}
}
@geraintluff
geraintluff / example1.schema.json
Created January 29, 2013 14:29
When using canonical addressing, these two schemas should behave *exactly* the same. Canonical addressing means that the use of "id" has *no* effect on schema lookup/dereferencing- so *neither* of these examples would have any effect on references to "http://example.com/other-schema#" anywhere else.
{
"title": "Some schema",
"items": {
"id": "http://example.com/other-schema",
"type": "array",
"items": {"$ref": "#"}
}
}
{
"responseStatus": {
"status": "success",
"message": "This is a valid message"
},
"data": {
"total": 2,
"collection": [
{
"id": 1,
{
"main": "food",
"sub": "bread"
}
{
"items": {
"allOf": [
{...}
]
},
"definitions": {
"myFragment": {"$ref": "#/items/allOf/0"}
}
}