PluginProbe
GiveWP – Donation Plugin and Fundraising Platform / 4.16.8.1
GiveWP – Donation Plugin and Fundraising Platform v4.16.8.1
4.16.9 4.16.8.1 4.16.8 4.16.7.2 4.16.7.1 4.16.7 4.16.6.1 4.16.6 4.16.5.1 4.16.5 4.16.4 4.16.3 4.16.2 4.16.1 4.16.0 4.15.5 4.15.4 4.15.3 4.15.2 4.15.1 4.15.0 2.3.0 2.3.1 2.3.2 2.30.0 All 255 releases
give / src / Session / SessionDonation / DonationAccessor.php

DonationAccessor.php in GiveWP – Donation Plugin and Fundraising Platform 4.16.8.1, at src/Session/SessionDonation/DonationAccessor.php

119 lines 2.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Give\Session\SessionDonation;
4
5 use DateTime;
6 use Give\Session\Accessor;
7 use Give\Session\SessionDonation\SessionObjects\Donation as DonationObject;
8 use Give\Session\SessionDonation\SessionObjects\FormEntry;
9
10 /**
11 * Class Donation
12 *
13 * This class provide way to access donation session data.
14 *
15 * @package Give\Session\Access
16 *
17 * @method DonationObject get
18 */
19 class DonationAccessor extends Accessor
20 {
21 /**
22 * Session Id
23 *
24 * @since 2.7.0
25 * @var string
26 */
27 protected $sessionKey = 'give_purchase';
28
29 /**
30 * Donation object.
31 *
32 * Since 2.7.0
33 *
34 * @var DonationObject
35 */
36 protected $dataObj;
37
38 /**
39 * property vs session key array.
40 * It is useful to map array keys to class properties.
41 *
42 * @var array
43 */
44 private $renameTo = [
45 'user_email' => 'donorEmail',
46 'user_info' => 'donorInfo',
47 'post_data' => 'formEntry',
48 'donation_id' => 'id',
49 'price' => 'totalAmount',
50 'gateway' => 'paymentGateway',
51 'date' => 'createdAt',
52 ];
53
54 /**
55 * Map array keys to class properties
56 *
57 * @since 2.7.0
58 *
59 * @param array $data
60 *
61 * @return DonationObject
62 */
63 protected function convertToObject($data)
64 {
65 if ( ! $data) {
66 return null;
67 }
68
69 // Cast date string to DateTime object.
70 $data['date'] = DateTime::createFromFormat('Y-m-d H:i:s', $data['date']);
71
72 // Rename key if property name exist for them.
73 foreach ($data as $key => $value) {
74 if (array_key_exists($key, $this->renameTo)) {
75 $data[$this->renameTo[$key]] = $value;
76 unset($data[$key]);
77 }
78 }
79
80 // Rename unknown keys.
81 $data = $this->renameArrayKeysToPropertyNames($data);
82
83 return DonationObject::fromArray($data);
84 }
85
86 /**
87 * Get donation id.
88 *
89 * @since 2.7.0
90 * @return int
91 *
92 */
93 public function getDonationId()
94 {
95 if ($donationId = $this->getByKey('id')) {
96 return absint($donationId);
97 }
98
99 return null;
100 }
101
102 /**
103 * Get donation id.
104 *
105 * @since 2.7.0
106 * @return int
107 *
108 */
109 public function getFormId()
110 {
111 /* @var FormEntry $data */
112 if ($data = $this->getByKey('formEntry')) {
113 return absint($data->formId);
114 }
115
116 return null;
117 }
118 }
119