From c786004f22fe2cb40be37aecf551b4ed68a8af50 Mon Sep 17 00:00:00 2001 From: Timothy Warren Date: Wed, 23 May 2012 15:52:49 -0400 Subject: [PATCH] Remove __invoke, add tests, fix closures --- JSObject.php | 37 ++++++++++++------------------ README.md | 31 ++++++++++++++++++++++++- tests.php | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 109 insertions(+), 23 deletions(-) create mode 100644 tests.php diff --git a/JSObject.php b/JSObject.php index 4381f22..cc0a893 100644 --- a/JSObject.php +++ b/JSObject.php @@ -17,7 +17,7 @@ * * @package JSObject */ -class JSObject { +class JSObject extends ArrayObject { /** * Create the object @@ -26,19 +26,23 @@ 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 $this->$name = $val; } } + // -------------------------------------------------------------------------- + /** * 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 \ No newline at end of file diff --git a/README.md b/README.md index 4394db2..9410d0d 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,33 @@ JSObject ======== -A PHP5.4 class to emulate Javascript object literals. \ No newline at end of file +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') + + + diff --git a/tests.php b/tests.php new file mode 100644 index 0000000..eb2e223 --- /dev/null +++ b/tests.php @@ -0,0 +1,64 @@ + [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']); + } + +} \ No newline at end of file