1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Sagitarius29\LaravelSubscriptions\Entities; |
4
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Model; |
6
|
|
|
use Sagitarius29\LaravelSubscriptions\Contracts\PlanPriceContract; |
7
|
|
|
use Sagitarius29\LaravelSubscriptions\Exceptions\PriceErrorException; |
8
|
|
|
|
9
|
|
|
class PlanPrice extends Model implements PlanPriceContract |
10
|
|
|
{ |
11
|
|
|
protected $table = 'plan_prices'; |
12
|
|
|
|
13
|
|
|
protected $fillable = [ |
14
|
|
|
'amount', 'interval', 'interval_unit', |
15
|
|
|
]; |
16
|
|
|
|
17
|
|
|
public static $DAY = 'day'; |
18
|
|
|
public static $MONTH = 'month'; |
19
|
|
|
public static $YEAR = 'year'; |
20
|
|
|
|
21
|
|
|
public function plan() |
22
|
|
|
{ |
23
|
|
|
return $this->belongsTo(Plan::class); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
public function getAmount(): int |
27
|
|
|
{ |
28
|
|
|
return $this->amount; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public static function make($interval, int $intervalUnit, float $amount): PlanPriceContract |
32
|
|
|
{ |
33
|
|
|
self::checkPriceInterval($interval); |
34
|
|
|
|
35
|
|
|
$attributes = [ |
36
|
|
|
'amount' => $amount, |
37
|
|
|
'interval' => $interval, |
38
|
|
|
'interval_unit' => $intervalUnit, |
39
|
|
|
]; |
40
|
|
|
|
41
|
|
|
return new self($attributes); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public static function makeWithoutInterval(float $amount): PlanPriceContract |
45
|
|
|
{ |
46
|
|
|
return new self([ |
47
|
|
|
'amount' => $amount, |
48
|
|
|
]); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function getInterval(): string |
52
|
|
|
{ |
53
|
|
|
return $this->interval; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function getIntervalUnit(): int |
57
|
|
|
{ |
58
|
|
|
return $this->interval_unit; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
protected static function checkPriceInterval(string $interval) |
62
|
|
|
{ |
63
|
|
|
$intervals = [ |
64
|
|
|
self::$DAY, self::$MONTH, self::$YEAR, |
65
|
|
|
]; |
66
|
|
|
if (! in_array($interval, $intervals)) { |
67
|
|
|
throw new PriceErrorException( |
68
|
|
|
'The interval \''.$interval.'\' is not correct. Available intervals are: '.implode(', ', $intervals) |
69
|
|
|
); |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|