Remove __invoke, add tests, fix closures

This commit is contained in:
Timothy Warren 2012-05-23 15:52:49 -04:00
parent f604e0f04a
commit c786004f22
3 changed files with 109 additions and 23 deletions

View File

@ -17,7 +17,7 @@
*
* @package JSObject
*/
class JSObject {
class JSObject extends ArrayObject {
/**
* Create the object
@ -26,12 +26,14 @@ class JSObject {
*/
public function __construct($params = [])
{
parent::__construct($params, ArrayObject::ARRAY_AS_PROPS);
foreach($params as $name => &$val)
{
// Bind '$this' for closures
if ($val instanceof Closure)
{
$val->bindTo($this);
$val = $val->bindTo($this);
}
// Add the parameter to the object
@ -39,6 +41,8 @@ class JSObject {
}
}
// --------------------------------------------------------------------------
/**
* Magic method to invoke appended closures
*
@ -48,6 +52,14 @@ class JSObject {
*/
public function __call($name, $params = [])
{
// Allow array operations on the object
if (substr($name, 0, 6) === 'array_' && is_callable($name))
{
$args = array_merge($this->getArrayCopy(), $params);
return call_user_func_array($name, [$args]);
}
// Call closures attached to the object
if (is_callable($this->$name))
{
return call_user_func_array($this->$name, $params);
@ -55,25 +67,6 @@ class JSObject {
return NULL;
}
/**
* Treat invokation of the object as creating a new object
*
* @param mixed
* @return JSObject
*/
public function __invoke($params = [])
{
$class = __CLASS__;
// Create the new object
$obj = new $class();
// Pass the parameters to the constructor of the new object
$obj = call_user_func_array([$obj, '__construct'], $params);
return $obj;
}
}
// End of JSObject.php

View File

@ -2,3 +2,32 @@ JSObject
========
A PHP 5.4 class to emulate Javascript object literals.
Also, can use ``array_`` functions to operate on the object's properties and values. (Only works for functions that start with ``array_`` and have the array as the first parameter)
Examples:
* Basic Usage
$obj = new JSObject([
'a' => 10,
'b' => 5,
'c' => 0,
'x' => function() {
return $this->a * $this->b;
}
]);
$obj->x(); // Returns 50
* Get an array of the properties of the object:
$obj = new JSObject([
'x' => 'foo',
'y' => 'bar'
]);
$obj->array_keys() // Returns: array('x', 'y')

64
tests.php Normal file
View File

@ -0,0 +1,64 @@
<?php
/**
* JSObject tests
*
* Requires simpletest - Either in PHP path,
* or in the folder with the test file
*/
// --------------------------------------------------------------------------
// Include simpletest
// it has to be set in your php path, or put in the tests folder
require_once('simpletest/autorun.php');
// Include JSObject
require_once('JSObject.php');
class JSObjectTests extends UnitTestCase {
function TestIsA()
{
$obj = new JSObject([
'x' => [0,1,2]
]);
$this->assertIsA($obj, 'ArrayObject');
$this->assertIsA($obj, 'JSObject');
}
function TestClosure()
{
$obj = new JSObject([
'a' => 10,
'b' => 5,
'c' => 0,
'x' => function() {
return $this->a * $this->b;
}
]);
$this->assertEqual($obj->x(), 50);
}
function TestArrayFlip()
{
$obj = new JSObject([
'x' => 'foo',
'y' => 'bar'
]);
$this->assertEqual($obj->array_flip(), ['foo' => 'x', 'bar' => 'y']);
}
function TestArrayKeys()
{
$obj = new JSObject([
'x' => 'foo',
'y' => 'bar'
]);
$this->assertEqual($obj->array_keys(), ['x','y']);
}
}