| 1 |
<?php |
| 2 |
/** |
| 3 |
* @package VikBooking |
| 4 |
* @subpackage core |
| 5 |
* @author E4J s.r.l. |
| 6 |
* @copyright Copyright (C) 2025 E4J s.r.l. All Rights Reserved. |
| 7 |
* @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL |
| 8 |
* @link https://vikwp.com |
| 9 |
*/ |
| 10 |
|
| 11 |
// No direct access |
| 12 |
defined('ABSPATH') or die('No script kiddies please!'); |
| 13 |
|
| 14 |
/** |
| 15 |
* RMS Pace Data-period abstract implementation. |
| 16 |
* |
| 17 |
* @since 1.18.6 (J) - 1.8.6 (WP) |
| 18 |
*/ |
| 19 |
abstract class VBORmsPaceDataperiod implements IteratorAggregate |
| 20 |
{ |
| 21 |
/** |
| 22 |
* @var array |
| 23 |
*/ |
| 24 |
private array $bookings = []; |
| 25 |
|
| 26 |
/** |
| 27 |
* @var DateTimeInterface |
| 28 |
*/ |
| 29 |
private DateTimeInterface $period; |
| 30 |
|
| 31 |
/** |
| 32 |
* @var ?DateInterval |
| 33 |
*/ |
| 34 |
private ?DateInterval $interval = null; |
| 35 |
|
| 36 |
/** |
| 37 |
* @var array |
| 38 |
*/ |
| 39 |
private array $listingsData = []; |
| 40 |
|
| 41 |
/** |
| 42 |
* Constucts the object by binding the various properties. |
| 43 |
* |
| 44 |
* @param array $bookings The period eligible bookings to iterate. |
| 45 |
* @param DateTimeInterface $period The data period under evaluation. |
| 46 |
* @param ?DateInterval $interval The date evaluation interval. |
| 47 |
*/ |
| 48 |
public function __construct(array $bookings, DateTimeInterface $period, ?DateInterval $interval = null) |
| 49 |
{ |
| 50 |
// bind properties |
| 51 |
$this->bookings = $bookings; |
| 52 |
$this->period = $period; |
| 53 |
$this->interval = $interval; |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* Returns the internal array iterator over the bookings. |
| 58 |
* |
| 59 |
* @return Traversable |
| 60 |
*/ |
| 61 |
public function getIterator(): Traversable |
| 62 |
{ |
| 63 |
return new ArrayIterator($this->bookings); |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* Returns the data period under evaluation. |
| 68 |
* |
| 69 |
* @return DateTimeInterface |
| 70 |
*/ |
| 71 |
public function getPeriod(): DateTimeInterface |
| 72 |
{ |
| 73 |
return $this->period; |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* Returns a list with the start and end period timestamps. |
| 78 |
* |
| 79 |
* @param bool $fromMidnight True to get the from timestamp at 00:00:00. |
| 80 |
* |
| 81 |
* @return array |
| 82 |
*/ |
| 83 |
public function getPeriodTimestamps(bool $fromMidnight = false): array |
| 84 |
{ |
| 85 |
// get the type of date interval |
| 86 |
$intervalType = $this->getIntervalType(); |
| 87 |
|
| 88 |
// clone the current date period |
| 89 |
$clonePeriod = clone $this->getPeriod(); |
| 90 |
|
| 91 |
// assume the date interval type is "DAY" |
| 92 |
if ($fromMidnight) { |
| 93 |
// start at 00:00:00 |
| 94 |
$clonePeriod->modify('00:00:00'); |
| 95 |
$tsFrom = $clonePeriod->format('U'); |
| 96 |
$clonePeriod->modify('23:59:59'); |
| 97 |
$tsTo = $clonePeriod->format('U'); |
| 98 |
} else { |
| 99 |
// start at 23:59:59 |
| 100 |
$clonePeriod->modify('23:59:59'); |
| 101 |
$tsFrom = $clonePeriod->format('U'); |
| 102 |
$tsTo = $tsFrom; |
| 103 |
} |
| 104 |
|
| 105 |
if ($intervalType === 'MONTH') { |
| 106 |
// adjust timestamp to the end of the month |
| 107 |
$tsTo = strtotime($clonePeriod->format('Y-m-t 23:59:59')); |
| 108 |
} |
| 109 |
|
| 110 |
return [$tsFrom, $tsTo]; |
| 111 |
} |
| 112 |
|
| 113 |
/** |
| 114 |
* Returns the current date evaluation interval. |
| 115 |
* |
| 116 |
* @return ?DateInterval |
| 117 |
*/ |
| 118 |
public function getInterval() |
| 119 |
{ |
| 120 |
return $this->interval; |
| 121 |
} |
| 122 |
|
| 123 |
/** |
| 124 |
* Returns the current date evaluation interval type. |
| 125 |
* |
| 126 |
* @return string Interval type like 'DAY' or 'MONTH'. |
| 127 |
*/ |
| 128 |
public function getIntervalType(): string |
| 129 |
{ |
| 130 |
// default interval type |
| 131 |
$intervalType = 'DAY'; |
| 132 |
|
| 133 |
if ($dateInterval = $this->getInterval()) { |
| 134 |
// check if the interval type is by month |
| 135 |
$intervalType = ($dateInterval->m ?? 0) ? 'MONTH' : 'DAY'; |
| 136 |
} |
| 137 |
|
| 138 |
return $intervalType; |
| 139 |
} |
| 140 |
|
| 141 |
/** |
| 142 |
* Returns the current listings data. |
| 143 |
* |
| 144 |
* @return array |
| 145 |
*/ |
| 146 |
public function getListings(): array |
| 147 |
{ |
| 148 |
return $this->listingsData; |
| 149 |
} |
| 150 |
|
| 151 |
/** |
| 152 |
* Sets the current listings data. |
| 153 |
* |
| 154 |
* @param array $listings Associative listings data list. |
| 155 |
* |
| 156 |
* @return self |
| 157 |
*/ |
| 158 |
public function setListings(array $listings): VBORmsPaceDataperiod |
| 159 |
{ |
| 160 |
$this->listingsData = $listings; |
| 161 |
|
| 162 |
return $this; |
| 163 |
} |
| 164 |
|
| 165 |
/** |
| 166 |
* Counts the total number of units for the present listings. |
| 167 |
* |
| 168 |
* @return int |
| 169 |
*/ |
| 170 |
public function getTotalInventoryCount(): int |
| 171 |
{ |
| 172 |
return (int) array_sum(array_column($this->listingsData, 'units')); |
| 173 |
} |
| 174 |
} |
| 175 |
|