Donation.php
193 lines
| 1 | <?php |
| 2 | namespace Give\Session\SessionDonation\SessionObjects; |
| 3 | |
| 4 | use DateTime; |
| 5 | use Give\Framework\Exceptions\Primitives\InvalidArgumentException; |
| 6 | use Give\Helpers\ArrayDataSet; |
| 7 | use Give\Session\Objects; |
| 8 | use Give\ValueObjects\CardInfo; |
| 9 | use Give\ValueObjects\DonorInfo; |
| 10 | use Give\ValueObjects\ValueObjects; |
| 11 | |
| 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 | class Donation implements Objects { |
| 27 | /** |
| 28 | * Donation id. |
| 29 | * |
| 30 | * @since 2.7.0 |
| 31 | * @var array |
| 32 | */ |
| 33 | public $id; |
| 34 | |
| 35 | /** |
| 36 | * Sanitized donation total amount. |
| 37 | * |
| 38 | * @since 2.7.0 |
| 39 | * @var string |
| 40 | */ |
| 41 | public $totalAmount; |
| 42 | |
| 43 | /** |
| 44 | * Donation purchase key. |
| 45 | * |
| 46 | * @since 2.7.0 |
| 47 | * @var string |
| 48 | */ |
| 49 | public $purchaseKey; |
| 50 | |
| 51 | /** |
| 52 | * Donor email. |
| 53 | * |
| 54 | * @since 2.7.0 |
| 55 | * @var string |
| 56 | */ |
| 57 | public $donorEmail; |
| 58 | |
| 59 | /** |
| 60 | * Donor email. |
| 61 | * |
| 62 | * @since 2.7.0 |
| 63 | * @var DateTime |
| 64 | */ |
| 65 | public $createdAt; |
| 66 | |
| 67 | /** |
| 68 | * Payment gateway id. |
| 69 | * |
| 70 | * @since 2.7.0 |
| 71 | * @var array |
| 72 | */ |
| 73 | public $paymentGateway; |
| 74 | |
| 75 | /** |
| 76 | * Array of properties and their cast type. |
| 77 | * |
| 78 | * @var ValueObjects[] |
| 79 | */ |
| 80 | private $caseTo = [ |
| 81 | 'formEntry' => FormEntry::class, |
| 82 | 'donorInfo' => DonorInfo::class, |
| 83 | 'cardInfo' => CardInfo::class, |
| 84 | ]; |
| 85 | |
| 86 | /** |
| 87 | * Take array and return object. |
| 88 | * |
| 89 | * @param $array |
| 90 | * |
| 91 | * @return Donation |
| 92 | */ |
| 93 | public static function fromArray( $array ) { |
| 94 | $expectedKeys = [ 'id', 'totalAmount', 'purchaseKey', 'donorEmail', 'createdAt', 'paymentGateway', 'formEntry', 'cardInfo', 'donorInfo' ]; |
| 95 | |
| 96 | if ( ! ArrayDataSet::hasRequiredKeys( $array, $expectedKeys ) ) { |
| 97 | throw new InvalidArgumentException( |
| 98 | 'Invalid Donation object, must have the exact following keys: ' . implode( ', ', $expectedKeys ) |
| 99 | ); |
| 100 | } |
| 101 | |
| 102 | $donation = new self(); |
| 103 | |
| 104 | $array['donorInfo'] = $donation->renameKeyInDonorInfo( $array['donorInfo'] ); |
| 105 | $array['cardInfo'] = $donation->filterCardInfoKeys( $array['cardInfo'] ); |
| 106 | |
| 107 | foreach ( $array as $key => $value ) { |
| 108 | if ( array_key_exists( $key, $donation->caseTo ) ) { |
| 109 | $class = $donation->caseTo[ $key ]; |
| 110 | $donation->{$key} = $class::fromArray( $value ); |
| 111 | continue; |
| 112 | } |
| 113 | |
| 114 | $donation->{$key} = is_array( $value ) ? |
| 115 | json_decode( json_encode( $value ) ) // Convert unlisted array type session data to stdClass object |
| 116 | : $value; |
| 117 | } |
| 118 | |
| 119 | return $donation; |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * Rename array key in donor info |
| 124 | * |
| 125 | * @since 2.7.0 |
| 126 | * @param array $array |
| 127 | * |
| 128 | * @return array |
| 129 | */ |
| 130 | private function renameKeyInDonorInfo( $array ) { |
| 131 | return ArrayDataSet::renameKeys( |
| 132 | $array, |
| 133 | [ |
| 134 | 'id' => 'wpUserId', |
| 135 | 'title' => 'honorific', |
| 136 | ] |
| 137 | ); |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * Filter array keys in card info |
| 142 | * |
| 143 | * @since 2.7.0 |
| 144 | * @param $array |
| 145 | * @return array |
| 146 | */ |
| 147 | private function filterCardInfoKeys( $array ) { |
| 148 | $array = $this->removePrefixFromArrayKeys( $array, [ 'card' ] ); |
| 149 | $array = ArrayDataSet::renameKeys( |
| 150 | $array, |
| 151 | [ |
| 152 | 'address' => 'line1', |
| 153 | 'address2' => 'line2', |
| 154 | ] |
| 155 | ); |
| 156 | $array = ArrayDataSet::moveArrayItemsUnderArrayKey( $array, [ 'line1', 'line2', 'city', 'state', 'country', 'zip' ], 'address' ); |
| 157 | |
| 158 | // Rename zip to postal code. |
| 159 | $array['address']['postalCode'] = $array['address']['zip']; |
| 160 | unset( $array['address']['zip'] ); |
| 161 | |
| 162 | return $array; |
| 163 | } |
| 164 | |
| 165 | /** |
| 166 | * Remove prefix from array key. |
| 167 | * |
| 168 | * @param array $array |
| 169 | * @param array $prefixes |
| 170 | * |
| 171 | * @return array |
| 172 | */ |
| 173 | private function removePrefixFromArrayKeys( $array, $prefixes ) { |
| 174 | foreach ( $array as $key => $value ) { |
| 175 | $newKey = lcfirst( str_replace( (array) $prefixes, '', $key ) ); |
| 176 | |
| 177 | if ( $key !== $newKey ) { |
| 178 | unset( $array[ $key ] ); |
| 179 | } |
| 180 | |
| 181 | if ( is_array( $value ) ) { |
| 182 | $array[ $newKey ] = $this->removePrefixFromArrayKeys( $value, $prefixes ); |
| 183 | continue; |
| 184 | } |
| 185 | |
| 186 | $array[ $newKey ] = $value; |
| 187 | |
| 188 | } |
| 189 | |
| 190 | return $array; |
| 191 | } |
| 192 | } |
| 193 |