Skip to content

Commit

Permalink
Multiple fixes/improvements to propertyBag
Browse files Browse the repository at this point in the history
  • Loading branch information
mattwire committed Jul 19, 2021
1 parent 5be410a commit 192ba90
Showing 1 changed file with 48 additions and 3 deletions.
51 changes: 48 additions & 3 deletions Civi/Payment/PropertyBag.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,23 @@ class PropertyBag implements \ArrayAccess {

protected static $propMap = [
'amount' => TRUE,
'total_amount' => 'amount',
'billingStreetAddress' => TRUE,
'billing_street_address' => 'billingStreetAddress',
'street_address' => 'billingStreetAddress',
'billingSupplementalAddress1' => TRUE,
'billingSupplementalAddress2' => TRUE,
'billingSupplementalAddress3' => TRUE,
'billingCity' => TRUE,
'billing_city' => 'billingCity',
'city' => 'billingCity',
'billingPostalCode' => TRUE,
'billing_postal_code' => 'billingPostalCode',
'postal_code' => 'billingPostalCode',
'billingCounty' => TRUE,
'billingStateProvince' => TRUE,
'billing_state_province' => 'billingStateProvince',
'state_province' => 'billingStateProvince',
'billingCountry' => TRUE,
'contactID' => TRUE,
'contact_id' => 'contactID',
Expand Down Expand Up @@ -174,13 +184,16 @@ public function offsetGet($offset) {
}
}

// These lines are here (and not in try block) because the catch must only
// catch the case when the prop is custom.
$getter = 'get' . ucfirst($prop);
if (!$this->getSuppressLegacyWarnings()) {
CRM_Core_Error::deprecatedFunctionWarning(
"get" . ucfirst($offset) . "()",
"PropertyBag array access for core property '$offset'"
);
}
return $this->get($prop, 'default');
return $this->$getter('default');
}

/**
Expand Down Expand Up @@ -258,13 +271,18 @@ protected function handleLegacyPropNames($prop, $silent = FALSE) {
if ($newName === NULL && substr($prop, -2) === '-' . \CRM_Core_BAO_LocationType::getBilling()
&& isset(static::$propMap[substr($prop, 0, -2)])
) {
$newName = substr($prop, 0, -2);
$billingAddressProp = substr($prop, 0, -2);
$newName = static::$propMap[$billingAddressProp] ?? NULL;
if ($newName === TRUE) {
// Good, modern name.
return $billingAddressProp;
}
}

if ($newName === NULL) {
if ($silent) {
// Only for use by offsetExists
return;
return NULL;
}
throw new \InvalidArgumentException("Unknown property '$prop'.");
}
Expand Down Expand Up @@ -591,6 +609,27 @@ public function setBillingCounty($input, $label = 'default') {
return $this->set('billingCounty', $label, (string) $input);
}

/**
* BillingStateProvince getter.
*
* @return string
*/
public function getBillingStateProvince($label = 'default') {
return $this->get('billingStateProvince', $label);
}

/**
* BillingStateProvince setter.
*
* Nb. we can't validate this unless we have the country ID too, so we don't.
*
* @param string $input
* @param string $label e.g. 'default'
*/
public function setBillingStateProvince($input, $label = 'default') {
return $this->set('billingStateProvince', $label, (string) $input);
}

/**
* BillingCountry getter.
*
Expand Down Expand Up @@ -717,6 +756,9 @@ public function setCurrency($value, $label = 'default') {
* @return string
*/
public function getDescription($label = 'default') {
if (!$this->has('description')) {
return '';
}
return $this->get('description', $label);
}

Expand Down Expand Up @@ -804,6 +846,9 @@ public function setFirstName($firstName, $label = 'default') {
* @return string|null
*/
public function getInvoiceID($label = 'default') {
if (!$this->has('invoiceID')) {
$this->set('invoiceID', $label, md5(uniqid(mt_rand(), TRUE)));
}
return $this->get('invoiceID', $label);
}

Expand Down

0 comments on commit 192ba90

Please sign in to comment.