-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlanFeatureResource.php
42 lines (37 loc) · 1.55 KB
/
PlanFeatureResource.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
<?php
namespace Err0r\Larasub\Resources;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;
class PlanFeatureResource extends JsonResource
{
public function __construct($resource, private $subscription = null)
{
parent::__construct($resource);
}
/**
* Transform the resource into an array.
*
* @return array<string, mixed>
*/
public function toArray(Request $request): array
{
$isSubscriptionPresent = $this->subscription instanceof (config('larasub.models.subscription'));
return [
'id' => $this->id,
'value' => $this->value,
'display_value' => $this->display_value,
'reset_period' => $this->reset_period,
'reset_period_type' => $this->reset_period_type,
'sort_order' => $this->sort_order,
'plan' => new (config('larasub.resources.plan'))($this->whenLoaded('plan')),
'feature' => new (config('larasub.resources.feature'))($this->whenLoaded('feature')),
$this->mergeWhen($isSubscriptionPresent && $this->feature->isConsumable(), fn () => [
'total_usages' => $this->subscription->totalFeatureUsageInPeriod($this->feature->slug),
'remaining_usages' => $this->subscription->remainingFeatureUsage($this->feature->slug),
'next_reset_at' => $this->subscription->nextAvailableFeatureUsage($this->feature->slug),
]),
'created_at' => $this->created_at,
'updated_at' => $this->updated_at,
];
}
}