Skip to content

Commit

Permalink
Merge branch 'backport-horde-upstream-serialization' of https://githu…
Browse files Browse the repository at this point in the history
…b.com/maintaina-com/Mime into maintaina-com-backport-horde-upstream-serialization
  • Loading branch information
mrubinsk committed Nov 6, 2022
2 parents da9d296 + 27d74a0 commit 68dc1cd
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 0 deletions.
34 changes: 34 additions & 0 deletions lib/Horde/Mime/Headers.php
Expand Up @@ -398,6 +398,40 @@ public function serialize()

return serialize($data);
}
/**
* Serialization.
*
* @return array Serialized data.
*/
public function __serialize(): array
{
$data = array(
// Serialized data ID.
self::VERSION,
$this->_headers->getArrayCopy(),
// TODO: BC
$this->_eol
);
return $data;
}

/**
* Unserialization.
*
* @param array $data Serialized data.
*
* @throws Horde_Mime_Exception
*/
public function __unserialize(array $data): void
{
if (!isset($data[0]) || ($data[0] != self::VERSION)) {
throw new Horde_Mime_Exception('Cache version change');
}

$this->_headers = new Horde_Support_CaseInsensitiveArray($data[1]);
// TODO: BC
$this->_eol = $data[2];
}

/**
* Unserialization.
Expand Down
40 changes: 40 additions & 0 deletions lib/Horde/Mime/Headers/ContentParam.php
Expand Up @@ -401,6 +401,9 @@ public function offsetUnset($offset)
/* Serializable methods */

/**
* Serialize (until PHP 7.3)
*
* @return string serialized object state
*/
public function serialize()
{
Expand All @@ -412,6 +415,43 @@ public function serialize()
}

/**
* Serialize (PHP 7.4+)
*
* @return array object state
*/
public function __serialize(): array
{
$vars = array_filter(get_object_vars($this));
if (isset($vars['_params'])) {
$vars['_params'] = $vars['_params']->getArrayCopy();
}
return $vars;
}

/**
* Unserialize (PHP 7.4+)
*
* @param array $data
*/
public function __unserialize(array $data): void
{
foreach ($data as $key => $val) {
switch ($key) {
case '_params':
$this->_params = new Horde_Support_CaseInsensitiveArray($val);
break;

default:
$this->$key = $val;
break;
}
}
}

/**
* Unserialize (until PHP 7.3)
*
* @param string $data
*/
public function unserialize($data)
{
Expand Down
56 changes: 56 additions & 0 deletions lib/Horde/Mime/Part.php
Expand Up @@ -2414,6 +2414,62 @@ public function serialize()
return serialize($data);
}


public function __serialize(): array
{
$data = array(
// Serialized data ID.
self::VERSION,
$this->_bytes,
$this->_eol,
$this->_hdrCharset,
$this->_headers,
$this->_metadata,
$this->_mimeid,
$this->_parts,
$this->_status,
$this->_transferEncoding
);

if (!empty($this->_contents)) {
$data[] = $this->_readStream($this->_contents);
}
return $data;
}
public function __unserialize(array $data): void
{
if (!isset($data[0]) || ($data[0] != self::VERSION)) {
switch ($data[0]) {
case 1:
$convert = new Horde_Mime_Part_Upgrade_V1($data);
$data = $convert->data;
break;

default:
$data = null;
break;
}

if (is_null($data)) {
throw new Exception('Cache version change');
}
}

$key = 0;
$this->_bytes = $data[++$key];
$this->_eol = $data[++$key];
$this->_hdrCharset = $data[++$key];
$this->_headers = $data[++$key];
$this->_metadata = $data[++$key];
$this->_mimeid = $data[++$key];
$this->_parts = $data[++$key];
$this->_status = $data[++$key];
$this->_transferEncoding = $data[++$key];

if (isset($data[++$key])) {
$this->setContents($data[$key]);
}
}
/**
* Unserialization.
*
Expand Down

0 comments on commit 68dc1cd

Please sign in to comment.