Skip to content

Commit

Permalink
[REF] Create polyfill function for array_key_first which only exists …
Browse files Browse the repository at this point in the history
…in php7.3 and onwards
  • Loading branch information
seamuslee001 committed Apr 3, 2022
1 parent c4b081e commit cde3917
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
4 changes: 2 additions & 2 deletions CRM/Contribute/Form/ContributionBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -1138,10 +1138,10 @@ public function cancelRecurring() {
* Arguably the form should start to build $this->_params in the pre-process main page & use that array consistently throughout.
*/
protected function setRecurringMembershipParams() {
$priceFieldId = array_key_first($this->_values['fee']);
$priceFieldId = CRM_Utils_Array::firstArrayKey($this->_values['fee']);
// Why is this an array in CRM_Contribute_Form_Contribution_Main::submit and a string in CRM_Contribute_Form_Contribution_Confirm::preProcess()?
if (is_array($this->_params["price_{$priceFieldId}"])) {
$priceFieldValue = array_key_first($this->_params["price_{$priceFieldId}"]);
$priceFieldValue = CRM_Utils_Array::firstArrayKey($this->_params["price_{$priceFieldId}"]);
}
else {
$priceFieldValue = $this->_params["price_{$priceFieldId}"];
Expand Down
18 changes: 18 additions & 0 deletions CRM/Utils/Array.php
Original file line number Diff line number Diff line change
Expand Up @@ -1351,4 +1351,22 @@ public static function prefixKeys(array $collection, string $prefix) {
return $result;
}

/**
* Return the first Array Key either using array_key_first or a custom function
* @param array $array the array to look through
*
* @return string|int|null
*/
public static function firstArrayKey(array $array) {
if (!function_exists('array_key_first')) {
foreach ($arr as $key => $unused) {
return $key;
}
return NULL;
}
else {
return array_key_first($array);
}
}

}

0 comments on commit cde3917

Please sign in to comment.