| 1 |
<?php |
| 2 |
|
| 3 |
namespace Give\Session\SessionDonation\SessionObjects; |
| 4 |
|
| 5 |
use DateTime; |
| 6 |
use Give\Framework\Exceptions\Primitives\InvalidArgumentException; |
| 7 |
use Give\Helpers\ArrayDataSet; |
| 8 |
use Give\Session\Objects; |
| 9 |
use Give\ValueObjects\CardInfo; |
| 10 |
use Give\ValueObjects\DonorInfo; |
| 11 |
use Give\ValueObjects\ValueObjects; |
| 12 |
|
| 13 |
/** |
| 14 |
* Class Donation |
| 15 |
* |
| 16 |
* This class is use to represent donation session data as object. |
| 17 |
* You can add custom data but that data only store momentarily because of donation session time limit. |
| 18 |
* This does not represent actual Donation model instead of that it has few donation related information which required for donation processing. |
| 19 |
* |
| 20 |
* @package Give\Session\SessionDonation\SessionObjects |
| 21 |
* |
| 22 |
* @property CardInfo $cardInfo |
| 23 |
* @property FormEntry $formEntry |
| 24 |
* @property DonorInfo $donorInfo |
| 25 |
*/ |
| 26 |
#[\AllowDynamicProperties] |
| 27 |
class Donation implements Objects |
| 28 |
{ |
| 29 |
/** |
| 30 |
* Donation id. |
| 31 |
* |
| 32 |
* @since 2.7.0 |
| 33 |
* @var array |
| 34 |
*/ |
| 35 |
public $id; |
| 36 |
|
| 37 |
/** |
| 38 |
* Sanitized donation total amount. |
| 39 |
* |
| 40 |
* @since 2.7.0 |
| 41 |
* @var string |
| 42 |
*/ |
| 43 |
public $totalAmount; |
| 44 |
|
| 45 |
/** |
| 46 |
* Donation purchase key. |
| 47 |
* |
| 48 |
* @since 2.7.0 |
| 49 |
* @var string |
| 50 |
*/ |
| 51 |
public $purchaseKey; |
| 52 |
|
| 53 |
/** |
| 54 |
* Donor email. |
| 55 |
* |
| 56 |
* @since 2.7.0 |
| 57 |
* @var string |
| 58 |
*/ |
| 59 |
public $donorEmail; |
| 60 |
|
| 61 |
/** |
| 62 |
* Donor email. |
| 63 |
* |
| 64 |
* @since 2.7.0 |
| 65 |
* @var DateTime |
| 66 |
*/ |
| 67 |
public $createdAt; |
| 68 |
|
| 69 |
/** |
| 70 |
* Payment gateway id. |
| 71 |
* |
| 72 |
* @since 2.7.0 |
| 73 |
* @var array |
| 74 |
*/ |
| 75 |
public $paymentGateway; |
| 76 |
|
| 77 |
/** |
| 78 |
* Donation-related objects. |
| 79 |
* |
| 80 |
* @since 3.18.0 |
| 81 |
* @var FormEntry |
| 82 |
*/ |
| 83 |
public $formEntry; |
| 84 |
|
| 85 |
/** |
| 86 |
* Donor information. |
| 87 |
* |
| 88 |
* @since 3.18.0 |
| 89 |
* @var DonorInfo |
| 90 |
*/ |
| 91 |
public $donorInfo; |
| 92 |
|
| 93 |
/** |
| 94 |
* Card information. |
| 95 |
* |
| 96 |
* @since 3.18.0 |
| 97 |
* @var CardInfo |
| 98 |
*/ |
| 99 |
public $cardInfo; |
| 100 |
|
| 101 |
/** |
| 102 |
* Array of properties and their cast type. |
| 103 |
* |
| 104 |
* @var ValueObjects[] |
| 105 |
*/ |
| 106 |
private $caseTo = [ |
| 107 |
'formEntry' => FormEntry::class, |
| 108 |
'donorInfo' => DonorInfo::class, |
| 109 |
'cardInfo' => CardInfo::class, |
| 110 |
]; |
| 111 |
|
| 112 |
/** |
| 113 |
* Take array and return object. |
| 114 |
* |
| 115 |
* @param $array |
| 116 |
* |
| 117 |
* @return Donation |
| 118 |
*/ |
| 119 |
public static function fromArray($array) |
| 120 |
{ |
| 121 |
$expectedKeys = [ |
| 122 |
'id', |
| 123 |
'totalAmount', |
| 124 |
'purchaseKey', |
| 125 |
'donorEmail', |
| 126 |
'createdAt', |
| 127 |
'paymentGateway', |
| 128 |
'formEntry', |
| 129 |
'cardInfo', |
| 130 |
'donorInfo', |
| 131 |
]; |
| 132 |
|
| 133 |
if ( ! ArrayDataSet::hasRequiredKeys($array, $expectedKeys)) { |
| 134 |
throw new InvalidArgumentException( |
| 135 |
'Invalid Donation object, must have the exact following keys: ' . implode(', ', $expectedKeys) |
| 136 |
); |
| 137 |
} |
| 138 |
|
| 139 |
$donation = new self(); |
| 140 |
|
| 141 |
$array['donorInfo'] = $donation->renameKeyInDonorInfo($array['donorInfo']); |
| 142 |
$array['cardInfo'] = $donation->filterCardInfoKeys($array['cardInfo']); |
| 143 |
|
| 144 |
foreach ($array as $key => $value) { |
| 145 |
if (array_key_exists($key, $donation->caseTo)) { |
| 146 |
$class = $donation->caseTo[$key]; |
| 147 |
$donation->{$key} = $class::fromArray($value); |
| 148 |
continue; |
| 149 |
} |
| 150 |
|
| 151 |
$donation->{$key} = is_array($value) ? |
| 152 |
json_decode(json_encode($value)) // Convert unlisted array type session data to stdClass object |
| 153 |
: $value; |
| 154 |
} |
| 155 |
|
| 156 |
return $donation; |
| 157 |
} |
| 158 |
|
| 159 |
/** |
| 160 |
* Rename array key in donor info |
| 161 |
* |
| 162 |
* @since 2.7.0 |
| 163 |
* |
| 164 |
* @param array $array |
| 165 |
* |
| 166 |
* @return array |
| 167 |
*/ |
| 168 |
private function renameKeyInDonorInfo($array) |
| 169 |
{ |
| 170 |
return ArrayDataSet::renameKeys( |
| 171 |
$array, |
| 172 |
[ |
| 173 |
'id' => 'wpUserId', |
| 174 |
'title' => 'honorific', |
| 175 |
] |
| 176 |
); |
| 177 |
} |
| 178 |
|
| 179 |
/** |
| 180 |
* Filter array keys in card info |
| 181 |
* |
| 182 |
* @since 2.7.0 |
| 183 |
* |
| 184 |
* @param $array |
| 185 |
* |
| 186 |
* @return array |
| 187 |
*/ |
| 188 |
private function filterCardInfoKeys($array) |
| 189 |
{ |
| 190 |
$array = $this->removePrefixFromArrayKeys($array, ['card']); |
| 191 |
$array = ArrayDataSet::renameKeys( |
| 192 |
$array, |
| 193 |
[ |
| 194 |
'address' => 'line1', |
| 195 |
'address2' => 'line2', |
| 196 |
] |
| 197 |
); |
| 198 |
$array = ArrayDataSet::moveArrayItemsUnderArrayKey( |
| 199 |
$array, |
| 200 |
['line1', 'line2', 'city', 'state', 'country', 'zip'], |
| 201 |
'address' |
| 202 |
); |
| 203 |
|
| 204 |
// Rename zip to postal code. |
| 205 |
$array['address']['postalCode'] = $array['address']['zip']; |
| 206 |
unset($array['address']['zip']); |
| 207 |
|
| 208 |
return $array; |
| 209 |
} |
| 210 |
|
| 211 |
/** |
| 212 |
* Remove prefix from array key. |
| 213 |
* |
| 214 |
* @param array $array |
| 215 |
* @param array $prefixes |
| 216 |
* |
| 217 |
* @return array |
| 218 |
*/ |
| 219 |
private function removePrefixFromArrayKeys($array, $prefixes) |
| 220 |
{ |
| 221 |
foreach ($array as $key => $value) { |
| 222 |
$newKey = lcfirst(str_replace((array)$prefixes, '', $key)); |
| 223 |
|
| 224 |
if ($key !== $newKey) { |
| 225 |
unset($array[$key]); |
| 226 |
} |
| 227 |
|
| 228 |
if (is_array($value)) { |
| 229 |
$array[$newKey] = $this->removePrefixFromArrayKeys($value, $prefixes); |
| 230 |
continue; |
| 231 |
} |
| 232 |
|
| 233 |
$array[$newKey] = $value; |
| 234 |
} |
| 235 |
|
| 236 |
return $array; |
| 237 |
} |
| 238 |
} |
| 239 |
|