Skip to content

Commit

Permalink
Implement the JsonSerializable interface in JRegistry while maintaini…
Browse files Browse the repository at this point in the history
…ng compatability with PHP 5.3.

This allows us to pass JRegistry objects to json_encode.
  • Loading branch information
realityking committed Jul 3, 2012
1 parent 67248cb commit 269dc02
Show file tree
Hide file tree
Showing 5 changed files with 135 additions and 4 deletions.
29 changes: 29 additions & 0 deletions libraries/compat/jsonserializable.php
@@ -0,0 +1,29 @@
<?php
/**
* @package Joomla.Compat
*
* @copyright Copyright (C) 2005 - 2012 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE
*/

defined('JPATH_PLATFORM') or die;

/**
* JsonSerializable interface. This file should only be loaded on PHP < 5.4
* It allows us to implement it in classes without requiring PHP 5.4
*
* @package Joomla.Platform
* @since 12.2
* @link http://www.php.net/manual/en/jsonserializable.jsonserialize.php
*/
interface JsonSerializable
{
/**
* Return data which should be serialized by json_encode().
*
* @return mixed
*
* @since 12.2
*/
public function jsonSerialize();
}
6 changes: 6 additions & 0 deletions libraries/import.legacy.php
Expand Up @@ -57,6 +57,12 @@ class_exists('JLoader') or die;
// Register the folder for the moved JHtml classes
JHtml::addIncludePath(JPATH_PLATFORM . '/legacy/html');

// Register classes for compatability with PHP 5.3
if (version_compare(PHP_VERSION, '5.4.0', '<'))
{
JLoader::register('JsonSerializable', __DIR__ . '/compat/jsonserializable.php');
}

// Add deprecated constants
// @deprecated 12.3
define('JPATH_ISWIN', IS_WIN);
Expand Down
6 changes: 6 additions & 0 deletions libraries/import.php
Expand Up @@ -47,6 +47,12 @@ class_exists('JLoader') or die;
// Import the base Joomla Platform libraries.
JLoader::import('joomla.factory');

// Register classes for compatability with PHP 5.3
if (version_compare(PHP_VERSION, '5.4.0', '<'))
{
JLoader::register('JsonSerializable', __DIR__ . '/compat/jsonserializable.php');
}

// Register classes that don't follow one file per class naming conventions.
JLoader::register('JText', JPATH_PLATFORM . '/joomla/language/text.php');
JLoader::register('JRoute', JPATH_PLATFORM . '/joomla/application/route.php');
16 changes: 15 additions & 1 deletion libraries/joomla/registry/registry.php
Expand Up @@ -18,7 +18,7 @@
* @subpackage Registry
* @since 11.1
*/
class JRegistry
class JRegistry implements JsonSerializable
{
/**
* Registry Object
Expand Down Expand Up @@ -81,6 +81,20 @@ public function __toString()
return $this->toString();
}

/**
* Implementation for the JsonSerializable interface.
* Allows us to pass JRegistry objects to json_encode.
*
* @return object
*
* @since 12.2
* @note The interface is only present in PHP 5.4 and up.
*/
public function jsonSerialize()
{
return $this->data;
}

/**
* Sets a default value if not already assigned.
*
Expand Down
82 changes: 79 additions & 3 deletions tests/suites/unit/joomla/registry/JRegistryTest.php
Expand Up @@ -28,7 +28,8 @@ public function setUp()

/**
* Test the JRegistry::__clone method.
* @
*
* @covers JRegistry::__clone
*/
public function test__clone()
{
Expand All @@ -50,6 +51,8 @@ public function test__clone()

/**
* Test the JRegistry::__toString method.
*
* @covers JRegistry::__toString
*/
public function test__toString()
{
Expand All @@ -65,7 +68,54 @@ public function test__toString()
);
}

/**
* Test the JRegistry::jsonSerialize method.
*
* @covers JRegistry::jsonSerialize
*/
public function testJsonSerialize()
{
if (version_compare(PHP_VERSION, '5.4.0', '<'))
{
$this->markTestSkipped('This test requires PHP 5.4 or newer.');
}

$object = new stdClass();
$a = new JRegistry($object);
$a->set('foo', 'bar');

// __toString only allows for a JSON value.
$this->assertThat(
json_encode($a),
$this->equalTo('{"foo":"bar"}'),
'Line: '.__LINE__.'.'
);
}

/**
* Tests serializing JRegistry objects.
*/
public function testSerialize()
{
$a = new JRegistry();
$a->set('foo', 'bar');

$serialized = serialize($a);
$b = unserialize($serialized);

// __toString only allows for a JSON value.
$this->assertThat(
$b,
$this->equalTo($a),
'Line: '.__LINE__.'.'
);
}

/**
* Test the JRegistry::def method.
*
* @covers JRegistry::def
*/
public function testDef()
{
$a = new JRegistry();
Expand All @@ -85,6 +135,8 @@ public function testDef()

/**
* Tet the JRegistry::bindData method.
*
* @covers JRegistry::bindData
*/
public function testBindData()
{
Expand Down Expand Up @@ -121,7 +173,9 @@ public function testBindData()
}

/**
* Test the JReigstry::exists method.
* Test the JRegistry::exists method.
*
* @covers JRegistry::exists
*/
public function testExists()
{
Expand Down Expand Up @@ -162,7 +216,9 @@ public function testExists()
}

/*
* Test the JRegistry get method
* Test the JRegistry::get method
*
* @covers JRegistry::get
*/
public function testGet()
{
Expand All @@ -174,6 +230,8 @@ public function testGet()

/**
* Test the JRegistry::getInstance method.
*
* @covers JRegistry::getInstance
*/
public function testGetInstance()
{
Expand Down Expand Up @@ -206,6 +264,8 @@ public function testGetInstance()

/**
* Test the JRegistry::loadArray method.
*
* @covers JRegistry::loadArray
*/
public function testLoadArray()
{
Expand All @@ -227,6 +287,8 @@ public function testLoadArray()

/**
* Test the JRegistry::loadFile method.
*
* @covers JRegistry::loadFile
*/
public function testLoadFile()
{
Expand Down Expand Up @@ -269,6 +331,8 @@ public function testLoadFile()

/**
* Test the JRegistry::loadString() method.
*
* @covers JRegistry::loadString
*/
public function testLoadString()
{
Expand Down Expand Up @@ -316,6 +380,8 @@ public function testLoadString()

/**
* Test the JRegistry::loadObject method.
*
* @covers JRegistry::loadObject
*/
public function testLoadObject()
{
Expand Down Expand Up @@ -345,6 +411,8 @@ public function testLoadObject()

/**
* Test the JRegistry::merge method.
*
* @covers JRegistry::merge
*/
public function testMerge()
{
Expand Down Expand Up @@ -402,6 +470,8 @@ public function testMerge()

/**
* Test the JRegistry::set method.
*
* @covers JRegistry::set
*/
public function testSet()
{
Expand All @@ -417,6 +487,8 @@ public function testSet()

/**
* Test the JRegistry::toArray method.
*
* @covers JRegistry::toArray
*/
public function testToArray()
{
Expand All @@ -440,6 +512,8 @@ public function testToArray()

/**
* Test the JRegistry::toObject method.
*
* @covers JRegistry::toObject
*/
public function testToObject()
{
Expand All @@ -463,6 +537,8 @@ public function testToObject()

/**
* Test the JRegistry::toString method.
*
* @covers JRegistry::toString
*/
public function testToString()
{
Expand Down

0 comments on commit 269dc02

Please sign in to comment.