2012-05-23 11:50:20 -04:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* JSObject
|
|
|
|
*
|
|
|
|
* A PHP class to have an analoge to javascript's object literals
|
|
|
|
*
|
|
|
|
* @package JSObject
|
|
|
|
* @author Timothy J. Warren
|
|
|
|
* @copyright Copyright (c) 2012
|
|
|
|
* @link https://github.com/aviat4ion/JSObject
|
|
|
|
*/
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------
|
2012-05-25 08:33:16 -04:00
|
|
|
// ! Shortcut Function
|
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns an instance of a JSObject instantiated with the passed parameters
|
|
|
|
*
|
|
|
|
* @param mixed
|
|
|
|
* @return JSObject
|
|
|
|
*/
|
|
|
|
function JSObject()
|
|
|
|
{
|
|
|
|
// Create the object
|
|
|
|
$obj = new JSObject();
|
|
|
|
|
|
|
|
// Set the parameters
|
|
|
|
call_user_func_array([$obj, '__construct'], func_get_args());
|
|
|
|
return $obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
// -------------------------------------------------------------------------
|
2012-05-23 11:50:20 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* JSObject class
|
|
|
|
*
|
|
|
|
* @package JSObject
|
|
|
|
*/
|
2012-05-23 15:52:49 -04:00
|
|
|
class JSObject extends ArrayObject {
|
2012-05-23 11:50:20 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Create the object
|
|
|
|
*
|
|
|
|
* @param mixed
|
|
|
|
*/
|
|
|
|
public function __construct($params = [])
|
|
|
|
{
|
2012-05-23 15:52:49 -04:00
|
|
|
parent::__construct($params, ArrayObject::ARRAY_AS_PROPS);
|
|
|
|
|
2012-05-24 12:07:57 -04:00
|
|
|
foreach($params as $name => $val)
|
2012-05-23 11:50:20 -04:00
|
|
|
{
|
2012-05-24 11:59:15 -04:00
|
|
|
if (empty($val)) continue;
|
|
|
|
|
2012-05-23 11:50:20 -04:00
|
|
|
// Bind '$this' for closures
|
|
|
|
if ($val instanceof Closure)
|
|
|
|
{
|
2012-05-23 15:52:49 -04:00
|
|
|
$val = $val->bindTo($this);
|
2012-05-23 11:50:20 -04:00
|
|
|
}
|
2012-05-23 15:52:49 -04:00
|
|
|
|
2012-05-23 11:50:20 -04:00
|
|
|
// Add the parameter to the object
|
|
|
|
$this->$name = $val;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-05-23 15:52:49 -04:00
|
|
|
// --------------------------------------------------------------------------
|
|
|
|
|
2012-05-23 11:50:20 -04:00
|
|
|
/**
|
|
|
|
* Magic method to invoke appended closures
|
|
|
|
*
|
|
|
|
* @param string
|
|
|
|
* @param array
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function __call($name, $params = [])
|
|
|
|
{
|
2012-05-24 11:59:15 -04:00
|
|
|
$function_blacklist = [
|
|
|
|
'array_change_key_case',
|
|
|
|
'array_combine',
|
|
|
|
'array_count_values',
|
|
|
|
'array_fill_keys',
|
|
|
|
'array_fill',
|
|
|
|
'array_key_exists',
|
|
|
|
'array_map',
|
|
|
|
'array_merge',
|
|
|
|
'array_merge_recursive',
|
|
|
|
'array_search',
|
|
|
|
'array_unshift',
|
|
|
|
];
|
|
|
|
|
2012-05-23 15:52:49 -04:00
|
|
|
// Allow array operations on the object
|
2012-05-24 11:59:15 -04:00
|
|
|
if (substr($name, 0, 6) === 'array_' && is_callable($name) && ! in_array($name, $function_blacklist))
|
2012-05-23 15:52:49 -04:00
|
|
|
{
|
2012-05-24 11:59:15 -04:00
|
|
|
$args = ( ! empty($params))
|
|
|
|
? array_merge($this->getArrayCopy(), $params)
|
|
|
|
: $this->getArrayCopy();
|
|
|
|
|
|
|
|
// Make sure the array items in the array parameter aren't used as function parameters
|
|
|
|
if (count($args === 1))
|
|
|
|
{
|
|
|
|
$args = [$args];
|
|
|
|
}
|
|
|
|
|
|
|
|
return call_user_func_array($name, $args);
|
2012-05-23 15:52:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Call closures attached to the object
|
2012-05-23 11:50:20 -04:00
|
|
|
if (is_callable($this->$name))
|
|
|
|
{
|
|
|
|
return call_user_func_array($this->$name, $params);
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// End of JSObject.php
|