Permalink
Browse files

Cookies, JSON_PRETTY_PRINT, Response protocol version

  • Loading branch information...
1 parent 9a7579f commit 7c20e2b29fbeb165738d25721ccd6b02b511bc29 @e-oz committed May 2, 2012
Showing with 204 additions and 4 deletions.
  1. +92 −0 Cookie.php
  2. +33 −0 ICookie.php
  3. +11 −1 IResponse.php
  4. +60 −2 Response.php
  5. +8 −1 SerializerJSON.php
View
92 Cookie.php
@@ -0,0 +1,92 @@
+<?php
+namespace Jamm\HTTP;
+
+class Cookie implements ICookie
+{
+ private $name, $value;
+ private $expire = 0;
+ private $path, $domain;
+ private $secure = false;
+ private $http_only = false;
+
+ public function __construct($name, $value = '', $expire = 0, $path = '', $domain = '', $secure = false, $http_only = false)
+ {
+ $this->name = $name;
+ $this->value = $value;
+ $this->expire = $expire;
+ $this->path = $path;
+ $this->domain = $domain;
+ $this->secure = $secure;
+ $this->http_only = $http_only;
+ }
+
+ public function setDomain($domain)
+ {
+ $this->domain = $domain;
+ }
+
+ public function getDomain()
+ {
+ return $this->domain;
+ }
+
+ public function setExpire($expire = 0)
+ {
+ $this->expire = $expire;
+ }
+
+ public function getExpire()
+ {
+ return $this->expire;
+ }
+
+ public function setHttpOnly($http_only = false)
+ {
+ $this->http_only = $http_only;
+ }
+
+ public function getHttpOnly()
+ {
+ return $this->http_only;
+ }
+
+ public function setName($name)
+ {
+ $this->name = $name;
+ }
+
+ public function getName()
+ {
+ return $this->name;
+ }
+
+ public function setPath($path)
+ {
+ $this->path = $path;
+ }
+
+ public function getPath()
+ {
+ return $this->path;
+ }
+
+ public function setSecure($secure = false)
+ {
+ $this->secure = $secure;
+ }
+
+ public function getSecure()
+ {
+ return $this->secure;
+ }
+
+ public function setValue($value)
+ {
+ $this->value = $value;
+ }
+
+ public function getValue()
+ {
+ return $this->value;
+ }
+}
View
33 ICookie.php
@@ -0,0 +1,33 @@
+<?php
+namespace Jamm\HTTP;
+
+interface ICookie
+{
+ public function setDomain($domain);
+
+ public function getDomain();
+
+ public function setExpire($expire = 0);
+
+ public function getExpire();
+
+ public function setHttpOnly($http_only = false);
+
+ public function getHttpOnly();
+
+ public function setName($name);
+
+ public function getName();
+
+ public function setPath($path);
+
+ public function getPath();
+
+ public function setSecure($secure = false);
+
+ public function getSecure();
+
+ public function setValue($value);
+
+ public function getValue();
+}
View
12 IResponse.php
@@ -17,6 +17,8 @@ public function setHeader($header, $value);
public function getHeader($header);
+ public function removeHeader($header);
+
/**
* Get body of the response
* @return string
@@ -53,6 +55,14 @@ public function isStatusError();
public function setStatusReason($status_reason);
public function getStatusReason();
-
+
public function __toString();
+
+ public function getProtocolVersion();
+
+ public function setProtocolVersion($protocol_version);
+
+ public function setCookie(ICookie $Cookie);
+
+ public function getCookie($name);
}
View
62 Response.php
@@ -13,13 +13,30 @@ class Response implements IResponse
private $Serializer;
/** @var string */
private $status_reason;
+ private $protocol_version;
+ /** @var ICookie[] */
+ private $cookies;
public function __construct($body = '', $status_code = 200)
{
$this->body = $body;
$this->status_code = $status_code;
}
+ public function setCookie(ICookie $Cookie)
+ {
+ $name = $Cookie->getName();
+ if (empty($name)) return false;
+ $name = $this->getNewOrExistingKeyInArray($name, $this->cookies);
+ $this->cookies[$name] = $Cookie;
+ }
+
+ public function getCookie($name)
+ {
+ $name = $this->getNewOrExistingKeyInArray($name, $this->cookies);
+ return isset($this->cookies[$name]) ? $this->cookies[$name] : NULL;
+ }
+
public function getStatusCode()
{
return $this->status_code;
@@ -88,12 +105,23 @@ public function setHeaders(array $headers)
*/
public function Send()
{
+ header_remove();
$body = $this->getBodyToSend();
$headers = $this->getHeadersToSend($body);
foreach ($headers as $header)
{
header($header);
}
+ if (!empty($this->cookies))
+ {
+ foreach ($this->cookies as $Cookie)
+ {
+ setcookie($Cookie->getName(), $Cookie->getValue(),
+ $Cookie->getExpire(), $Cookie->getPath(),
+ $Cookie->getDomain(), $Cookie->getSecure(),
+ $Cookie->getHttpOnly());
+ }
+ }
print $body;
}
@@ -111,8 +139,9 @@ private function getHeadersToSend(&$body)
{
$this->setHeader('Content-Length', strlen($body));
}
- $protocol = isset($_SERVER['SERVER_PROTOCOL']) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.0';
- $headers[] = $protocol.' '.$this->getStatusCode().' '.$this->getStatusReason();
+
+ $headers[] = 'HTTP/'.$this->getProtocolVersion()
+ .' '.$this->getStatusCode().' '.$this->getStatusReason();
foreach ($this->getHeaders() as $header_key => $header_value)
{
$headers[] = $header_key.': '.$header_value;
@@ -268,4 +297,33 @@ public function setStatusReason($status_reason)
{
$this->status_reason = $status_reason;
}
+
+ public function removeHeader($header)
+ {
+ if (empty($header)) return false;
+ $header = $this->getNewOrExistingKeyInArray($header, $this->headers);
+ unset($this->headers[$header]);
+ return true;
+ }
+
+ public function getProtocolVersion()
+ {
+ if (empty($this->protocol_version))
+ {
+ if (isset($_SERVER['SERVER_PROTOCOL']))
+ {
+ list(, $this->protocol_version) = explode('/', $_SERVER['SERVER_PROTOCOL']);
+ }
+ else
+ {
+ $this->protocol_version = '1.0';
+ }
+ }
+ return $this->protocol_version;
+ }
+
+ public function setProtocolVersion($protocol_version)
+ {
+ $this->protocol_version = $protocol_version;
+ }
}
View
9 SerializerJSON.php
@@ -5,7 +5,14 @@ class SerializerJSON implements ISerializer
{
public function serialize($data)
{
- return json_encode($data);
+ if (strpos(PHP_VERSION, '5.4')!==false)
+ {
+ return json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
+ }
+ else
+ {
+ return json_encode($data);
+ }
}
public function unserialize($data)

0 comments on commit 7c20e2b

Please sign in to comment.