<commit_before><?php defined('SYSPATH') or die('No direct script access.');
/**
 * Dispatch Response
 * 
 * @package		Dispatch
 * @category	Base
 * @author		<NAME> <<EMAIL>>
 * @copyright	(c) 2011 <NAME>
 * @license		MIT
 */
class Kohana_Dispatch_Response implements Iterator, ArrayAccess, Countable
{	
	/**
	 * Response
	 * 
	 * @access	protected
	 * @var		Response|FALSE
	 */
	protected $_response = FALSE;
	
	/**
	 * Data
	 * 
	 * @access	protected
	 * @var		NULL|array
	 */
	protected $_data = array();
	
	/**
	 * Factory pattern
	 * 
	 * @access	public
	 * @return	Dispatch_Response
	 */
	public static function factory($response)
	{
		return new Dispatch_Response($response);	
	}
	
	/**
	 * Initialization
	 * 
	 * @access	public
	 * @param	Response|FALSE
	 * @return	void
	 */
	public function __construct($response)
	{
		$this->_response = $response;
		
		$this->_data = ($this->_response instanceof Response) ? $this->_filter_response($this->_response) : array();
	}
	
	/**
	 * Whether or not response is loaded
	 * 
	 * @access	public
	 * @return	bool
	 */
	public function loaded()
	{
		if ($status = $this->status())
		{
			if ($status >= 200 && $status <= 300)
				return TRUE;
		}
		
		return FALSE;
	}

	/**
	 * Get Response status
	 * 
	 * @access	public
	 * @return	int|bool
	 */
	public function status()
	{
		if ($this->_response instanceof Response)
			return $this->_response->status();
		
		return FALSE;
	}
	
	/**
	 * Return Response as array
	 * 
	 * @access	public
	 * @return	array
	 */
	public function as_array()
	{
		return $this->_data;
	}
	
	/**
	 * Get response
	 * 
	 * @access	public
	 * @return	Response|FALSE
	 */
	public function get_response()
	{
		return $this->_response;
	}

	/**
	 * Filter Response
	 * 
	 * @todo	Detect empty body and return empty array
	 * @todo	Detect Dataflow module and map driver based on Content-Type.
	 * @access	protected
	 * @param	Request
	 * @return	array|Response
	 */
	protected function _filter_response(Response $response)
	{
		// Check if access is available for pass-through
		if (method_exists('get_body', $response) && $body = $response->get_body())
		{
			if (is_array($body))
				return $body;
			
			if ($body instanceof Dataflow)
				return $body->get()->as_array();
		}
		
		if ($body = $response->body())
		{
			switch ($response->headers('Content-Type'))
			{
				case 'application/json':<commit_msg>Extending native response to handle pass-through of objects and arrays for internal sub requests. Minor changes to user guide.<commit_after><?php defined('SYSPATH') or die('No direct script access.');
/**
 * Dispatch Response
 * 
 * @package		Dispatch
 * @category	Base
 * @author		<NAME> <<EMAIL>>
 * @copyright	(c) 2011 <NAME>
 * @license		MIT
 */
class Kohana_Dispatch_Response implements Iterator, ArrayAccess, Countable
{	
	/**
	 * Response
	 * 
	 * @access	protected
	 * @var		Response|FALSE
	 */
	protected $_response = FALSE;
	
	/**
	 * Data
	 * 
	 * @access	protected
	 * @var		NULL|array
	 */
	protected $_data = array();
	
	/**
	 * Factory pattern
	 * 
	 * @access	public
	 * @return	Dispatch_Response
	 */
	public static function factory($response)
	{
		return new Dispatch_Response($response);	
	}
	
	/**
	 * Initialization
	 * 
	 * @access	public
	 * @param	Response|FALSE
	 * @return	void
	 */
	public function __construct($response)
	{
		$this->_response = $response;
		
		$this->_data = ($this->_response instanceof Response) ? $this->_filter_response($this->_response) : array();
	}
	
	/**
	 * Whether or not response is loaded
	 * 
	 * @access	public
	 * @return	bool
	 */
	public function loaded()
	{
		if ($status = $this->status())
		{
			if ($status >= 200 && $status <= 300)
				return TRUE;
		}
		
		return FALSE;
	}

	/**
	 * Get Response status
	 * 
	 * @access	public
	 * @return	int|bool
	 */
	public function status()
	{
		if ($this->_response instanceof Response)
			return $this->_response->status();
		
		return FALSE;
	}
	
	/**
	 * Return Response as array
	 * 
	 * @access	public
	 * @return	array
	 */
	public function as_array()
	{
		return $this->_data;
	}
	
	/**
	 * Get response
	 * 
	 * @access	public
	 * @return	Response|FALSE
	 */
	public function get_response()
	{
		return $this->_response;
	}

	/**
	 * Filter Response
	 * 
	 * @todo	Detect empty body and return empty array
	 * @todo	Detect Dataflow module and map driver based on Content-Type.
	 * @access	protected
	 * @param	Request
	 * @return	array|Response
	 */
	protected function _filter_response(Response $response)
	{
		// Check if access is available for pass-through
		if (method_exists($response, 'get_body') && $body = $response->get_body())
		{
			if (is_array($body))
				return $body;
			
			if ($body instanceof Dataflow)
				return $body->get()->as_array();
		}
		
		if ($body = $response->body())
		{
			switch ($response->headers('Content-Type'))
			{
				case 'application/json':
{'fraction_non_alphanumeric': 0.1406345957011259, 'fraction_numerical': 0.0040941658137154556, 'mean_word_length': 3.018092105263158, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 45, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '804054', 'n_tokens_mistral': 1779, 'n_tokens_neox': 1498, 'n_words': 419}