Boundaries.php
4 years ago
Cast.php
4 years ago
Comparison.php
2 years ago
Converter.php
2 years ago
Creator.php
2 years ago
Date.php
9 months ago
DeprecatedProperties.php
4 years ago
Difference.php
2 years ago
IntervalRounding.php
2 years ago
IntervalStep.php
4 years ago
Localization.php
9 months ago
Macro.php
4 years ago
MagicParameter.php
2 years ago
Mixin.php
2 years ago
Modifiers.php
4 years ago
Mutability.php
4 years ago
ObjectInitialisation.php
4 years ago
Options.php
2 years ago
Rounding.php
2 years ago
Serialization.php
2 years ago
Test.php
9 months ago
Timestamp.php
1 year ago
ToStringFormat.php
3 years ago
Units.php
2 years ago
Week.php
4 years ago
index.php
3 years ago
Modifiers.php
158 lines
| 1 | <?php |
| 2 | namespace MailPoetVendor\Carbon\Traits; |
| 3 | if (!defined('ABSPATH')) exit; |
| 4 | use MailPoetVendor\Carbon\CarbonInterface; |
| 5 | use ReturnTypeWillChange; |
| 6 | trait Modifiers |
| 7 | { |
| 8 | protected static $midDayAt = 12; |
| 9 | public static function getMidDayAt() |
| 10 | { |
| 11 | return static::$midDayAt; |
| 12 | } |
| 13 | public static function setMidDayAt($hour) |
| 14 | { |
| 15 | static::$midDayAt = $hour; |
| 16 | } |
| 17 | public function midDay() |
| 18 | { |
| 19 | return $this->setTime(static::$midDayAt, 0, 0, 0); |
| 20 | } |
| 21 | public function next($modifier = null) |
| 22 | { |
| 23 | if ($modifier === null) { |
| 24 | $modifier = $this->dayOfWeek; |
| 25 | } |
| 26 | return $this->change('next ' . (\is_string($modifier) ? $modifier : static::$days[$modifier])); |
| 27 | } |
| 28 | private function nextOrPreviousDay($weekday = \true, $forward = \true) |
| 29 | { |
| 30 | $date = $this; |
| 31 | $step = $forward ? 1 : -1; |
| 32 | do { |
| 33 | $date = $date->addDays($step); |
| 34 | } while ($weekday ? $date->isWeekend() : $date->isWeekday()); |
| 35 | return $date; |
| 36 | } |
| 37 | public function nextWeekday() |
| 38 | { |
| 39 | return $this->nextOrPreviousDay(); |
| 40 | } |
| 41 | public function previousWeekday() |
| 42 | { |
| 43 | return $this->nextOrPreviousDay(\true, \false); |
| 44 | } |
| 45 | public function nextWeekendDay() |
| 46 | { |
| 47 | return $this->nextOrPreviousDay(\false); |
| 48 | } |
| 49 | public function previousWeekendDay() |
| 50 | { |
| 51 | return $this->nextOrPreviousDay(\false, \false); |
| 52 | } |
| 53 | public function previous($modifier = null) |
| 54 | { |
| 55 | if ($modifier === null) { |
| 56 | $modifier = $this->dayOfWeek; |
| 57 | } |
| 58 | return $this->change('last ' . (\is_string($modifier) ? $modifier : static::$days[$modifier])); |
| 59 | } |
| 60 | public function firstOfMonth($dayOfWeek = null) |
| 61 | { |
| 62 | $date = $this->startOfDay(); |
| 63 | if ($dayOfWeek === null) { |
| 64 | return $date->day(1); |
| 65 | } |
| 66 | return $date->modify('first ' . static::$days[$dayOfWeek] . ' of ' . $date->rawFormat('F') . ' ' . $date->year); |
| 67 | } |
| 68 | public function lastOfMonth($dayOfWeek = null) |
| 69 | { |
| 70 | $date = $this->startOfDay(); |
| 71 | if ($dayOfWeek === null) { |
| 72 | return $date->day($date->daysInMonth); |
| 73 | } |
| 74 | return $date->modify('last ' . static::$days[$dayOfWeek] . ' of ' . $date->rawFormat('F') . ' ' . $date->year); |
| 75 | } |
| 76 | public function nthOfMonth($nth, $dayOfWeek) |
| 77 | { |
| 78 | $date = $this->avoidMutation()->firstOfMonth(); |
| 79 | $check = $date->rawFormat('Y-m'); |
| 80 | $date = $date->modify('+' . $nth . ' ' . static::$days[$dayOfWeek]); |
| 81 | return $date->rawFormat('Y-m') === $check ? $this->modify((string) $date) : \false; |
| 82 | } |
| 83 | public function firstOfQuarter($dayOfWeek = null) |
| 84 | { |
| 85 | return $this->setDate($this->year, $this->quarter * static::MONTHS_PER_QUARTER - 2, 1)->firstOfMonth($dayOfWeek); |
| 86 | } |
| 87 | public function lastOfQuarter($dayOfWeek = null) |
| 88 | { |
| 89 | return $this->setDate($this->year, $this->quarter * static::MONTHS_PER_QUARTER, 1)->lastOfMonth($dayOfWeek); |
| 90 | } |
| 91 | public function nthOfQuarter($nth, $dayOfWeek) |
| 92 | { |
| 93 | $date = $this->avoidMutation()->day(1)->month($this->quarter * static::MONTHS_PER_QUARTER); |
| 94 | $lastMonth = $date->month; |
| 95 | $year = $date->year; |
| 96 | $date = $date->firstOfQuarter()->modify('+' . $nth . ' ' . static::$days[$dayOfWeek]); |
| 97 | return $lastMonth < $date->month || $year !== $date->year ? \false : $this->modify((string) $date); |
| 98 | } |
| 99 | public function firstOfYear($dayOfWeek = null) |
| 100 | { |
| 101 | return $this->month(1)->firstOfMonth($dayOfWeek); |
| 102 | } |
| 103 | public function lastOfYear($dayOfWeek = null) |
| 104 | { |
| 105 | return $this->month(static::MONTHS_PER_YEAR)->lastOfMonth($dayOfWeek); |
| 106 | } |
| 107 | public function nthOfYear($nth, $dayOfWeek) |
| 108 | { |
| 109 | $date = $this->avoidMutation()->firstOfYear()->modify('+' . $nth . ' ' . static::$days[$dayOfWeek]); |
| 110 | return $this->year === $date->year ? $this->modify((string) $date) : \false; |
| 111 | } |
| 112 | public function average($date = null) |
| 113 | { |
| 114 | return $this->addRealMicroseconds((int) ($this->diffInRealMicroseconds($this->resolveCarbon($date), \false) / 2)); |
| 115 | } |
| 116 | public function closest($date1, $date2) |
| 117 | { |
| 118 | return $this->diffInRealMicroseconds($date1) < $this->diffInRealMicroseconds($date2) ? $date1 : $date2; |
| 119 | } |
| 120 | public function farthest($date1, $date2) |
| 121 | { |
| 122 | return $this->diffInRealMicroseconds($date1) > $this->diffInRealMicroseconds($date2) ? $date1 : $date2; |
| 123 | } |
| 124 | public function min($date = null) |
| 125 | { |
| 126 | $date = $this->resolveCarbon($date); |
| 127 | return $this->lt($date) ? $this : $date; |
| 128 | } |
| 129 | public function minimum($date = null) |
| 130 | { |
| 131 | return $this->min($date); |
| 132 | } |
| 133 | public function max($date = null) |
| 134 | { |
| 135 | $date = $this->resolveCarbon($date); |
| 136 | return $this->gt($date) ? $this : $date; |
| 137 | } |
| 138 | public function maximum($date = null) |
| 139 | { |
| 140 | return $this->max($date); |
| 141 | } |
| 142 | #[\ReturnTypeWillChange] |
| 143 | public function modify($modify) |
| 144 | { |
| 145 | return parent::modify((string) $modify); |
| 146 | } |
| 147 | public function change($modifier) |
| 148 | { |
| 149 | return $this->modify(\preg_replace_callback('/^(next|previous|last)\\s+(\\d{1,2}(h|am|pm|:\\d{1,2}(:\\d{1,2})?))$/i', function ($match) { |
| 150 | $match[2] = \str_replace('h', ':00', $match[2]); |
| 151 | $test = $this->avoidMutation()->modify($match[2]); |
| 152 | $method = $match[1] === 'next' ? 'lt' : 'gt'; |
| 153 | $match[1] = $test->{$method}($this) ? $match[1] . ' day' : 'today'; |
| 154 | return $match[1] . ' ' . $match[2]; |
| 155 | }, \strtr(\trim($modifier), [' at ' => ' ', 'just now' => 'now', 'after tomorrow' => 'tomorrow +1 day', 'before yesterday' => 'yesterday -1 day']))); |
| 156 | } |
| 157 | } |
| 158 |