Exceptions
6 years ago
Lang
6 years ago
Laravel
6 years ago
Carbon.php
6 years ago
CarbonInterval.php
6 years ago
CarbonPeriod.php
6 years ago
Translator.php
6 years ago
Upgrade.php
6 years ago
index.php
6 years ago
CarbonInterval.php
979 lines
| 1 | <?php |
| 2 | |
| 3 | /* |
| 4 | * This file is part of the Carbon package. |
| 5 | * |
| 6 | * (c) Brian Nesbitt <brian@nesbot.com> |
| 7 | * |
| 8 | * For the full copyright and license information, please view the LICENSE |
| 9 | * file that was distributed with this source code. |
| 10 | */ |
| 11 | namespace MailPoetVendor\Carbon; |
| 12 | |
| 13 | if (!defined('ABSPATH')) exit; |
| 14 | |
| 15 | |
| 16 | use Closure; |
| 17 | use DateInterval; |
| 18 | use InvalidArgumentException; |
| 19 | use ReflectionClass; |
| 20 | use ReflectionFunction; |
| 21 | use ReflectionMethod; |
| 22 | use MailPoetVendor\Symfony\Component\Translation\TranslatorInterface; |
| 23 | /** |
| 24 | * A simple API extension for DateInterval. |
| 25 | * The implementation provides helpers to handle weeks but only days are saved. |
| 26 | * Weeks are calculated based on the total days of the current instance. |
| 27 | * |
| 28 | * @property int $years Total years of the current interval. |
| 29 | * @property int $months Total months of the current interval. |
| 30 | * @property int $weeks Total weeks of the current interval calculated from the days. |
| 31 | * @property int $dayz Total days of the current interval (weeks * 7 + days). |
| 32 | * @property int $hours Total hours of the current interval. |
| 33 | * @property int $minutes Total minutes of the current interval. |
| 34 | * @property int $seconds Total seconds of the current interval. |
| 35 | * @property-read int $dayzExcludeWeeks Total days remaining in the final week of the current instance (days % 7). |
| 36 | * @property-read int $daysExcludeWeeks alias of dayzExcludeWeeks |
| 37 | * @property-read float $totalYears Number of years equivalent to the interval. |
| 38 | * @property-read float $totalMonths Number of months equivalent to the interval. |
| 39 | * @property-read float $totalWeeks Number of weeks equivalent to the interval. |
| 40 | * @property-read float $totalDays Number of days equivalent to the interval. |
| 41 | * @property-read float $totalDayz Alias for totalDays. |
| 42 | * @property-read float $totalHours Number of hours equivalent to the interval. |
| 43 | * @property-read float $totalMinutes Number of minutes equivalent to the interval. |
| 44 | * @property-read float $totalSeconds Number of seconds equivalent to the interval. |
| 45 | * |
| 46 | * @method static CarbonInterval years($years = 1) Create instance specifying a number of years. |
| 47 | * @method static CarbonInterval year($years = 1) Alias for years() |
| 48 | * @method static CarbonInterval months($months = 1) Create instance specifying a number of months. |
| 49 | * @method static CarbonInterval month($months = 1) Alias for months() |
| 50 | * @method static CarbonInterval weeks($weeks = 1) Create instance specifying a number of weeks. |
| 51 | * @method static CarbonInterval week($weeks = 1) Alias for weeks() |
| 52 | * @method static CarbonInterval days($days = 1) Create instance specifying a number of days. |
| 53 | * @method static CarbonInterval dayz($days = 1) Alias for days() |
| 54 | * @method static CarbonInterval day($days = 1) Alias for days() |
| 55 | * @method static CarbonInterval hours($hours = 1) Create instance specifying a number of hours. |
| 56 | * @method static CarbonInterval hour($hours = 1) Alias for hours() |
| 57 | * @method static CarbonInterval minutes($minutes = 1) Create instance specifying a number of minutes. |
| 58 | * @method static CarbonInterval minute($minutes = 1) Alias for minutes() |
| 59 | * @method static CarbonInterval seconds($seconds = 1) Create instance specifying a number of seconds. |
| 60 | * @method static CarbonInterval second($seconds = 1) Alias for seconds() |
| 61 | * @method CarbonInterval years($years = 1) Set the years portion of the current interval. |
| 62 | * @method CarbonInterval year($years = 1) Alias for years(). |
| 63 | * @method CarbonInterval months($months = 1) Set the months portion of the current interval. |
| 64 | * @method CarbonInterval month($months = 1) Alias for months(). |
| 65 | * @method CarbonInterval weeks($weeks = 1) Set the weeks portion of the current interval. Will overwrite dayz value. |
| 66 | * @method CarbonInterval week($weeks = 1) Alias for weeks(). |
| 67 | * @method CarbonInterval days($days = 1) Set the days portion of the current interval. |
| 68 | * @method CarbonInterval dayz($days = 1) Alias for days(). |
| 69 | * @method CarbonInterval day($days = 1) Alias for days(). |
| 70 | * @method CarbonInterval hours($hours = 1) Set the hours portion of the current interval. |
| 71 | * @method CarbonInterval hour($hours = 1) Alias for hours(). |
| 72 | * @method CarbonInterval minutes($minutes = 1) Set the minutes portion of the current interval. |
| 73 | * @method CarbonInterval minute($minutes = 1) Alias for minutes(). |
| 74 | * @method CarbonInterval seconds($seconds = 1) Set the seconds portion of the current interval. |
| 75 | * @method CarbonInterval second($seconds = 1) Alias for seconds(). |
| 76 | */ |
| 77 | class CarbonInterval extends \DateInterval |
| 78 | { |
| 79 | /** |
| 80 | * Interval spec period designators |
| 81 | */ |
| 82 | const PERIOD_PREFIX = 'P'; |
| 83 | const PERIOD_YEARS = 'Y'; |
| 84 | const PERIOD_MONTHS = 'M'; |
| 85 | const PERIOD_DAYS = 'D'; |
| 86 | const PERIOD_TIME_PREFIX = 'T'; |
| 87 | const PERIOD_HOURS = 'H'; |
| 88 | const PERIOD_MINUTES = 'M'; |
| 89 | const PERIOD_SECONDS = 'S'; |
| 90 | /** |
| 91 | * A translator to ... er ... translate stuff |
| 92 | * |
| 93 | * @var \Symfony\Component\Translation\TranslatorInterface |
| 94 | */ |
| 95 | protected static $translator; |
| 96 | /** |
| 97 | * @var array|null |
| 98 | */ |
| 99 | protected static $cascadeFactors; |
| 100 | /** |
| 101 | * @var array|null |
| 102 | */ |
| 103 | private static $flipCascadeFactors; |
| 104 | /** |
| 105 | * The registered macros. |
| 106 | * |
| 107 | * @var array |
| 108 | */ |
| 109 | protected static $macros = array(); |
| 110 | /** |
| 111 | * Before PHP 5.4.20/5.5.4 instead of FALSE days will be set to -99999 when the interval instance |
| 112 | * was created by DateTime::diff(). |
| 113 | */ |
| 114 | const PHP_DAYS_FALSE = -99999; |
| 115 | /** |
| 116 | * Mapping of units and factors for cascading. |
| 117 | * |
| 118 | * Should only be modified by changing the factors or referenced constants. |
| 119 | * |
| 120 | * @return array |
| 121 | */ |
| 122 | public static function getCascadeFactors() |
| 123 | { |
| 124 | return static::$cascadeFactors ?: array('minutes' => array(\MailPoetVendor\Carbon\Carbon::SECONDS_PER_MINUTE, 'seconds'), 'hours' => array(\MailPoetVendor\Carbon\Carbon::MINUTES_PER_HOUR, 'minutes'), 'dayz' => array(\MailPoetVendor\Carbon\Carbon::HOURS_PER_DAY, 'hours'), 'months' => array(\MailPoetVendor\Carbon\Carbon::DAYS_PER_WEEK * \MailPoetVendor\Carbon\Carbon::WEEKS_PER_MONTH, 'dayz'), 'years' => array(\MailPoetVendor\Carbon\Carbon::MONTHS_PER_YEAR, 'months')); |
| 125 | } |
| 126 | private static function standardizeUnit($unit) |
| 127 | { |
| 128 | $unit = \rtrim($unit, 'sz') . 's'; |
| 129 | return $unit === 'days' ? 'dayz' : $unit; |
| 130 | } |
| 131 | private static function getFlipCascadeFactors() |
| 132 | { |
| 133 | if (!self::$flipCascadeFactors) { |
| 134 | self::$flipCascadeFactors = array(); |
| 135 | foreach (static::getCascadeFactors() as $to => $tuple) { |
| 136 | list($factor, $from) = $tuple; |
| 137 | self::$flipCascadeFactors[self::standardizeUnit($from)] = array(self::standardizeUnit($to), $factor); |
| 138 | } |
| 139 | } |
| 140 | return self::$flipCascadeFactors; |
| 141 | } |
| 142 | /** |
| 143 | * @param array $cascadeFactors |
| 144 | */ |
| 145 | public static function setCascadeFactors(array $cascadeFactors) |
| 146 | { |
| 147 | self::$flipCascadeFactors = null; |
| 148 | static::$cascadeFactors = $cascadeFactors; |
| 149 | } |
| 150 | /** |
| 151 | * Determine if the interval was created via DateTime:diff() or not. |
| 152 | * |
| 153 | * @param DateInterval $interval |
| 154 | * |
| 155 | * @return bool |
| 156 | */ |
| 157 | private static function wasCreatedFromDiff(\DateInterval $interval) |
| 158 | { |
| 159 | return $interval->days !== \false && $interval->days !== static::PHP_DAYS_FALSE; |
| 160 | } |
| 161 | /////////////////////////////////////////////////////////////////// |
| 162 | //////////////////////////// CONSTRUCTORS ///////////////////////// |
| 163 | /////////////////////////////////////////////////////////////////// |
| 164 | /** |
| 165 | * Create a new CarbonInterval instance. |
| 166 | * |
| 167 | * @param int $years |
| 168 | * @param int $months |
| 169 | * @param int $weeks |
| 170 | * @param int $days |
| 171 | * @param int $hours |
| 172 | * @param int $minutes |
| 173 | * @param int $seconds |
| 174 | */ |
| 175 | public function __construct($years = 1, $months = null, $weeks = null, $days = null, $hours = null, $minutes = null, $seconds = null) |
| 176 | { |
| 177 | $spec = $years; |
| 178 | if (!\is_string($spec) || \floatval($years) || \preg_match('/^[0-9.]/', $years)) { |
| 179 | $spec = static::PERIOD_PREFIX; |
| 180 | $spec .= $years > 0 ? $years . static::PERIOD_YEARS : ''; |
| 181 | $spec .= $months > 0 ? $months . static::PERIOD_MONTHS : ''; |
| 182 | $specDays = 0; |
| 183 | $specDays += $weeks > 0 ? $weeks * static::getDaysPerWeek() : 0; |
| 184 | $specDays += $days > 0 ? $days : 0; |
| 185 | $spec .= $specDays > 0 ? $specDays . static::PERIOD_DAYS : ''; |
| 186 | if ($hours > 0 || $minutes > 0 || $seconds > 0) { |
| 187 | $spec .= static::PERIOD_TIME_PREFIX; |
| 188 | $spec .= $hours > 0 ? $hours . static::PERIOD_HOURS : ''; |
| 189 | $spec .= $minutes > 0 ? $minutes . static::PERIOD_MINUTES : ''; |
| 190 | $spec .= $seconds > 0 ? $seconds . static::PERIOD_SECONDS : ''; |
| 191 | } |
| 192 | if ($spec === static::PERIOD_PREFIX) { |
| 193 | // Allow the zero interval. |
| 194 | $spec .= '0' . static::PERIOD_YEARS; |
| 195 | } |
| 196 | } |
| 197 | parent::__construct($spec); |
| 198 | } |
| 199 | /** |
| 200 | * Returns the factor for a given source-to-target couple. |
| 201 | * |
| 202 | * @param string $source |
| 203 | * @param string $target |
| 204 | * |
| 205 | * @return int|null |
| 206 | */ |
| 207 | public static function getFactor($source, $target) |
| 208 | { |
| 209 | $source = self::standardizeUnit($source); |
| 210 | $target = self::standardizeUnit($target); |
| 211 | $factors = static::getFlipCascadeFactors(); |
| 212 | if (isset($factors[$source])) { |
| 213 | list($to, $factor) = $factors[$source]; |
| 214 | if ($to === $target) { |
| 215 | return $factor; |
| 216 | } |
| 217 | } |
| 218 | return null; |
| 219 | } |
| 220 | /** |
| 221 | * Returns current config for days per week. |
| 222 | * |
| 223 | * @return int |
| 224 | */ |
| 225 | public static function getDaysPerWeek() |
| 226 | { |
| 227 | return static::getFactor('dayz', 'weeks') ?: \MailPoetVendor\Carbon\Carbon::DAYS_PER_WEEK; |
| 228 | } |
| 229 | /** |
| 230 | * Returns current config for hours per day. |
| 231 | * |
| 232 | * @return int |
| 233 | */ |
| 234 | public static function getHoursPerDay() |
| 235 | { |
| 236 | return static::getFactor('hours', 'dayz') ?: \MailPoetVendor\Carbon\Carbon::HOURS_PER_DAY; |
| 237 | } |
| 238 | /** |
| 239 | * Returns current config for minutes per hour. |
| 240 | * |
| 241 | * @return int |
| 242 | */ |
| 243 | public static function getMinutesPerHours() |
| 244 | { |
| 245 | return static::getFactor('minutes', 'hours') ?: \MailPoetVendor\Carbon\Carbon::MINUTES_PER_HOUR; |
| 246 | } |
| 247 | /** |
| 248 | * Returns current config for seconds per minute. |
| 249 | * |
| 250 | * @return int |
| 251 | */ |
| 252 | public static function getSecondsPerMinutes() |
| 253 | { |
| 254 | return static::getFactor('seconds', 'minutes') ?: \MailPoetVendor\Carbon\Carbon::SECONDS_PER_MINUTE; |
| 255 | } |
| 256 | /** |
| 257 | * Create a new CarbonInterval instance from specific values. |
| 258 | * This is an alias for the constructor that allows better fluent |
| 259 | * syntax as it allows you to do CarbonInterval::create(1)->fn() rather than |
| 260 | * (new CarbonInterval(1))->fn(). |
| 261 | * |
| 262 | * @param int $years |
| 263 | * @param int $months |
| 264 | * @param int $weeks |
| 265 | * @param int $days |
| 266 | * @param int $hours |
| 267 | * @param int $minutes |
| 268 | * @param int $seconds |
| 269 | * |
| 270 | * @return static |
| 271 | */ |
| 272 | public static function create($years = 1, $months = null, $weeks = null, $days = null, $hours = null, $minutes = null, $seconds = null) |
| 273 | { |
| 274 | return new static($years, $months, $weeks, $days, $hours, $minutes, $seconds); |
| 275 | } |
| 276 | /** |
| 277 | * Get a copy of the instance. |
| 278 | * |
| 279 | * @return static |
| 280 | */ |
| 281 | public function copy() |
| 282 | { |
| 283 | $date = new static($this->spec()); |
| 284 | $date->invert = $this->invert; |
| 285 | return $date; |
| 286 | } |
| 287 | /** |
| 288 | * Provide static helpers to create instances. Allows CarbonInterval::years(3). |
| 289 | * |
| 290 | * Note: This is done using the magic method to allow static and instance methods to |
| 291 | * have the same names. |
| 292 | * |
| 293 | * @param string $name |
| 294 | * @param array $args |
| 295 | * |
| 296 | * @return static |
| 297 | */ |
| 298 | public static function __callStatic($name, $args) |
| 299 | { |
| 300 | $arg = \count($args) === 0 ? 1 : $args[0]; |
| 301 | switch ($name) { |
| 302 | case 'years': |
| 303 | case 'year': |
| 304 | return new static($arg); |
| 305 | case 'months': |
| 306 | case 'month': |
| 307 | return new static(null, $arg); |
| 308 | case 'weeks': |
| 309 | case 'week': |
| 310 | return new static(null, null, $arg); |
| 311 | case 'days': |
| 312 | case 'dayz': |
| 313 | case 'day': |
| 314 | return new static(null, null, null, $arg); |
| 315 | case 'hours': |
| 316 | case 'hour': |
| 317 | return new static(null, null, null, null, $arg); |
| 318 | case 'minutes': |
| 319 | case 'minute': |
| 320 | return new static(null, null, null, null, null, $arg); |
| 321 | case 'seconds': |
| 322 | case 'second': |
| 323 | return new static(null, null, null, null, null, null, $arg); |
| 324 | } |
| 325 | if (static::hasMacro($name)) { |
| 326 | return \call_user_func_array(array(new static(0), $name), $args); |
| 327 | } |
| 328 | } |
| 329 | /** |
| 330 | * Creates a CarbonInterval from string. |
| 331 | * |
| 332 | * Format: |
| 333 | * |
| 334 | * Suffix | Unit | Example | DateInterval expression |
| 335 | * -------|---------|---------|------------------------ |
| 336 | * y | years | 1y | P1Y |
| 337 | * mo | months | 3mo | P3M |
| 338 | * w | weeks | 2w | P2W |
| 339 | * d | days | 28d | P28D |
| 340 | * h | hours | 4h | PT4H |
| 341 | * m | minutes | 12m | PT12M |
| 342 | * s | seconds | 59s | PT59S |
| 343 | * |
| 344 | * e. g. `1w 3d 4h 32m 23s` is converted to 10 days 4 hours 32 minutes and 23 seconds. |
| 345 | * |
| 346 | * Special cases: |
| 347 | * - An empty string will return a zero interval |
| 348 | * - Fractions are allowed for weeks, days, hours and minutes and will be converted |
| 349 | * and rounded to the next smaller value (caution: 0.5w = 4d) |
| 350 | * |
| 351 | * @param string $intervalDefinition |
| 352 | * |
| 353 | * @return static |
| 354 | */ |
| 355 | public static function fromString($intervalDefinition) |
| 356 | { |
| 357 | if (empty($intervalDefinition)) { |
| 358 | return new static(0); |
| 359 | } |
| 360 | $years = 0; |
| 361 | $months = 0; |
| 362 | $weeks = 0; |
| 363 | $days = 0; |
| 364 | $hours = 0; |
| 365 | $minutes = 0; |
| 366 | $seconds = 0; |
| 367 | $pattern = '/(\\d+(?:\\.\\d+)?)\\h*([^\\d\\h]*)/i'; |
| 368 | \preg_match_all($pattern, $intervalDefinition, $parts, \PREG_SET_ORDER); |
| 369 | while ($match = \array_shift($parts)) { |
| 370 | list($part, $value, $unit) = $match; |
| 371 | $intValue = \intval($value); |
| 372 | $fraction = \floatval($value) - $intValue; |
| 373 | switch (\strtolower($unit)) { |
| 374 | case 'year': |
| 375 | case 'years': |
| 376 | case 'y': |
| 377 | $years += $intValue; |
| 378 | break; |
| 379 | case 'month': |
| 380 | case 'months': |
| 381 | case 'mo': |
| 382 | $months += $intValue; |
| 383 | break; |
| 384 | case 'week': |
| 385 | case 'weeks': |
| 386 | case 'w': |
| 387 | $weeks += $intValue; |
| 388 | if ($fraction) { |
| 389 | $parts[] = array(null, $fraction * static::getDaysPerWeek(), 'd'); |
| 390 | } |
| 391 | break; |
| 392 | case 'day': |
| 393 | case 'days': |
| 394 | case 'd': |
| 395 | $days += $intValue; |
| 396 | if ($fraction) { |
| 397 | $parts[] = array(null, $fraction * static::getHoursPerDay(), 'h'); |
| 398 | } |
| 399 | break; |
| 400 | case 'hour': |
| 401 | case 'hours': |
| 402 | case 'h': |
| 403 | $hours += $intValue; |
| 404 | if ($fraction) { |
| 405 | $parts[] = array(null, $fraction * static::getMinutesPerHours(), 'm'); |
| 406 | } |
| 407 | break; |
| 408 | case 'minute': |
| 409 | case 'minutes': |
| 410 | case 'm': |
| 411 | $minutes += $intValue; |
| 412 | if ($fraction) { |
| 413 | $seconds += \round($fraction * static::getSecondsPerMinutes()); |
| 414 | } |
| 415 | break; |
| 416 | case 'second': |
| 417 | case 'seconds': |
| 418 | case 's': |
| 419 | $seconds += $intValue; |
| 420 | break; |
| 421 | default: |
| 422 | throw new \InvalidArgumentException(\sprintf('Invalid part %s in definition %s', $part, $intervalDefinition)); |
| 423 | } |
| 424 | } |
| 425 | return new static($years, $months, $weeks, $days, $hours, $minutes, $seconds); |
| 426 | } |
| 427 | /** |
| 428 | * Create a CarbonInterval instance from a DateInterval one. Can not instance |
| 429 | * DateInterval objects created from DateTime::diff() as you can't externally |
| 430 | * set the $days field. |
| 431 | * |
| 432 | * Pass false as second argument to get a microseconds-precise interval. Else |
| 433 | * microseconds in the original interval will not be kept. |
| 434 | * |
| 435 | * @param DateInterval $di |
| 436 | * @param bool $trimMicroseconds (true by default) |
| 437 | * |
| 438 | * @return static |
| 439 | */ |
| 440 | public static function instance(\DateInterval $di, $trimMicroseconds = \true) |
| 441 | { |
| 442 | $microseconds = $trimMicroseconds || \version_compare(\PHP_VERSION, '7.1.0-dev', '<') ? 0 : $di->f; |
| 443 | $instance = new static(static::getDateIntervalSpec($di)); |
| 444 | if ($microseconds) { |
| 445 | $instance->f = $microseconds; |
| 446 | } |
| 447 | $instance->invert = $di->invert; |
| 448 | foreach (array('y', 'm', 'd', 'h', 'i', 's') as $unit) { |
| 449 | if ($di->{$unit} < 0) { |
| 450 | $instance->{$unit} *= -1; |
| 451 | } |
| 452 | } |
| 453 | return $instance; |
| 454 | } |
| 455 | /** |
| 456 | * Make a CarbonInterval instance from given variable if possible. |
| 457 | * |
| 458 | * Always return a new instance. Parse only strings and only these likely to be intervals (skip dates |
| 459 | * and recurrences). Throw an exception for invalid format, but otherwise return null. |
| 460 | * |
| 461 | * @param mixed $var |
| 462 | * |
| 463 | * @return static|null |
| 464 | */ |
| 465 | public static function make($var) |
| 466 | { |
| 467 | if ($var instanceof \DateInterval) { |
| 468 | return static::instance($var); |
| 469 | } |
| 470 | if (\is_string($var)) { |
| 471 | $var = \trim($var); |
| 472 | if (\substr($var, 0, 1) === 'P') { |
| 473 | return new static($var); |
| 474 | } |
| 475 | if (\preg_match('/^(?:\\h*\\d+(?:\\.\\d+)?\\h*[a-z]+)+$/i', $var)) { |
| 476 | return static::fromString($var); |
| 477 | } |
| 478 | } |
| 479 | } |
| 480 | /////////////////////////////////////////////////////////////////// |
| 481 | /////////////////////// LOCALIZATION ////////////////////////////// |
| 482 | /////////////////////////////////////////////////////////////////// |
| 483 | /** |
| 484 | * Initialize the translator instance if necessary. |
| 485 | * |
| 486 | * @return \Symfony\Component\Translation\TranslatorInterface |
| 487 | */ |
| 488 | protected static function translator() |
| 489 | { |
| 490 | if (static::$translator === null) { |
| 491 | static::$translator = \MailPoetVendor\Carbon\Translator::get(); |
| 492 | } |
| 493 | return static::$translator; |
| 494 | } |
| 495 | /** |
| 496 | * Get the translator instance in use. |
| 497 | * |
| 498 | * @return \Symfony\Component\Translation\TranslatorInterface |
| 499 | */ |
| 500 | public static function getTranslator() |
| 501 | { |
| 502 | return static::translator(); |
| 503 | } |
| 504 | /** |
| 505 | * Set the translator instance to use. |
| 506 | * |
| 507 | * @param TranslatorInterface $translator |
| 508 | */ |
| 509 | public static function setTranslator(\MailPoetVendor\Symfony\Component\Translation\TranslatorInterface $translator) |
| 510 | { |
| 511 | static::$translator = $translator; |
| 512 | } |
| 513 | /** |
| 514 | * Get the current translator locale. |
| 515 | * |
| 516 | * @return string |
| 517 | */ |
| 518 | public static function getLocale() |
| 519 | { |
| 520 | return static::translator()->getLocale(); |
| 521 | } |
| 522 | /** |
| 523 | * Set the current translator locale. |
| 524 | * |
| 525 | * @param string $locale |
| 526 | */ |
| 527 | public static function setLocale($locale) |
| 528 | { |
| 529 | return static::translator()->setLocale($locale) !== \false; |
| 530 | } |
| 531 | /////////////////////////////////////////////////////////////////// |
| 532 | ///////////////////////// GETTERS AND SETTERS ///////////////////// |
| 533 | /////////////////////////////////////////////////////////////////// |
| 534 | /** |
| 535 | * Get a part of the CarbonInterval object. |
| 536 | * |
| 537 | * @param string $name |
| 538 | * |
| 539 | * @throws \InvalidArgumentException |
| 540 | * |
| 541 | * @return int|float |
| 542 | */ |
| 543 | public function __get($name) |
| 544 | { |
| 545 | if (\substr($name, 0, 5) === 'total') { |
| 546 | return $this->total(\substr($name, 5)); |
| 547 | } |
| 548 | switch ($name) { |
| 549 | case 'years': |
| 550 | return $this->y; |
| 551 | case 'months': |
| 552 | return $this->m; |
| 553 | case 'dayz': |
| 554 | return $this->d; |
| 555 | case 'hours': |
| 556 | return $this->h; |
| 557 | case 'minutes': |
| 558 | return $this->i; |
| 559 | case 'seconds': |
| 560 | return $this->s; |
| 561 | case 'weeks': |
| 562 | return (int) \floor($this->d / static::getDaysPerWeek()); |
| 563 | case 'daysExcludeWeeks': |
| 564 | case 'dayzExcludeWeeks': |
| 565 | return $this->d % static::getDaysPerWeek(); |
| 566 | default: |
| 567 | throw new \InvalidArgumentException(\sprintf("Unknown getter '%s'", $name)); |
| 568 | } |
| 569 | } |
| 570 | /** |
| 571 | * Set a part of the CarbonInterval object. |
| 572 | * |
| 573 | * @param string $name |
| 574 | * @param int $val |
| 575 | * |
| 576 | * @throws \InvalidArgumentException |
| 577 | */ |
| 578 | public function __set($name, $val) |
| 579 | { |
| 580 | switch ($name) { |
| 581 | case 'years': |
| 582 | $this->y = $val; |
| 583 | break; |
| 584 | case 'months': |
| 585 | $this->m = $val; |
| 586 | break; |
| 587 | case 'weeks': |
| 588 | $this->d = $val * static::getDaysPerWeek(); |
| 589 | break; |
| 590 | case 'dayz': |
| 591 | $this->d = $val; |
| 592 | break; |
| 593 | case 'hours': |
| 594 | $this->h = $val; |
| 595 | break; |
| 596 | case 'minutes': |
| 597 | $this->i = $val; |
| 598 | break; |
| 599 | case 'seconds': |
| 600 | $this->s = $val; |
| 601 | break; |
| 602 | } |
| 603 | } |
| 604 | /** |
| 605 | * Allow setting of weeks and days to be cumulative. |
| 606 | * |
| 607 | * @param int $weeks Number of weeks to set |
| 608 | * @param int $days Number of days to set |
| 609 | * |
| 610 | * @return static |
| 611 | */ |
| 612 | public function weeksAndDays($weeks, $days) |
| 613 | { |
| 614 | $this->dayz = $weeks * static::getDaysPerWeek() + $days; |
| 615 | return $this; |
| 616 | } |
| 617 | /** |
| 618 | * Register a custom macro. |
| 619 | * |
| 620 | * @param string $name |
| 621 | * @param object|callable $macro |
| 622 | * |
| 623 | * @return void |
| 624 | */ |
| 625 | public static function macro($name, $macro) |
| 626 | { |
| 627 | static::$macros[$name] = $macro; |
| 628 | } |
| 629 | /** |
| 630 | * Remove all macros. |
| 631 | */ |
| 632 | public static function resetMacros() |
| 633 | { |
| 634 | static::$macros = array(); |
| 635 | } |
| 636 | /** |
| 637 | * Register macros from a mixin object. |
| 638 | * |
| 639 | * @param object $mixin |
| 640 | * |
| 641 | * @throws \ReflectionException |
| 642 | * |
| 643 | * @return void |
| 644 | */ |
| 645 | public static function mixin($mixin) |
| 646 | { |
| 647 | $reflection = new \ReflectionClass($mixin); |
| 648 | $methods = $reflection->getMethods(\ReflectionMethod::IS_PUBLIC | \ReflectionMethod::IS_PROTECTED); |
| 649 | foreach ($methods as $method) { |
| 650 | $method->setAccessible(\true); |
| 651 | static::macro($method->name, $method->invoke($mixin)); |
| 652 | } |
| 653 | } |
| 654 | /** |
| 655 | * Check if macro is registered. |
| 656 | * |
| 657 | * @param string $name |
| 658 | * |
| 659 | * @return bool |
| 660 | */ |
| 661 | public static function hasMacro($name) |
| 662 | { |
| 663 | return isset(static::$macros[$name]); |
| 664 | } |
| 665 | /** |
| 666 | * Call given macro. |
| 667 | * |
| 668 | * @param string $name |
| 669 | * @param array $parameters |
| 670 | * |
| 671 | * @return mixed |
| 672 | */ |
| 673 | protected function callMacro($name, $parameters) |
| 674 | { |
| 675 | $macro = static::$macros[$name]; |
| 676 | $reflection = new \ReflectionFunction($macro); |
| 677 | $reflectionParameters = $reflection->getParameters(); |
| 678 | $expectedCount = \count($reflectionParameters); |
| 679 | $actualCount = \count($parameters); |
| 680 | if ($expectedCount > $actualCount && $reflectionParameters[$expectedCount - 1]->name === 'self') { |
| 681 | for ($i = $actualCount; $i < $expectedCount - 1; $i++) { |
| 682 | $parameters[] = $reflectionParameters[$i]->getDefaultValue(); |
| 683 | } |
| 684 | $parameters[] = $this; |
| 685 | } |
| 686 | if ($macro instanceof \Closure && \method_exists($macro, 'bindTo')) { |
| 687 | $macro = $macro->bindTo($this, \get_class($this)); |
| 688 | } |
| 689 | return \call_user_func_array($macro, $parameters); |
| 690 | } |
| 691 | /** |
| 692 | * Allow fluent calls on the setters... CarbonInterval::years(3)->months(5)->day(). |
| 693 | * |
| 694 | * Note: This is done using the magic method to allow static and instance methods to |
| 695 | * have the same names. |
| 696 | * |
| 697 | * @param string $name |
| 698 | * @param array $args |
| 699 | * |
| 700 | * @return static |
| 701 | */ |
| 702 | public function __call($name, $args) |
| 703 | { |
| 704 | if (static::hasMacro($name)) { |
| 705 | return $this->callMacro($name, $args); |
| 706 | } |
| 707 | $arg = \count($args) === 0 ? 1 : $args[0]; |
| 708 | switch ($name) { |
| 709 | case 'years': |
| 710 | case 'year': |
| 711 | $this->years = $arg; |
| 712 | break; |
| 713 | case 'months': |
| 714 | case 'month': |
| 715 | $this->months = $arg; |
| 716 | break; |
| 717 | case 'weeks': |
| 718 | case 'week': |
| 719 | $this->dayz = $arg * static::getDaysPerWeek(); |
| 720 | break; |
| 721 | case 'days': |
| 722 | case 'dayz': |
| 723 | case 'day': |
| 724 | $this->dayz = $arg; |
| 725 | break; |
| 726 | case 'hours': |
| 727 | case 'hour': |
| 728 | $this->hours = $arg; |
| 729 | break; |
| 730 | case 'minutes': |
| 731 | case 'minute': |
| 732 | $this->minutes = $arg; |
| 733 | break; |
| 734 | case 'seconds': |
| 735 | case 'second': |
| 736 | $this->seconds = $arg; |
| 737 | break; |
| 738 | } |
| 739 | return $this; |
| 740 | } |
| 741 | /** |
| 742 | * Get the current interval in a human readable format in the current locale. |
| 743 | * |
| 744 | * @param bool $short (false by default), returns short units if true |
| 745 | * |
| 746 | * @return string |
| 747 | */ |
| 748 | public function forHumans($short = \false) |
| 749 | { |
| 750 | $periods = array('year' => array('y', $this->years), 'month' => array('m', $this->months), 'week' => array('w', $this->weeks), 'day' => array('d', $this->daysExcludeWeeks), 'hour' => array('h', $this->hours), 'minute' => array('min', $this->minutes), 'second' => array('s', $this->seconds)); |
| 751 | $parts = array(); |
| 752 | foreach ($periods as $unit => $options) { |
| 753 | list($shortUnit, $count) = $options; |
| 754 | if ($count > 0) { |
| 755 | $parts[] = static::translator()->transChoice($short ? $shortUnit : $unit, $count, array(':count' => $count)); |
| 756 | } |
| 757 | } |
| 758 | return \implode(' ', $parts); |
| 759 | } |
| 760 | /** |
| 761 | * Format the instance as a string using the forHumans() function. |
| 762 | * |
| 763 | * @return string |
| 764 | */ |
| 765 | public function __toString() |
| 766 | { |
| 767 | return $this->forHumans(); |
| 768 | } |
| 769 | /** |
| 770 | * Convert the interval to a CarbonPeriod. |
| 771 | * |
| 772 | * @return CarbonPeriod |
| 773 | */ |
| 774 | public function toPeriod() |
| 775 | { |
| 776 | return \MailPoetVendor\Carbon\CarbonPeriod::createFromArray(\array_merge(array($this), \func_get_args())); |
| 777 | } |
| 778 | /** |
| 779 | * Invert the interval. |
| 780 | * |
| 781 | * @return $this |
| 782 | */ |
| 783 | public function invert() |
| 784 | { |
| 785 | $this->invert = $this->invert ? 0 : 1; |
| 786 | return $this; |
| 787 | } |
| 788 | /** |
| 789 | * Add the passed interval to the current instance. |
| 790 | * |
| 791 | * @param DateInterval $interval |
| 792 | * |
| 793 | * @return static |
| 794 | */ |
| 795 | public function add(\DateInterval $interval) |
| 796 | { |
| 797 | $sign = ($this->invert === 1) !== ($interval->invert === 1) ? -1 : 1; |
| 798 | if (static::wasCreatedFromDiff($interval)) { |
| 799 | $this->dayz += $interval->days * $sign; |
| 800 | } else { |
| 801 | $this->years += $interval->y * $sign; |
| 802 | $this->months += $interval->m * $sign; |
| 803 | $this->dayz += $interval->d * $sign; |
| 804 | $this->hours += $interval->h * $sign; |
| 805 | $this->minutes += $interval->i * $sign; |
| 806 | $this->seconds += $interval->s * $sign; |
| 807 | } |
| 808 | if (($this->years || $this->months || $this->dayz || $this->hours || $this->minutes || $this->seconds) && $this->years <= 0 && $this->months <= 0 && $this->dayz <= 0 && $this->hours <= 0 && $this->minutes <= 0 && $this->seconds <= 0) { |
| 809 | $this->years *= -1; |
| 810 | $this->months *= -1; |
| 811 | $this->dayz *= -1; |
| 812 | $this->hours *= -1; |
| 813 | $this->minutes *= -1; |
| 814 | $this->seconds *= -1; |
| 815 | $this->invert(); |
| 816 | } |
| 817 | return $this; |
| 818 | } |
| 819 | /** |
| 820 | * Multiply current instance given number of times |
| 821 | * |
| 822 | * @param float $factor |
| 823 | * |
| 824 | * @return $this |
| 825 | */ |
| 826 | public function times($factor) |
| 827 | { |
| 828 | if ($factor < 0) { |
| 829 | $this->invert = $this->invert ? 0 : 1; |
| 830 | $factor = -$factor; |
| 831 | } |
| 832 | $this->years = (int) \round($this->years * $factor); |
| 833 | $this->months = (int) \round($this->months * $factor); |
| 834 | $this->dayz = (int) \round($this->dayz * $factor); |
| 835 | $this->hours = (int) \round($this->hours * $factor); |
| 836 | $this->minutes = (int) \round($this->minutes * $factor); |
| 837 | $this->seconds = (int) \round($this->seconds * $factor); |
| 838 | return $this; |
| 839 | } |
| 840 | /** |
| 841 | * Get the interval_spec string of a date interval. |
| 842 | * |
| 843 | * @param DateInterval $interval |
| 844 | * |
| 845 | * @return string |
| 846 | */ |
| 847 | public static function getDateIntervalSpec(\DateInterval $interval) |
| 848 | { |
| 849 | $date = \array_filter(array(static::PERIOD_YEARS => \abs($interval->y), static::PERIOD_MONTHS => \abs($interval->m), static::PERIOD_DAYS => \abs($interval->d))); |
| 850 | $time = \array_filter(array(static::PERIOD_HOURS => \abs($interval->h), static::PERIOD_MINUTES => \abs($interval->i), static::PERIOD_SECONDS => \abs($interval->s))); |
| 851 | $specString = static::PERIOD_PREFIX; |
| 852 | foreach ($date as $key => $value) { |
| 853 | $specString .= $value . $key; |
| 854 | } |
| 855 | if (\count($time) > 0) { |
| 856 | $specString .= static::PERIOD_TIME_PREFIX; |
| 857 | foreach ($time as $key => $value) { |
| 858 | $specString .= $value . $key; |
| 859 | } |
| 860 | } |
| 861 | return $specString === static::PERIOD_PREFIX ? 'PT0S' : $specString; |
| 862 | } |
| 863 | /** |
| 864 | * Get the interval_spec string. |
| 865 | * |
| 866 | * @return string |
| 867 | */ |
| 868 | public function spec() |
| 869 | { |
| 870 | return static::getDateIntervalSpec($this); |
| 871 | } |
| 872 | /** |
| 873 | * Comparing 2 date intervals. |
| 874 | * |
| 875 | * @param DateInterval $a |
| 876 | * @param DateInterval $b |
| 877 | * |
| 878 | * @return int |
| 879 | */ |
| 880 | public static function compareDateIntervals(\DateInterval $a, \DateInterval $b) |
| 881 | { |
| 882 | $current = \MailPoetVendor\Carbon\Carbon::now(); |
| 883 | $passed = $current->copy()->add($b); |
| 884 | $current->add($a); |
| 885 | if ($current < $passed) { |
| 886 | return -1; |
| 887 | } |
| 888 | if ($current > $passed) { |
| 889 | return 1; |
| 890 | } |
| 891 | return 0; |
| 892 | } |
| 893 | /** |
| 894 | * Comparing with passed interval. |
| 895 | * |
| 896 | * @param DateInterval $interval |
| 897 | * |
| 898 | * @return int |
| 899 | */ |
| 900 | public function compare(\DateInterval $interval) |
| 901 | { |
| 902 | return static::compareDateIntervals($this, $interval); |
| 903 | } |
| 904 | /** |
| 905 | * Convert overflowed values into bigger units. |
| 906 | * |
| 907 | * @return $this |
| 908 | */ |
| 909 | public function cascade() |
| 910 | { |
| 911 | foreach (static::getFlipCascadeFactors() as $source => $cascade) { |
| 912 | list($target, $factor) = $cascade; |
| 913 | if ($source === 'dayz' && $target === 'weeks') { |
| 914 | continue; |
| 915 | } |
| 916 | $value = $this->{$source}; |
| 917 | $this->{$source} = $modulo = $value % $factor; |
| 918 | $this->{$target} += ($value - $modulo) / $factor; |
| 919 | } |
| 920 | return $this; |
| 921 | } |
| 922 | /** |
| 923 | * Get amount of given unit equivalent to the interval. |
| 924 | * |
| 925 | * @param string $unit |
| 926 | * |
| 927 | * @throws \InvalidArgumentException |
| 928 | * |
| 929 | * @return float |
| 930 | */ |
| 931 | public function total($unit) |
| 932 | { |
| 933 | $realUnit = $unit = \strtolower($unit); |
| 934 | if (\in_array($unit, array('days', 'weeks'))) { |
| 935 | $realUnit = 'dayz'; |
| 936 | } elseif (!\in_array($unit, array('seconds', 'minutes', 'hours', 'dayz', 'months', 'years'))) { |
| 937 | throw new \InvalidArgumentException("Unknown unit '{$unit}'."); |
| 938 | } |
| 939 | $result = 0; |
| 940 | $cumulativeFactor = 0; |
| 941 | $unitFound = \false; |
| 942 | foreach (static::getFlipCascadeFactors() as $source => $cascade) { |
| 943 | list($target, $factor) = $cascade; |
| 944 | if ($source === $realUnit) { |
| 945 | $unitFound = \true; |
| 946 | $result += $this->{$source}; |
| 947 | $cumulativeFactor = 1; |
| 948 | } |
| 949 | if ($factor === \false) { |
| 950 | if ($unitFound) { |
| 951 | break; |
| 952 | } |
| 953 | $result = 0; |
| 954 | $cumulativeFactor = 0; |
| 955 | continue; |
| 956 | } |
| 957 | if ($target === $realUnit) { |
| 958 | $unitFound = \true; |
| 959 | } |
| 960 | if ($cumulativeFactor) { |
| 961 | $cumulativeFactor *= $factor; |
| 962 | $result += $this->{$target} * $cumulativeFactor; |
| 963 | continue; |
| 964 | } |
| 965 | $result = ($result + $this->{$source}) / $factor; |
| 966 | } |
| 967 | if (isset($target) && !$cumulativeFactor) { |
| 968 | $result += $this->{$target}; |
| 969 | } |
| 970 | if (!$unitFound) { |
| 971 | throw new \InvalidArgumentException("Unit {$unit} have no configuration to get total from other units."); |
| 972 | } |
| 973 | if ($unit === 'weeks') { |
| 974 | return $result / static::getDaysPerWeek(); |
| 975 | } |
| 976 | return $result; |
| 977 | } |
| 978 | } |
| 979 |