lithium\storage\session\strategy\Encrypt::read PHP Method

read() public method

Read encryption method.
public read ( array $data, array $options = [] ) : mixed
$data array the Data being read.
$options array Options for this method.
return mixed Returns the decrypted key or the dataset.
    public function read($data, array $options = array())
    {
        $class = $options['class'];
        $encrypted = $class::read(null, array('strategies' => false));
        $key = isset($options['key']) ? $options['key'] : null;
        if (!isset($encrypted['__encrypted']) || !$encrypted['__encrypted']) {
            return isset($encrypted[$key]) ? $encrypted[$key] : null;
        }
        $current = $this->_decrypt($encrypted['__encrypted']);
        if ($key) {
            return isset($current[$key]) ? $current[$key] : null;
        } else {
            return $current;
        }
    }

Usage Example

 public function testDelete()
 {
     $encrypt = new Encrypt(array('secret' => $this->secret));
     $key = 'fookey';
     $value = 'barvalue';
     $result = $encrypt->write($value, array('class' => $this->mock, 'key' => $key));
     $this->assertTrue($result);
     $cookie = MockCookieSession::data();
     $result = $encrypt->read($key, array('class' => $this->mock, 'key' => $key));
     $this->assertEqual($value, $result);
     $result = $encrypt->delete($key, array('class' => $this->mock, 'key' => $key));
     $cookie = MockCookieSession::data();
     $this->assertTrue(empty($cookie['__encrypted']));
     $result = $encrypt->read($key, array('class' => $this->mock));
     $this->assertFalse($result);
 }