| 1 |
<?php |
| 2 |
/** |
| 3 |
* Report: DateRange |
| 4 |
* |
| 5 |
* @package SimplePay |
| 6 |
* @subpackage Core |
| 7 |
* @copyright Copyright (c) 2023, Sandhills Development, LLC |
| 8 |
* @license http://opensource.org/licenses/gpl-2.0.php GNU Public License |
| 9 |
* @since 4.6.7 |
| 10 |
*/ |
| 11 |
|
| 12 |
namespace SimplePay\Core\Report; |
| 13 |
|
| 14 |
use DateTimeImmutable; |
| 15 |
use InvalidArgumentException; |
| 16 |
|
| 17 |
/** |
| 18 |
* DateRange class. |
| 19 |
* |
| 20 |
* Standardizes the date range for reports. |
| 21 |
* |
| 22 |
* @since 4.6.7 |
| 23 |
*/ |
| 24 |
class DateRange { |
| 25 |
|
| 26 |
// Valid range types. |
| 27 |
const RANGE_TYPES = array( |
| 28 |
'today', |
| 29 |
'7days', |
| 30 |
'4weeks', |
| 31 |
'3months', |
| 32 |
'12months', |
| 33 |
'monthtodate', |
| 34 |
'yeartodate', |
| 35 |
'custom', |
| 36 |
); |
| 37 |
|
| 38 |
/** |
| 39 |
* The type of date range. |
| 40 |
* |
| 41 |
* @var string |
| 42 |
*/ |
| 43 |
public $type; |
| 44 |
|
| 45 |
/** |
| 46 |
* The start of the date range. |
| 47 |
* |
| 48 |
* @since 4.6.7 |
| 49 |
* |
| 50 |
* @var \DateTimeImmutable |
| 51 |
*/ |
| 52 |
public $start; |
| 53 |
|
| 54 |
/** |
| 55 |
* The end of the date range. |
| 56 |
* |
| 57 |
* @since 4.6.7 |
| 58 |
* |
| 59 |
* @var \DateTimeImmutable |
| 60 |
*/ |
| 61 |
public $end; |
| 62 |
|
| 63 |
/** |
| 64 |
* DateRange. |
| 65 |
* |
| 66 |
* @since 4.6.7 |
| 67 |
* |
| 68 |
* @param string $type The type of date range. |
| 69 |
* @param string $start The start of the date range. |
| 70 |
* @param string $end The end of the date range. |
| 71 |
* @throws \InvalidArgumentException If the date range range is invalid. |
| 72 |
* @throws \InvalidArgumentException If the date range start is invalid. |
| 73 |
* @throws \InvalidArgumentException If the date range end is invalid. |
| 74 |
*/ |
| 75 |
public function __construct( $type, $start, $end ) { |
| 76 |
if ( ! in_array( $type, self::RANGE_TYPES, true ) ) { |
| 77 |
throw new InvalidArgumentException( 'Invalid date range type.' ); |
| 78 |
} |
| 79 |
|
| 80 |
$start = new DateTimeImmutable( $start ); |
| 81 |
|
| 82 |
if ( ! $start instanceof DateTimeImmutable ) { |
| 83 |
throw new InvalidArgumentException( 'Invalid date range start.' ); |
| 84 |
} |
| 85 |
|
| 86 |
$end = new DateTimeImmutable( $end ); |
| 87 |
|
| 88 |
if ( ! $end instanceof DateTimeImmutable ) { |
| 89 |
throw new InvalidArgumentException( 'Invalid date range end.' ); |
| 90 |
} |
| 91 |
|
| 92 |
$this->type = $type; |
| 93 |
$this->start = $start; |
| 94 |
$this->end = $end; |
| 95 |
} |
| 96 |
|
| 97 |
} |
| 98 |
|