lithium\storage\session\adapter\Cookie::write()

public method

Write a value to the cookie store.

Parameters

  • string $key

    Key of the item to be stored.

  • mixed $value

    The value to be stored.

  • array $options

    Options array.

Returns

\Closure

Function returning boolean true on successful write, false otherwise.

Source

	public function write($key, $value = null, array $options = []) {
		$expire = (!isset($options['expire']) && empty($this->_config['expire']));
		$cookieClass = __CLASS__;

		if ($expire && $key !== $this->_config['name']) {
			return null;
		}
		$expires = (isset($options['expire'])) ? $options['expire'] : $this->_config['expire'];

		return function($params) use (&$expires, $cookieClass) {
			$key = $params['key'];
			$value = $params['value'];
			$key = [$key => $value];
			if (is_array($value)) {
				$key = Set::flatten($key);
			}

			foreach ($key as $name => $val) {
				$result = setcookie(
					$cookieClass::keyFormat($name, $this->_config),
					$val,
					strtotime($expires),
					$this->_config['path'],
					$this->_config['domain'],
					$this->_config['secure'],
					$this->_config['httponly']
				);
				if (!$result) {
					throw new RuntimeException("There was an error setting {$name} cookie.");
				}
			}
			return true;
		};
	}