<commit_before><?php
/**
* @version		$Id$
* @category		Koowa
* @package      Koowa_Filter
* @copyright    Copyright (C) 2007 - 2010 <NAME>. All rights reserved.
* @license      GNU GPLv3 <http://www.gnu.org/licenses/gpl.html>
* @link 		http://www.nooku.org
*/

/**
 * Integer filter
 *
 * @author		<NAME> <<EMAIL>>
 * @category	Koowa
 * @package     Koowa_Filter
 */
class KFilterJson extends KFilterAbstract
{
	/**
	 * Constructor
	 *
	 * @param 	object	An optional KConfig object with configuration options
	 */
	public function __construct(KConfig $config) 
	{
		parent::__construct($config);
		
		//Don't walk the incoming data array or object
		$this->_walk = false;
	}
	
	/**
	 * Validate a value
	 *
	 * @param	scalar	Value to be validated
	 * @return	bool	True when the variable is valid
	 */
	protected function _validate($value)
	{
		return is_string($value) && !is_null(json_decode($value));
	}
	
	/**
	 * Sanitize a value
	 *
	 * @param	scalar	Value to be sanitized
	 * @return	string
	 */
	protected function _sanitize($value)
	{
		if(is_a($value, 'KConfig')) {
			$value = $value->toArray(); 
		}
		
		return json_encode($value);
	}
}
<commit_msg>KFilterJson is now able to do bi-directional sanitisation. If the value passed for sanitisation is a json encoding string it will be decode, otherwise the value will be encoded.<commit_after><?php
/**
* @version		$Id$
* @category		Koowa
* @package      Koowa_Filter
* @copyright    Copyright (C) 2007 - 2010 <NAME>. All rights reserved.
* @license      GNU GPLv3 <http://www.gnu.org/licenses/gpl.html>
* @link 		http://www.nooku.org
*/

/**
 * Json filter
 * 
 * If the value being sanitized is a json string it will be decoded, otherwise
 * the value will be encoded upon sanitisation. 
 *
 * @author		<NAME> <<EMAIL>>
 * @category	Koowa
 * @package     Koowa_Filter
 */
class KFilterJson extends KFilterAbstract
{
	/**
	 * Constructor
	 *
	 * @param 	object	An optional KConfig object with configuration options
	 */
	public function __construct(KConfig $config) 
	{
		parent::__construct($config);

		//Don't walk the incoming data array or object
		$this->_walk = false;
	}
	
	/**
	 * Validate a value
	 *
	 * @param	scalar	Value to be validated
	 * @return	bool	True when the variable is valid
	 */
	protected function _validate($value)
	{
		return is_string($value) && !is_null(json_decode($value));
	}
	
	/**
	 * Sanitize a value
	 * 
	 * 
	 *
	 * @param	scalar	Value to be sanitized
	 * @return	string
	 */
	protected function _sanitize($value)
	{
		$result = null;
		
		if(is_a($value, 'KConfig')) {
			$value = $value->toArray(); 
		}	
		
		if(is_string($value)) {
			$result = json_decode($value);
		}
		
		if(is_null($result)) {
		 	$result =  json_encode($value);
		}
		
		return $result;
	}
}

{'fraction_non_alphanumeric': 0.13005780346820808, 'fraction_numerical': 0.006502890173410405, 'mean_word_length': 3.176470588235294, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4536805', 'n_tokens_mistral': 1025, 'n_tokens_neox': 904, 'n_words': 263}