Location: PHPKode > projects > Porte > porte-0.2.2/src/plugins/Json.php
<?php

/**
 * Copyright (c) 2008, SARL Adaltas. All rights reserved.
 * Code licensed under the BSD License:
 * http://porte.adaltas.com/en/developer/license.html
 */

/**
 * PorteJson
 *
 * Add json import and export support to records or list of records by registering two new method "fromJson"
 * and "toJson" to each record.
 * 
 * @package    Porte
 * @subpackage plugin
 * @author     David Worms info(at)adaltas.com
 * @copyright  2008 Adaltas
 */
class PorteJson{
	
	public static function _modelTableAfter($models,$type){
		PorteModel::addMethod($models,$type,'fromJson',null,array('PorteJson','fromJson'));
		PorteModel::addMethod($models,$type,'toJson',null,array('PorteJson','toJson'));
	}
	
	/**
	 * The fromJson method is a simple wrapper around the fromArray method with which it share all 
	 * its arguments and behavior with the difference that it expect a JSON formated string instead 
	 * of an array as first argument.
	 * 
	 * @return PorteRecord The current recored
	 * @param $json JSON string
	 * @param $options Various options used to alter the behavior of the mapping
	 */
	public static function fromJson($arg,$record,$json,$options=array()){
		return $record->fromArray(json_decode($json,1),$options);
	}
	
	/**
	 * The toJson method is a simple wrapper around the toArray method with which it share all 
	 * its arguments and behavior with the difference that it return a JSON formated string instead 
	 * of an array.
	 * 
	 * @return string JSON formated string
	 * @param $options Various options used to alter the behavior of the mapping
	 */
	public static function toJson($arg,$record,$options=array()){
		return json_encode($record->toArray($options));
	}
	
}

PorteEvents::connect('model_table_after',array('PorteJson','_modelTableAfter'));

?>
Return current item: Porte