lithium\storage\session\adapter\Cookie::write PHP Method

write() public method

Write a value to the cookie store.
public write ( string $key, mixed $value = null, array $options = [] ) : Closure
$key string Key of the item to be stored.
$value mixed The value to be stored.
$options array Options array.
return Closure Function returning boolean `true` on successful write, `false` otherwise.
    public function write($key, $value = null, array $options = array())
    {
        $expire = !isset($options['expire']) && empty($this->_config['expire']);
        $config = $this->_config;
        $cookieClass = __CLASS__;
        if ($expire && $key !== $config['name']) {
            return null;
        }
        $expires = isset($options['expire']) ? $options['expire'] : $config['expire'];
        return function ($self, $params) use(&$config, &$expires, $cookieClass) {
            $key = $params['key'];
            $value = $params['value'];
            $key = array($key => $value);
            if (is_array($value)) {
                $key = Set::flatten($key);
            }
            foreach ($key as $name => $val) {
                $name = $cookieClass::keyFormat($name, $config);
                $result = setcookie($name, $val, strtotime($expires), $config['path'], $config['domain'], $config['secure'], $config['httponly']);
                if (!$result) {
                    throw new RuntimeException("There was an error setting {$name} cookie.");
                }
            }
            return true;
        };
    }

Usage Example

 public function testBadWrite()
 {
     $cookie = new Cookie(array('expire' => null));
     $this->assertNull($cookie->write('bad', 'val'));
 }