/** * Add a Coookie * * Cookie definition: <em>cookiename[=cookievalue]</em> * * @param string|array $cookie * Cookie to add * * @param string|optional $value * Cookie value, in case a non RFC definition is being used * * @return Next\HTTP\Cookies * Cookies Object (Fluent Interface) * * @throws Next\HTTP\Headers\Fields\FieldsException * Invalid or mal-formed Cookie Value */ public function addCookie($cookie, $value = NULL) { // Well-formed Cookie. Will be added "as is" if ($cookie instanceof Cookie) { $this->cookies->add($cookie); return $this; } // Recursion... if (is_array($cookie)) { foreach ($cookie as $n => $v) { $this->addCookie($n, $v); } } else { /** * @internal * In case Next\HTTP\Cookies::addCookie() was invoked like: * * $cookies -> addCookie( 'cookiename', 'cookievalue' ) * * Instead of: * * $cookies -> addCookie( 'cookiename=cookievalue' ) * * Let's build the full Cookie representation before add it */ if (!is_null($value) && strpos($value, '=') === FALSE) { $cookie = sprintf('%s=%s', $cookie, $value); } try { $this->cookies->add(new Cookie($cookie)); } catch (FieldsException $e) { /** * @internal * We'll rethrow the same Exception caught if a true error occur * so our Exception Handler can do the rest */ if ($e->getCode() !== FieldsException::ALL_INVALID) { throw FieldsException::invalidHeaderValue($e->getMessage(), $e->getCode()); } } } return $this; }
/** * Add a Header * * @param mixed $header * Header name. Possible values are: * * <ul> * * <li>An integer as HTTP Response State Header</li> * * <li> * * A well-formed Header Field. * * It'll be used "as is", an instance of * Next\HTTP\Headers\Fields\Field * </li> * * <li> * * A Header Field Name. * * We'll try to match a valid one or send it as "Generic" * </li> * * <li> * * An associative array of Header Fields to be added * recursively, where each value can be a well-formed * Header Field or a Header Field Name * </li> * * </ul> * * @param string|optional $value * Header Field Value * * @return Next\HTTP\Headers\AbstractHeaders * AbstractHeaders Object (Fluent Interface) * * @throws Next\HTTP\Headers\Fields\FieldsException * Invalid or mal-formed Header Value */ public function addHeader($header, $value = NULL) { if (is_null($header)) { return $this; } // Well-formed Header Field. Don't need to be known if ($header instanceof Field) { if ($this->accept($header)) { $this->headers->add($header); } return $this; } // Recursion... if (is_array($header)) { foreach ($header as $n => $v) { // Usually non-associative arrays have Headers in their values if ($v instanceof Field) { $this->addHeader($v, $n); } else { $this->addHeader($n, $v); } } } else { // Preparing Header Name for Classname Mapping $header = strtolower(str_replace('HTTP_', '', $header)); $header = ucfirst(preg_replace('/(-|_)(\\w)/e', "strtoupper( '\\2' )", $header)); // Checking if its a known Header Field if (array_key_exists($header, $this->known)) { // Building full Classname... $class = sprintf('%s\\%s', self::FIELDS_NS, $this->known[$header]); // ... checking if it exists... if (class_exists($class)) { try { // ...and trying to add it $object = new $class($value); if ($this->accept($object)) { $this->headers->add($object); } } catch (FieldsException $e) { /** * @internal * We'll rethrow the same Exception caught if a true error occur * so our Exception Handler can do the rest */ if ($e->getCode() !== FieldsException::ALL_INVALID) { throw FieldsException::invalidHeaderValue($e->getMessage(), $e->getCode()); } } } } else { /** * @internal * If it is a unknown header, let's add it as Generic Header * * Generic Header don't need to be accepted */ $this->addHeader(new Generic(sprintf('%s: %s', $header, $value))); } } return $this; }