Skip to content

Commit

Permalink
BackwardsCompatibilityBreak - fRecordSet::registerCallback() was rena…
Browse files Browse the repository at this point in the history
…med to fRecordSet::registerMethodCallback(). fORMJSON refactored slightly.
  • Loading branch information
wbond committed Apr 12, 2012
1 parent dc35e6a commit 095c09a
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 39 deletions.
39 changes: 6 additions & 33 deletions classes/fORMJSON.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -32,13 +32,7 @@ static public function extend()
array('fORMJSON', 'toJSON') array('fORMJSON', 'toJSON')
); );


fORM::registerHookCallback( fRecordSet::registerMethodCallback(
'*',
'replace::toArray()',
array('fORMJSON', 'toArray')
);

fRecordSet::registerCallback(
'toJSON', 'toJSON',
array('fORMJSON', 'toJSONRecordSet') array('fORMJSON', 'toJSONRecordSet')
); );
Expand Down Expand Up @@ -72,7 +66,7 @@ static public function reflect($class, &$signatures, $include_doc_comments)




/** /**
* Returns the values array - used by {@link toJSONRecordSet()} * Returns a JSON object representation of the record
* *
* @internal * @internal
* *
Expand All @@ -84,38 +78,17 @@ static public function reflect($class, &$signatures, $include_doc_comments)
* @param array &$parameters The parameters passed to the method * @param array &$parameters The parameters passed to the method
* @return string The JSON object that represents the values of this record * @return string The JSON object that represents the values of this record
*/ */
static public function toArray($object, &$values, &$old_values, &$related_records, &$method_name, &$parameters) static public function toJSON($object, &$values, &$old_values, &$related_records, &$method_name, &$parameters)
{ {
$output = array(); $output = array();
foreach ($values as $column => $value) { foreach ($values as $column => $value) {
if (is_object($value) && is_callable(array($value, '__toString'))) { if (is_object($value) && is_callable(array($value, '__toString'))) {
$value = $value->__toString(); $value = $value->__toString();
} }
$output[$column] = $value; $output[$column] = $value;
} }


return $output; return fJSON::encode($output);
}


/**
* Returns a JSON object representation of the record
*
* @internal
*
* @param fActiveRecord $object The fActiveRecord instance
* @param array &$values The current values
* @param array &$old_values The old values
* @param array &$related_records Any records related to this record
* @param string &$method_name The method that was called
* @param array &$parameters The parameters passed to the method
* @return string The JSON object that represents the values of this record
*/
static public function toJSON($object, &$values, &$old_values, &$related_records, &$method_name, &$parameters)
{
$method = 'toArray';
$params = array();
return fJSON::encode(self::toArray($object, $values, $old_values, $related_records, $method, $params));
} }




Expand All @@ -133,7 +106,7 @@ static public function toJSON($object, &$values, &$old_values, &$related_records
*/ */
static public function toJSONRecordSet($record_set, $class, &$records, &$pointer, &$associate) static public function toJSONRecordSet($record_set, $class, &$records, &$pointer, &$associate)
{ {
return fJSON::encode($record_set->call('toArray')); return '[' . join(',', $record_set->call('toJSON')) . ']';
} }




Expand Down
12 changes: 6 additions & 6 deletions classes/fRecordSet.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class fRecordSet implements Iterator
* *
* @var array * @var array
*/ */
static private $callbacks = array(); static private $method_callbacks = array();




/** /**
Expand Down Expand Up @@ -319,7 +319,7 @@ static public function configure($class)




/** /**
* Registers a callback to be called when a specific method name is handled by __call() * Registers a callback to be called when a specific method is handled by __call()
* *
* The callback should accept the following parameters: * The callback should accept the following parameters:
* - $record_set: The actual record set * - $record_set: The actual record set
Expand All @@ -332,9 +332,9 @@ static public function configure($class)
* @param callback $callback The callback to execute - see method description for parameter list * @param callback $callback The callback to execute - see method description for parameter list
* @return void * @return void
*/ */
static public function registerCallback($method, $callback) static public function registerMethodCallback($method, $callback)
{ {
self::$callbacks[$method] = $callback; self::$method_callbacks[$method] = $callback;
} }




Expand Down Expand Up @@ -394,9 +394,9 @@ public function __call($method_name, $parameters)
{ {
list($action, $element) = explode('_', fGrammar::underscorize($method_name), 2); list($action, $element) = explode('_', fGrammar::underscorize($method_name), 2);


if (isset(self::$callbacks[$method_name])) { if (isset(self::$method_callbacks[$method_name])) {
return call_user_func_array( return call_user_func_array(
self::$callbacks[$method_name], self::$method_callbacks[$method_name],
array( array(
$this, $this,
$this->class, $this->class,
Expand Down

0 comments on commit 095c09a

Please sign in to comment.