Skip to content

Implements __serialize and __unserialize #255

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 24 additions & 4 deletions CRM/Core/Payment/OmnipayMultiProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,18 @@ class CRM_Core_Payment_OmnipayMultiProcessor extends CRM_Core_Payment_PaymentExt
* @var \Civi\Payment\PropertyBag
*/
protected $propertyBag;


/**
* For PHP8.1
* https://www.php.net/manual/en/language.oop5.magic.php#object.serialize
* @return array
*/
public function __serialize(): array {
$data = (object) get_object_vars($this);
$this->cleanupObjectForSerialization($data, TRUE);
return (array) $data;
}

/**
* Serialize, first removing gateway
*
Expand All @@ -91,8 +102,7 @@ class CRM_Core_Payment_OmnipayMultiProcessor extends CRM_Core_Payment_PaymentExt
* @return string
*/
public function serialize(): string {
$this->cleanupClassForSerialization(TRUE);
return serialize(get_object_vars($this));
return serialize($this->serialize());
}

/**
Expand All @@ -104,7 +114,17 @@ public function serialize(): string {
*/
public function unserialize($data) {
$values = unserialize($data);
foreach ($values as $key => $value) {
$this->__unserialize($values);
}

/**
* For PHP8.1
* https://www.php.net/manual/en/language.oop5.magic.php#object.unserialize
*
* @param array $data
*/
public function __unserialize(array $data): void {
foreach ($data as $key => $value) {
$this->$key = $value;
}
}
Expand Down
31 changes: 21 additions & 10 deletions CRM/Core/Payment/PaymentExtended.php
Original file line number Diff line number Diff line change
Expand Up @@ -381,24 +381,35 @@ public function serialize() {
return serialize($this);
}

/**
* Unset various objects that will fail to serialize when the form is stored to session.
*
* @param object $object (by reference)
* @param bool $isIncludeGateWay Should we also unset the gateway.
*/
protected function cleanupObjectForSerialization(&$object, $isIncludeGateWay = FALSE) {
if (\Civi::settings()->get('omnipay_developer_mode') && !empty($object->history)) {
$object->logHttpTraffic(FALSE);
}
$object->history = [];
$object->client = NULL;
$object->lock = NULL;
$object->guzzleClient = NULL;
if ($isIncludeGateWay) {
$object->gateway = NULL;
}
}

/**
* Unset various objects that will fail to serialize when the form is stored to session.
*
* @param bool $isIncludeGateWay
* Should we also unset the gateway.
* (possibly the default here should be TRUE but we want to be sure we are not
* unsetting it when it is still being used.)
* For retro-compatibilty, this still transforms the current entity.
*/
protected function cleanupClassForSerialization($isIncludeGateWay = FALSE) {
if (\Civi::settings()->get('omnipay_developer_mode') && !empty($this->history)) {
$this->logHttpTraffic(FALSE);
}
$this->history = [];
$this->client = NULL;
$this->lock = NULL;
$this->guzzleClient = NULL;
if ($isIncludeGateWay) {
$this->gateway = NULL;
}
$this->cleanupObjectForSerialization($this, $isIncludeGateWay);
}
}