Skip to content

Commit

Permalink
Backporting to PHP 5.3.
Browse files Browse the repository at this point in the history
Improving readme.
  • Loading branch information
deceze committed Nov 13, 2012
1 parent 46b0994 commit e2a895d
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 60 deletions.
20 changes: 10 additions & 10 deletions RisonDecoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ class RisonDecoder extends Rison {

protected $whitespace = '',
$idRegex = null,
$tokens = [],
$bangs = [];
$tokens = array(),
$bangs = array();

public function __construct($rison) {
$this->rison = $rison;
Expand All @@ -30,20 +30,20 @@ public function __construct($rison) {
}

protected function init() {
$this->tokens = [
$this->tokens = array(
'!' => array($this, 'parseBang'),
'(' => array($this, 'parseObject'),
"'" => array($this, 'parseStringLiteral'),
'-' => array($this, 'parseNumber')
];
);
$this->tokens += array_fill_keys(range(0, 9), $this->tokens['-']);

$this->bangs = [
$this->bangs = array(
't' => true,
'f' => false,
'n' => null,
'(' => array($this, 'parseArray')
];
);

$this->idRegex = "/[^{$this->notIdstart}{$this->notIdchar}][^{$this->notIdchar}]*/";
}
Expand Down Expand Up @@ -90,7 +90,7 @@ protected function parseBang() {
}

protected function parseObject() {
$obj = [];
$obj = array();

while (($c = $this->next()) !== ')') {
if ($obj) {
Expand Down Expand Up @@ -124,7 +124,7 @@ protected function parseObject() {
}

protected function parseArray() {
$array = [];
$array = array();

while (($c = $this->next()) !== ')') {
if ($c === false) {
Expand Down Expand Up @@ -175,11 +175,11 @@ protected function parseNumber() {
$state = 'int';
$permittedSigns = '-';

static $transitions = [
static $transitions = array(
'int+.' => 'frac',
'int+e' => 'exp',
'frac+e' => 'exp'
];
);

do {
$c = substr($this->rison, $i++, 1);
Expand Down
8 changes: 4 additions & 4 deletions RisonEncoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ class RisonEncoder extends Rison {

protected $value = null;

protected $encoders = [];
protected $encoders = array();

public function __construct($value) {
$this->value = $value;
$this->init();
}

protected function init() {
$this->encoders = [
$this->encoders = array(
'boolean' => array($this, 'encodeBoolean'),
'integer' => array($this, 'encodeInteger'),
'double' => array($this, 'encodeDouble'),
Expand All @@ -26,7 +26,7 @@ protected function init() {
'object' => array($this, 'encodeObject'),
'resource' => array($this, 'encodeResource'),
'null' => array($this, 'encodeNull'),
];
);

$this->idOkRegex = "/^[^{$this->notIdstart}{$this->notIdchar}][^{$this->notIdchar}]*\$/";
}
Expand Down Expand Up @@ -88,7 +88,7 @@ protected function encodeArray(array $array) {
protected function encodeObject($object) {
$object = (array)$object;
ksort($object);
$encoded = [];
$encoded = array();

foreach ($object as $key => $value) {
$encoded[] = $this->encodeValue($key) . ':' . $this->encodeValue($value);
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "kunststube/rison",
"description": "A Rison encoder and decoder for PHP.",
"description": "A PHP encoder and decoder for Rison, the compact JSON-like data format optimized for URIs.",
"keywords": ["rison", "encode", "decode"],
"homepage": "https://github.com/deceze/Kunststube-Rison",
"authors": [
Expand All @@ -14,7 +14,7 @@
"psr-0": { "Kunststube\\Rison": "" }
},
"require": {
"php": ">=5.4.0"
"php": ">=5.3.0"
},
"target-dir": "Kunststube/Rison"
}
77 changes: 46 additions & 31 deletions readme.md
Original file line number Diff line number Diff line change
@@ -1,59 +1,74 @@
Kunststube\Rison encoder and decoder for PHP
============================================

Rison is a compact data format optimized for URIs, a slight variation of JSON. See http://mjtemplate.org/examples/rison.html for more information and other implementations.
Rison is a compact data format optimized for URIs, a slight variation of JSON.

Requires PHP 5.4 (mostly because of the simplified array syntax).
JSON:

{"a":0,"b":"foo","c":"23skidoo"}

URI-encoded JSON:

%7B%22a%22:0,%22b%22%3A%22foo%22%2C%22c%22%3A%2223skidoo%22%7D

Rison:

(a:0,b:foo,c:'23skidoo')

URI-encoded Rison:

(a:0,b:foo,c:'23skidoo')

See http://mjtemplate.org/examples/rison.html for more information and other implementations.

Usage
-----

### Procedural/convenience wrapper ###

```php
require_once 'Rison/rison_functions.php';

$data = array('foo', 'bar' => array('baz'));

// encoding
$rison = Kunststube\Rison\rison_encode($data);
var_dump($rison);

// decoding
$data = Kunststube\Rison\rison_decode($rison);
var_dump($data);
```

### Object oriented ###

```php
require_once 'Rison/RisonEncoder.php';
require_once 'Rison/RisonDecoder.php';

use Kunststube\Rison;

// decoding
try {
$rison = '(baz:!(1,1.2e41,0.42,(a:!t,0:!f,1:!n)),foo:bar)';
$decoder = new Rison\RisonDecoder($rison);
$data = $decoder->decode();

var_dump($data);
} catch (Rison\RisonParseErrorException $e) {
echo $e->getMessage(), ' in string: ', $e->getRison();
} catch (InvalidArgumentException $e) {
echo $e->getMessage();
}
$data = array('foo', 'bar' => array('baz'));

// encoding
try {
$data = array('foo', 'bar' => array('baz'));
$encoder = new Rison\RisonEncoder($data);
$rison = $encoder->encode();

var_dump($rison);
} catch (InvalidArgumentException $e) {
echo $e->getMessage();
}
```

### Procedural/convenience wrapper ###

```php
require_once 'Rison/rison_functions.php';

// decoding
$rison = '(baz:!(1,1.2e41,0.42,(a:!t,0:!f,1:!n)),foo:bar)';
$data = Kunststube\Rison\rison_decode($rison);
var_dump($data);

// encoding
$data = array('foo', 'bar' => array('baz'));
$rison = Kunststube\Rison\rison_encode($data);
var_dump($rison);
try {
$decoder = new Rison\RisonDecoder($rison);
$data = $decoder->decode();
var_dump($data);
} catch (Rison\RisonParseErrorException $e) {
echo $e->getMessage(), ' in string: ', $e->getRison();
} catch (InvalidArgumentException $e) {
echo $e->getMessage();
}
```

PSR-0
Expand All @@ -64,7 +79,7 @@ The repository is organized so its contents can be dumped into a folder `Kunstst
Information
-----------

Version: 0.91
Version: 0.92
Author: David Zentgraf
Contact: rison@kunststube.net
License: Public Domain
14 changes: 7 additions & 7 deletions tests/Rison/RisonDecoderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public function testObjectsOOP() {
}

public function testSimpleObject() {
$php = ['a' => 0, 'b' => 1];
$php = array('a' => 0, 'b' => 1);
$rison = '(a:0,b:1)';
$json = '{"a":0,"b":1}';

Expand All @@ -28,7 +28,7 @@ public function testSimpleObject() {
}

public function testComplexObject() {
$php = ['a' => 0, 'b' => 'foo', 'c' => '23skidoo'];
$php = array('a' => 0, 'b' => 'foo', 'c' => '23skidoo');
$rison = "(a:0,b:foo,c:'23skidoo')";
$json = '{"a":0,"b":"foo","c":"23skidoo"}';

Expand Down Expand Up @@ -145,7 +145,7 @@ public function testAbcDef() {
}

public function testEmptyObject() {
$php = [];
$php = array();
$rison = '()';
$json = '{}';

Expand All @@ -154,7 +154,7 @@ public function testEmptyObject() {
}

public function testSingleObject() {
$php = ['a' => 0];
$php = array('a' => 0);
$rison = '(a:0)';
$json = '{"a":0}';

Expand All @@ -163,7 +163,7 @@ public function testSingleObject() {
}

public function testComplexQuoteObject() {
$php = ['id' => null, 'type' => '/common/document'];
$php = array('id' => null, 'type' => '/common/document');
$rison = '(id:!n,type:/common/document)';
$json = '{"id":null,"type":"/common/document"}';

Expand All @@ -172,7 +172,7 @@ public function testComplexQuoteObject() {
}

public function testEmptyArray() {
$php = [];
$php = array();
$rison = '!()';
$json = '[]';

Expand All @@ -181,7 +181,7 @@ public function testEmptyArray() {
}

public function testPrimitiveTypeArray() {
$php = [true, false, null, ''];
$php = array(true, false, null, '');
$rison = "!(!t,!f,!n,'')";
$json = '[true,false,null,""]';

Expand Down
12 changes: 6 additions & 6 deletions tests/Rison/RisonEncoderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ public function testObjects() {
}

public function testSimpleObject() {
$php = ['a' => 0, 'b' => 1];
$php = array('a' => 0, 'b' => 1);
$rison = '(a:0,b:1)';

$this->assertEquals($rison, R\rison_encode($php));
}

public function testComplexObject() {
$php = ['a' => 0, 'b' => 'foo', 'c' => '23skidoo'];
$php = array('a' => 0, 'b' => 'foo', 'c' => '23skidoo');
$rison = "(a:0,b:foo,c:'23skidoo')";

$this->assertEquals($rison, R\rison_encode($php));
Expand Down Expand Up @@ -116,28 +116,28 @@ public function testAbcDef() {
}

public function testEmptyObject() {
$php = [];
$php = array();
$rison = '()';

$this->assertEquals($rison, R\rison_encode($php));
}

public function testSingleObject() {
$php = ['a' => 0];
$php = array('a' => 0);
$rison = '(a:0)';

$this->assertEquals($rison, R\rison_encode($php));
}

public function testComplexQuoteObject() {
$php = ['id' => null, 'type' => '/common/document'];
$php = array('id' => null, 'type' => '/common/document');
$rison = '(id:!n,type:/common/document)';

$this->assertEquals($rison, R\rison_encode($php));
}

public function testPrimitiveTypeArray() {
$php = [true, false, null, ''];
$php = array(true, false, null, '');
$rison = "!(!t,!f,!n,'')";

$this->assertEquals($rison, R\rison_encode($php));
Expand Down

0 comments on commit e2a895d

Please sign in to comment.