PluginProbe
GiveWP – Donation Plugin and Fundraising Platform / 4.16.9
GiveWP – Donation Plugin and Fundraising Platform v4.16.9
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 / Donors / Models / Donor.php

Donor.php in GiveWP – Donation Plugin and Fundraising Platform 4.16.9, at src/Donors/Models/Donor.php

274 lines 6.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Give\Donors\Models;
4
5 use DateTime;
6 use Exception;
7 use Give\Donations\Models\Donation;
8 use Give\Donations\ValueObjects\DonationMetaKeys;
9 use Give\Donors\DataTransferObjects\DonorQueryData;
10 use Give\Donors\Factories\DonorFactory;
11 use Give\Donors\ValueObjects\DonorAddress;
12 use Give\Framework\Exceptions\Primitives\InvalidArgumentException;
13 use Give\Framework\Models\Contracts\ModelCrud;
14 use Give\Framework\Models\Contracts\ModelHasFactory;
15 use Give\Framework\Models\Model;
16 use Give\Framework\Models\ModelQueryBuilder;
17 use Give\Framework\Models\ValueObjects\Relationship;
18 use Give\Framework\Support\ValueObjects\Money;
19 use Give\Subscriptions\Models\Subscription;
20
21 /**
22 * Class Donor
23 *
24 * @since 4.4.0 Add "notes" property
25 * @since 3.7.0 Add "phone" property
26 * @since 2.24.0 add new properties $totalAmountDonated and $totalNumberOfDonations
27 * @since 2.19.6
28 *
29 * @property int $id
30 * @property int $userId
31 * @property DateTime $createdAt
32 * @property string $name
33 * @property string $prefix
34 * @property string $firstName
35 * @property string $lastName
36 * @property string $email
37 * @property string $phone
38 * @property string[] $additionalEmails
39 * @property DonorAddress[] $addresses
40 * @property int $avatarId
41 * @property string $company
42 * @property Money $totalAmountDonated
43 * @property int $totalNumberOfDonations
44 * @property Subscription[] $subscriptions
45 * @property Donation[] $donations
46 * @property DonorNote[] $notes
47 */
48 class Donor extends Model implements ModelCrud, ModelHasFactory
49 {
50 /**
51 * @inheritdoc
52 */
53 protected $properties = [
54 'id' => 'int',
55 'userId' => ['int', 0],
56 'createdAt' => DateTime::class,
57 'name' => 'string',
58 'firstName' => 'string',
59 'lastName' => 'string',
60 'email' => 'string',
61 'phone' => 'string',
62 'prefix' => 'string',
63 'additionalEmails' => ['array', []],
64 'addresses' => ['array', []],
65 'avatarId' => 'int',
66 'company' => 'string',
67 'totalAmountDonated' => Money::class,
68 'totalNumberOfDonations' => 'int',
69 ];
70
71 /**
72 * @inheritdoc
73 */
74 protected $relationships = [
75 'donations' => Relationship::HAS_MANY,
76 'subscriptions' => Relationship::HAS_MANY,
77 'notes' => Relationship::HAS_MANY,
78 ];
79
80 /**
81 * @since 2.19.6
82 *
83 * @param $id
84 *
85 * @return Donor|null
86 */
87 public static function find($id)
88 {
89 return give()->donors->getById($id);
90 }
91
92 /**
93 * @since 2.19.6
94 *
95 * @return Donor|null
96 */
97 public static function whereEmail(string $email)
98 {
99 return give()->donors->getByEmail($email);
100 }
101
102 /**
103 * @since 2.19.6
104 *
105 * @param string $donorEmail
106 *
107 * @return bool
108 */
109 public function hasEmail(string $donorEmail): bool
110 {
111 $emails = array_merge($this->additionalEmails ?? [], [$this->email]);
112
113 return in_array($donorEmail, $emails, true);
114 }
115
116 /**
117 * @since 2.21.0
118 *
119 * @param int $userId
120 *
121 * @return Donor|null
122 */
123 public static function whereUserId(int $userId)
124 {
125 return give()->donors->getByWpUserId($userId);
126 }
127
128 /**
129 * @since 2.20.0 return mutated model instance
130 * @since 2.19.6
131 *
132 * @param array $attributes
133 *
134 * @return Donor
135 *
136 * @throws Exception|InvalidArgumentException
137 */
138 public static function create(array $attributes): Donor
139 {
140 $donor = new static($attributes);
141
142 give()->donors->insert($donor);
143
144 return $donor;
145 }
146
147 /**
148 * @since 2.20.0 mutate model and return void
149 * @since 2.19.6
150 *
151 * @return void
152 *
153 * @throws Exception|InvalidArgumentException
154 */
155 public function save()
156 {
157 if ( ! $this->id) {
158 give()->donors->insert($this);
159 } else {
160 give()->donors->update($this);
161 }
162 }
163
164 /**
165 * @since 2.19.6
166 *
167 * @throws Exception
168 */
169 public function delete()
170 {
171 return give()->donors->delete($this);
172 }
173
174 /**
175 * @since 2.19.6
176 *
177 * @return ModelQueryBuilder<Donation>
178 */
179 public function donations(): ModelQueryBuilder
180 {
181 return give()->donations->queryByDonorId($this->id);
182 }
183
184 /**
185 * @since 2.19.6
186 *
187 * @return ModelQueryBuilder<Subscription>
188 */
189 public function subscriptions(): ModelQueryBuilder
190 {
191 return give()->subscriptions->queryByDonorId($this->id);
192 }
193
194 /**
195 * @since 2.19.6
196 *
197 * @return int
198 */
199 public function totalDonations(): int
200 {
201 return give()->donations->getTotalDonationCountByDonorId($this->id);
202 }
203
204 /**
205 * @since 2.19.6
206 *
207 * @return int
208 */
209 public function totalAmountDonated(): int
210 {
211 return array_sum(array_column($this->donations, DonationMetaKeys::AMOUNT()->getKeyAsCamelCase()));
212 }
213
214 /**
215 * @since 2.19.6
216 *
217 * @return ModelQueryBuilder<Donor>
218 */
219 public static function query(): ModelQueryBuilder
220 {
221 return give()->donors->prepareQuery();
222 }
223
224 /**
225 * @since 4.4.0
226 *
227 * @return ModelQueryBuilder<DonorNote>
228 */
229 public function notes(): ModelQueryBuilder
230 {
231 return give()->donors->notes->queryByDonorId($this->id);
232 }
233
234 /**
235 * @since 2.19.6
236 *
237 * @param object $object
238 *
239 * @return Donor
240 */
241 public static function fromQueryBuilderObject($object): Donor
242 {
243 return DonorQueryData::fromObject($object)->toDonor();
244 }
245
246 /**
247 * @since 2.19.6
248 *
249 * @return DonorFactory
250 */
251 public static function factory(): DonorFactory
252 {
253 return new DonorFactory(static::class);
254 }
255
256
257 /**
258 * @since 4.4.0
259 */
260 public function isAnonymous(): bool
261 {
262 if ($this->donations) {
263 foreach ($this->donations as $donation) {
264 if ($donation->anonymous) {
265 return true;
266 }
267 }
268 }
269
270 return false;
271 }
272
273 }
274