Permalink
Browse files

Backporting to PHP 5.3.

Improving readme.
  • Loading branch information...
1 parent 46b0994 commit e2a895d82fa7a8d02823d1b595897f6aa3345076 @deceze committed Nov 13, 2012
Showing with 75 additions and 60 deletions.
  1. +10 −10 RisonDecoder.php
  2. +4 −4 RisonEncoder.php
  3. +2 −2 composer.json
  4. +46 −31 readme.md
  5. +7 −7 tests/Rison/RisonDecoderTest.php
  6. +6 −6 tests/Rison/RisonEncoderTest.php
View
@@ -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;
@@ -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}]*/";
}
@@ -90,7 +90,7 @@ protected function parseBang() {
}
protected function parseObject() {
- $obj = [];
+ $obj = array();
while (($c = $this->next()) !== ')') {
if ($obj) {
@@ -124,7 +124,7 @@ protected function parseObject() {
}
protected function parseArray() {
- $array = [];
+ $array = array();
while (($c = $this->next()) !== ')') {
if ($c === false) {
@@ -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);
View
@@ -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'),
@@ -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}]*\$/";
}
@@ -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);
View
@@ -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": [
@@ -14,7 +14,7 @@
"psr-0": { "Kunststube\\Rison": "" }
},
"require": {
- "php": ">=5.4.0"
+ "php": ">=5.3.0"
},
"target-dir": "Kunststube/Rison"
}
View
@@ -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
@@ -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
@@ -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}';
@@ -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"}';
@@ -145,7 +145,7 @@ public function testAbcDef() {
}
public function testEmptyObject() {
- $php = [];
+ $php = array();
$rison = '()';
$json = '{}';
@@ -154,7 +154,7 @@ public function testEmptyObject() {
}
public function testSingleObject() {
- $php = ['a' => 0];
+ $php = array('a' => 0);
$rison = '(a:0)';
$json = '{"a":0}';
@@ -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"}';
@@ -172,7 +172,7 @@ public function testComplexQuoteObject() {
}
public function testEmptyArray() {
- $php = [];
+ $php = array();
$rison = '!()';
$json = '[]';
@@ -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,""]';
@@ -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));
@@ -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));

0 comments on commit e2a895d

Please sign in to comment.