This repository has been archived by the owner on Apr 30, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 347
/
Period.php
executable file
·106 lines (92 loc) · 1.79 KB
/
Period.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
<?php
declare(strict_types=1);
namespace Rinvex\Subscriptions\Services;
use Carbon\Carbon;
class Period
{
/**
* Starting date of the period.
*
* @var string
*/
protected $start;
/**
* Ending date of the period.
*
* @var string
*/
protected $end;
/**
* Interval.
*
* @var string
*/
protected $interval;
/**
* Interval count.
*
* @var int
*/
protected $period = 1;
/**
* Create a new Period instance.
*
* @param string $interval
* @param int $count
* @param string $start
*
* @return void
*/
public function __construct($interval = 'month', $count = 1, $start = '')
{
if (empty($start)) {
$this->start = now();
} elseif (! $start instanceof Carbon) {
$this->start = new Carbon($start);
} else {
$this->start = $start;
}
if ($count > 0) {
$this->period = $count;
}
$start = clone $this->start;
$method = 'add'.ucfirst($this->interval).'s';
$this->end = $start->{$method}($this->period);
}
/**
* Get start date.
*
* @return \Carbon\Carbon
*/
public function getStartDate(): Carbon
{
return $this->start;
}
/**
* Get end date.
*
* @return \Carbon\Carbon
*/
public function getEndDate(): Carbon
{
return $this->end;
}
/**
* Get period interval.
*
* @return string
*/
public function getInterval(): string
{
return $this->interval;
}
/**
* Get period interval count.
*
* @return int
*/
public function getIntervalCount(): int
{
return $this->period;
}
}