lithium\net\http\Response::cookies()

public method

Add a cookie to header output, or return a single cookie or full cookie list.

This function's parameters are designed to be analogous to setcookie(). Function parameters expire, path, domain, secure, and httponly may be passed in as an associative array alongside value inside $value.

NOTE: Cookies values are expected to be scalar. This function will not serialize cookie values. If you wish to store a non-scalar value, you must serialize the data first.

NOTE: Cookie values are stored as an associative array containing at minimum a value key. Cookies which have been set multiple times do not overwrite each other. Rather they are stored as an array of associative arrays.

Parameters

  • string $key
  • string $value

Returns

mixed

Source

	public function cookies($key = null, $value = null) {
		if (!$key) {
			$key = $this->cookies;
			$this->cookies = [];
		}
		if (is_array($key)) {
			foreach ($key as $cookie => $value) {
				$this->cookies($cookie, $value);
			}
		} elseif (is_string($key)) {
			if ($value === null) {
				return isset($this->cookies[$key]) ? $this->cookies[$key] : null;
			}
			if ($value === false) {
				unset($this->cookies[$key]);
			} else {
				if (is_array($value)) {
					if (array_values($value) === $value) {
						foreach ($value as $i => $set) {
							if (!is_array($set)) {
								$value[$i] = ['value' => $set];
							}
						}
					}
				} else {
					$value = ['value' => $value];
				}
				if (isset($this->cookies[$key])) {
					$orig = $this->cookies[$key];
					if (array_values($orig) !== $orig) {
						$orig = [$orig];
					}
					if (array_values($value) !== $value) {
						$value = [$value];
					}
					$this->cookies[$key] = array_merge($orig, $value);
				} else {
					$this->cookies[$key] = $value;
				}
			}
		}
		return $this->cookies;
	}