| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
namespace Carbon\Traits; |
| 15 |
|
| 16 |
use Carbon\CarbonInterface; |
| 17 |
use Carbon\Exceptions\UnknownUnitException; |
| 18 |
use Carbon\WeekDay; |
| 19 |
use DateInterval; |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
trait Rounding |
| 32 |
{ |
| 33 |
use IntervalRounding; |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
public function roundUnit( |
| 39 |
string $unit, |
| 40 |
DateInterval|string|float|int $precision = 1, |
| 41 |
callable|string $function = 'round', |
| 42 |
): static { |
| 43 |
$metaUnits = [ |
| 44 |
|
| 45 |
'millennium' => [static::YEARS_PER_MILLENNIUM, 'year'], |
| 46 |
|
| 47 |
'century' => [static::YEARS_PER_CENTURY, 'year'], |
| 48 |
|
| 49 |
'decade' => [static::YEARS_PER_DECADE, 'year'], |
| 50 |
|
| 51 |
'quarter' => [static::MONTHS_PER_QUARTER, 'month'], |
| 52 |
|
| 53 |
'millisecond' => [1000, 'microsecond'], |
| 54 |
]; |
| 55 |
$normalizedUnit = static::singularUnit($unit); |
| 56 |
$ranges = array_merge(static::getRangesByUnit($this->daysInMonth), [ |
| 57 |
|
| 58 |
'microsecond' => [0, 999999], |
| 59 |
]); |
| 60 |
$factor = 1; |
| 61 |
|
| 62 |
if ($normalizedUnit === 'week') { |
| 63 |
$normalizedUnit = 'day'; |
| 64 |
$precision *= static::DAYS_PER_WEEK; |
| 65 |
} |
| 66 |
|
| 67 |
if (isset($metaUnits[$normalizedUnit])) { |
| 68 |
[$factor, $normalizedUnit] = $metaUnits[$normalizedUnit]; |
| 69 |
} |
| 70 |
|
| 71 |
$precision *= $factor; |
| 72 |
|
| 73 |
if (!isset($ranges[$normalizedUnit])) { |
| 74 |
throw new UnknownUnitException($unit); |
| 75 |
} |
| 76 |
|
| 77 |
$found = false; |
| 78 |
$fraction = 0; |
| 79 |
$arguments = null; |
| 80 |
$initialValue = null; |
| 81 |
$factor = $this->year < 0 ? -1 : 1; |
| 82 |
$changes = []; |
| 83 |
$minimumInc = null; |
| 84 |
|
| 85 |
foreach ($ranges as $unit => [$minimum, $maximum]) { |
| 86 |
if ($normalizedUnit === $unit) { |
| 87 |
$arguments = [$this->$unit, $minimum]; |
| 88 |
$initialValue = $this->$unit; |
| 89 |
$fraction = $precision - floor($precision); |
| 90 |
$found = true; |
| 91 |
|
| 92 |
continue; |
| 93 |
} |
| 94 |
|
| 95 |
if ($found) { |
| 96 |
$delta = $maximum + 1 - $minimum; |
| 97 |
$factor /= $delta; |
| 98 |
$fraction *= $delta; |
| 99 |
$inc = ($this->$unit - $minimum) * $factor; |
| 100 |
|
| 101 |
if ($inc !== 0.0) { |
| 102 |
$minimumInc = $minimumInc ?? ($arguments[0] / pow(2, 52)); |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
if (abs($inc) < $minimumInc) { |
| 107 |
$inc = $minimumInc * ($inc < 0 ? -1 : 1); |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
if ($function !== 'floor' || abs($arguments[0] + $inc - $initialValue) >= $precision) { |
| 112 |
$arguments[0] += $inc; |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
$changes[$unit] = round( |
| 117 |
$minimum + ($fraction ? $fraction * $function(($this->$unit - $minimum) / $fraction) : 0), |
| 118 |
); |
| 119 |
|
| 120 |
|
| 121 |
while ($changes[$unit] >= $delta) { |
| 122 |
$changes[$unit] -= $delta; |
| 123 |
} |
| 124 |
|
| 125 |
$fraction -= floor($fraction); |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
[$value, $minimum] = $arguments; |
| 130 |
$normalizedValue = floor($function(($value - $minimum) / $precision) * $precision + $minimum); |
| 131 |
|
| 132 |
|
| 133 |
$result = $this; |
| 134 |
|
| 135 |
foreach ($changes as $unit => $value) { |
| 136 |
$result = $result->$unit($value); |
| 137 |
} |
| 138 |
|
| 139 |
return $result->$normalizedUnit($normalizedValue); |
| 140 |
} |
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
public function floorUnit(string $unit, DateInterval|string|float|int $precision = 1): static |
| 146 |
{ |
| 147 |
return $this->roundUnit($unit, $precision, 'floor'); |
| 148 |
} |
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
public function ceilUnit(string $unit, DateInterval|string|float|int $precision = 1): static |
| 154 |
{ |
| 155 |
return $this->roundUnit($unit, $precision, 'ceil'); |
| 156 |
} |
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
public function round(DateInterval|string|float|int $precision = 1, callable|string $function = 'round'): static |
| 162 |
{ |
| 163 |
return $this->roundWith($precision, $function); |
| 164 |
} |
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
public function floor(DateInterval|string|float|int $precision = 1): static |
| 170 |
{ |
| 171 |
return $this->round($precision, 'floor'); |
| 172 |
} |
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
public function ceil(DateInterval|string|float|int $precision = 1): static |
| 178 |
{ |
| 179 |
return $this->round($precision, 'ceil'); |
| 180 |
} |
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
public function roundWeek(WeekDay|int|null $weekStartsAt = null): static |
| 188 |
{ |
| 189 |
return $this->closest( |
| 190 |
$this->avoidMutation()->floorWeek($weekStartsAt), |
| 191 |
$this->avoidMutation()->ceilWeek($weekStartsAt), |
| 192 |
); |
| 193 |
} |
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
public function floorWeek(WeekDay|int|null $weekStartsAt = null): static |
| 201 |
{ |
| 202 |
return $this->startOfWeek($weekStartsAt); |
| 203 |
} |
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
public function ceilWeek(WeekDay|int|null $weekStartsAt = null): static |
| 211 |
{ |
| 212 |
if ($this->isMutable()) { |
| 213 |
$startOfWeek = $this->avoidMutation()->startOfWeek($weekStartsAt); |
| 214 |
|
| 215 |
return $startOfWeek != $this ? |
| 216 |
$this->startOfWeek($weekStartsAt)->addWeek() : |
| 217 |
$this; |
| 218 |
} |
| 219 |
|
| 220 |
$startOfWeek = $this->startOfWeek($weekStartsAt); |
| 221 |
|
| 222 |
return $startOfWeek != $this ? |
| 223 |
$startOfWeek->addWeek() : |
| 224 |
$this->avoidMutation(); |
| 225 |
} |
| 226 |
} |