<?php
namespace Engine\Mvc\Response;

/**
 * Class that handles the Response sent to the client
 *
 * These classes are expected to act as data-containers, and the actual response sending should be handled in disp.php (front controller)
 *
 * @see http://book.cakephp.org/2.0/en/controllers/request-response.html#CakeRequest::clientIp
 * @see http://stackoverflow.com/questions/13359818/what-is-the-right-way-to-handle-post-data-in-mvc
 * @see http://symfony.com/doc/current/book/http_fundamentals.html
 * @see http://framework.zend.com/manual/1.12/en/zend.controller.request.html
 *
 * @author Lucian Sirbu
 */
abstract class GeneralAbstract
{
	/**
	 * The headers object
	 * @var \Engine\Mvc\Response\Components\Headers
	 */
	public $headers = null;

	/**
	 * The body object
	 * @var \Engine\Mvc\Response\Components\Body
	 */
	public $body = null;

	/**
	 * Can be 'gzip', null.
	 * @var String
	 */
	public $compressionMethod = 'gzip';

	/**
	 * Build the response object
	 * @param string $body
	 * @param array $headersArr
	 */
	public function __construct($body=null, array $headersArr = [])
	{
		$this->initialize($body, $headersArr);
	}

	/**
	 * Initialize the object
	 * @param string $body
	 * @param array $headersArr
	 */
	abstract protected function initialize($body, array $headersArr);

	/**
	 * Append data to the body
	 * @param string $body
	 */
	public function appendToBody($body)
	{
		$this->body->append($body);
	}

	/**
	 * Set the body object
	 * @param string $body
	 */
	public function setBody($body)
	{
		$this->body->set($body);
	}

	/**
	 * Get the body object
	 * @return mixed
	 */
	public function getBody()
	{
		return $this->body->get();
	}

	/**
	 * Prepare the final headers
	 * @return \Engine\Mvc\Response\Components\Headers
	 */
	public function getHeadersFinal()
	{
		return $this->headers;
	}


	/**
	 * Get the response body as string
	 */
	public function asString()
	{
		return $this->body->asString();
	}
}
