resources
1 year ago
CampaignDonationsBlockViewModel.php
1 year ago
app.tsx
1 year ago
block.json
1 year ago
edit.tsx
1 year ago
index.tsx
1 year ago
render.php
1 year ago
styles.scss
1 year ago
CampaignDonationsBlockViewModel.php
65 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Give\Campaigns\Blocks\CampaignDonations; |
| 4 | |
| 5 | use Give\Campaigns\Models\Campaign; |
| 6 | use Give\Framework\Support\ValueObjects\Money; |
| 7 | use Give\Framework\Views\View; |
| 8 | |
| 9 | /** |
| 10 | * @since 4.0.0 |
| 11 | */ |
| 12 | class CampaignDonationsBlockViewModel |
| 13 | { |
| 14 | /** |
| 15 | * @var Campaign $campaign |
| 16 | */ |
| 17 | private $campaign; |
| 18 | |
| 19 | /** |
| 20 | * @var array |
| 21 | */ |
| 22 | private $donations; |
| 23 | |
| 24 | /** |
| 25 | * @var array $attributes |
| 26 | */ |
| 27 | private $attributes; |
| 28 | |
| 29 | /** |
| 30 | * @since 4.0.0 |
| 31 | */ |
| 32 | public function __construct(Campaign $campaign, array $donations, array $attributes) |
| 33 | { |
| 34 | $this->attributes = $attributes; |
| 35 | $this->campaign = $campaign; |
| 36 | $this->donations = $donations; |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * @since 4.0.0 |
| 41 | */ |
| 42 | public function render(): void |
| 43 | { |
| 44 | View::render('Campaigns/Blocks/CampaignDonations.render', [ |
| 45 | 'campaign' => $this->campaign, |
| 46 | 'donations' => $this->formatDonationsData($this->donations), |
| 47 | 'attributes' => $this->attributes, |
| 48 | ]); |
| 49 | } |
| 50 | |
| 51 | |
| 52 | /** |
| 53 | * @since 4.0.0 |
| 54 | */ |
| 55 | private function formatDonationsData(array $donations): array |
| 56 | { |
| 57 | return array_map(static function ($entry) { |
| 58 | $entry->date = human_time_diff(strtotime($entry->date)); |
| 59 | $entry->amount = Money::fromDecimal($entry->amount, give_get_currency()); |
| 60 | |
| 61 | return $entry; |
| 62 | }, $donations); |
| 63 | } |
| 64 | } |
| 65 |