<?php
class Library_Response extends stdClass implements ArrayAccess {
	/**
	 * indikace uspechu nebo neuspechu
	 * @var bool
	 */
	public $success = false;
	
	/**
	 * stavova hlaska
	 * @var string
	 */
	public $status = "UNKNOWN";
	
	/**
	 * Kodove oznaceni stavu
	 * @var int
	 */
	public $statusCode = 0;
	
	/**
	 * data vlozena v odpovedi
	 * @var array
	 */
	public $data = array();
	
	/**
	 * uchovava informaci o pozici v poli
	 * @var mixed
	 */
	private $_position;
	
	public function offsetExists($offset) {
		return isset($this->data[$offset]);
	}
	
	public function offsetGet($offset) {
		return $this->data[$offset];
	}
	
	public function offsetSet($offset, $value) {
		return $this->data[$offset] = $value;
	}
	
	public function offsetUnset($offset) {
		unset($this->data[$offset]);
	}
	
	/**
	 * vraci pole dat
	 * @return array
	 */
	public function getData() { 
		return $this->data;
	}
	
	/**
	 * nastavi status jako uspesny
	 * @param string $status
	 * @param int $statusCode
	 * @return Library_Response
	 */
	public function setOk($status = "OK", $statusCode = 200) {
		$this->status = $status;
		$this->statusCode = $statusCode;
		$this->success = true;
		
		return $this;
	}
	
	/**
	 * nastavi novy chybovy status
	 * @param string $status
	 * @param int $statusCode
	 * @return Library_Response
	 */
	public function setStatus($status, $statusCode = 0) {
		$this->status = $status;
		$this->statusCode = $statusCode;
		
		return $this;
	}
	
	public function sendHeader() {
		$message = "OK";
		
		switch ($this->statusCode) {
			case 200:
				$message = "OK";
				break;
			
			case 400:
				$message = "Bad Request";
				break;
				
			case 403:
				$message = "Forbidden";
				break;
			
			case 404:
				$message = "Not Found";
				break;
			
			case 405:
				$message = "Method Not Allowed";
				break;
			
			case 409:
				$message = "Conflict";
				break;
		}
		
		//odeslani hlavicky
		header("HTTP/1.1 $this->statusCode $message");
	}
}