public set ( string $name, array | string | DateTime $values, boolean $replaceExistingHeader = true ) : void | ||
$name | string | Name of the header, for example "Location", "Content-Description" etc. |
$values | array | string | DateTime | An array of values or a single value for the specified header field |
$replaceExistingHeader | boolean | If a header with the same name should be replaced. Default is TRUE. |
return | void |
public function set($name, $values, $replaceExistingHeader = true)
{
if (strtoupper(substr($name, 0, 10)) === 'SET-COOKIE') {
throw new \InvalidArgumentException('The "Set-Cookie" headers must be set via setCookie().', 1345128153);
}
if ($values instanceof \DateTimeInterface) {
$date = clone $values;
$date->setTimezone(new \DateTimeZone('GMT'));
$values = [$date->format('D, d M Y H:i:s') . ' GMT'];
} else {
$values = (array) $values;
}
switch ($name) {
case 'Cache-Control':
if (count($values) !== 1) {
throw new \InvalidArgumentException('The "Cache-Control" header must be unique and thus only one field value may be specified.', 1337849415);
}
$this->setCacheControlDirectivesFromRawHeader(array_pop($values));
break;
case 'Cookie':
if (count($values) !== 1) {
throw new \InvalidArgumentException('The "Cookie" header must be unique and thus only one field value may be specified.', 1345127727);
}
$this->setCookiesFromRawHeader(array_pop($values));
break;
default:
if ($replaceExistingHeader === true || !isset($this->fields[$name])) {
$this->fields[$name] = $values;
} else {
$this->fields[$name] = array_merge($this->fields[$name], $values);
}
}
}
/** * Sets the specified HTTP header * * DateTime objects will be converted to a string representation internally but * will be returned as DateTime objects on calling getHeader(). * * Please note that dates are normalized to GMT internally, so that getHeader() will return * the same point in time, but not necessarily in the same timezone, if it was not * GMT previously. GMT is used synonymously with UTC as per RFC 2616 3.3.1. * * @param string $name Name of the header, for example "Location", "Content-Description" etc. * @param array|string|\DateTime $values An array of values or a single value for the specified header field * @param boolean $replaceExistingHeader If a header with the same name should be replaced. Default is TRUE. * @return self This message, for method chaining * @throws \InvalidArgumentException * @api */ public function setHeader($name, $values, $replaceExistingHeader = true) { switch ($name) { case 'Content-Type': if (is_array($values)) { if (count($values) !== 1) { throw new \InvalidArgumentException('The "Content-Type" header must be unique and thus only one field value may be specified.', 1454949291); } $values = (string) $values[0]; } if (stripos($values, 'charset') === false && stripos($values, 'text/') === 0) { $values .= '; charset=' . $this->charset; } break; } $this->headers->set($name, $values, $replaceExistingHeader); return $this; }