Code Coverage |
||||||||||
Classes and Traits |
Functions and Methods |
Lines |
||||||||
Total | |
0.00% |
0 / 1 |
|
22.22% |
2 / 9 |
CRAP | |
32.43% |
12 / 37 |
Cookie | |
0.00% |
0 / 1 |
|
22.22% |
2 / 9 |
106.15 | |
32.43% |
12 / 37 |
fromSetCookieArray(array $setCookies) | |
0.00% |
0 / 1 |
6.74 | |
44.44% |
4 / 9 |
|||
fromString($headerLine) | |
0.00% |
0 / 1 |
20 | |
0.00% |
0 / 15 |
|||
__construct(array $array = array()) | |
100.00% |
1 / 1 |
1 | |
100.00% |
2 / 2 |
|||
setEncodeValue($encodeValue) | |
100.00% |
1 / 1 |
1 | |
100.00% |
2 / 2 |
|||
getEncodeValue() | |
0.00% |
0 / 1 |
2 | |
0.00% |
0 / 1 |
|||
getFieldName() | |
0.00% |
0 / 1 |
2 | |
0.00% |
0 / 1 |
|||
getFieldValue() | |
0.00% |
0 / 1 |
3.07 | |
80.00% |
4 / 5 |
|||
toString() | |
0.00% |
0 / 1 |
2 | |
0.00% |
0 / 1 |
|||
__toString() | |
0.00% |
0 / 1 |
2 | |
0.00% |
0 / 1 |
<?php | |
/** | |
* Zend Framework (http://framework.zend.com/) | |
* | |
* @link http://github.com/zendframework/zf2 for the canonical source repository | |
* @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com) | |
* @license http://framework.zend.com/license/new-bsd New BSD License | |
*/ | |
namespace Zend\Http\Header; | |
use ArrayObject; | |
/** | |
* @see http://www.ietf.org/rfc/rfc2109.txt | |
* @see http://www.w3.org/Protocols/rfc2109/rfc2109 | |
*/ | |
class Cookie extends ArrayObject implements HeaderInterface | |
{ | |
protected $encodeValue = true; | |
public static function fromSetCookieArray(array $setCookies) | |
{ | |
$nvPairs = array(); | |
/* @var $setCookie SetCookie */ | |
foreach ($setCookies as $setCookie) { | |
if (!$setCookie instanceof SetCookie) { | |
throw new Exception\InvalidArgumentException(__CLASS__ . '::' . __METHOD__ . ' requires an array of SetCookie objects'); | |
} | |
if (array_key_exists($setCookie->getName(), $nvPairs)) { | |
throw new Exception\InvalidArgumentException('Two cookies with the same name were provided to ' . __CLASS__ . '::' . __METHOD__); | |
} | |
$nvPairs[$setCookie->getName()] = $setCookie->getValue(); | |
} | |
return new static($nvPairs); | |
} | |
public static function fromString($headerLine) | |
{ | |
$header = new static(); | |
list($name, $value) = GenericHeader::splitHeaderLine($headerLine); | |
// check to ensure proper header type for this factory | |
if (strtolower($name) !== 'cookie') { | |
throw new Exception\InvalidArgumentException('Invalid header line for Server string: "' . $name . '"'); | |
} | |
$nvPairs = preg_split('#;\s*#', $value); | |
$arrayInfo = array(); | |
foreach ($nvPairs as $nvPair) { | |
$parts = explode('=', $nvPair, 2); | |
if (count($parts) != 2) { | |
throw new Exception\RuntimeException('Malformed Cookie header found'); | |
} | |
list($name, $value) = $parts; | |
$arrayInfo[$name] = urldecode($value); | |
} | |
$header->exchangeArray($arrayInfo); | |
return $header; | |
} | |
public function __construct(array $array = array()) | |
{ | |
parent::__construct($array, ArrayObject::ARRAY_AS_PROPS); | |
} | |
public function setEncodeValue($encodeValue) | |
{ | |
$this->encodeValue = (bool) $encodeValue; | |
return $this; | |
} | |
public function getEncodeValue() | |
{ | |
return $this->encodeValue; | |
} | |
public function getFieldName() | |
{ | |
return 'Cookie'; | |
} | |
public function getFieldValue() | |
{ | |
$nvPairs = array(); | |
foreach ($this as $name => $value) { | |
$nvPairs[] = $name . '=' . (($this->encodeValue) ? urlencode($value) : $value); | |
} | |
return implode('; ', $nvPairs); | |
} | |
public function toString() | |
{ | |
return 'Cookie: ' . $this->getFieldValue(); | |
} | |
/** | |
* Get the cookie as a string, suitable for sending as a "Cookie" header in an | |
* HTTP request | |
* | |
* @return string | |
*/ | |
public function __toString() | |
{ | |
return $this->toString(); | |
} | |
} |