Boundaries.php
2 years ago
Cast.php
2 years ago
Comparison.php
1 year ago
Converter.php
2 years ago
Creator.php
2 years ago
Date.php
1 month ago
DeprecatedProperties.php
2 years ago
Difference.php
2 years ago
IntervalRounding.php
2 years ago
IntervalStep.php
2 years ago
Localization.php
1 month ago
Macro.php
2 years ago
MagicParameter.php
2 years ago
Mixin.php
2 years ago
Modifiers.php
2 years ago
Mutability.php
2 years ago
ObjectInitialisation.php
2 years ago
Options.php
1 year ago
Rounding.php
2 years ago
Serialization.php
2 years ago
Test.php
1 month ago
Timestamp.php
1 month ago
ToStringFormat.php
2 years ago
Units.php
2 years ago
Week.php
2 years ago
IntervalRounding.php
49 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 IAWPSCOPED\Carbon\Traits; |
| 12 | |
| 13 | use IAWPSCOPED\Carbon\CarbonInterval; |
| 14 | use IAWPSCOPED\Carbon\Exceptions\InvalidIntervalException; |
| 15 | use DateInterval; |
| 16 | /** |
| 17 | * Trait to call rounding methods to interval or the interval of a period. |
| 18 | * @internal |
| 19 | */ |
| 20 | trait IntervalRounding |
| 21 | { |
| 22 | protected function callRoundMethod(string $method, array $parameters) |
| 23 | { |
| 24 | $action = \substr($method, 0, 4); |
| 25 | if ($action !== 'ceil') { |
| 26 | $action = \substr($method, 0, 5); |
| 27 | } |
| 28 | if (\in_array($action, ['round', 'floor', 'ceil'])) { |
| 29 | return $this->{$action . 'Unit'}(\substr($method, \strlen($action)), ...$parameters); |
| 30 | } |
| 31 | return null; |
| 32 | } |
| 33 | protected function roundWith($precision, $function) |
| 34 | { |
| 35 | $unit = 'second'; |
| 36 | if ($precision instanceof DateInterval) { |
| 37 | $precision = (string) CarbonInterval::instance($precision, [], \true); |
| 38 | } |
| 39 | if (\is_string($precision) && \preg_match('/^\\s*(?<precision>\\d+)?\\s*(?<unit>\\w+)(?<other>\\W.*)?$/', $precision, $match)) { |
| 40 | if (\trim($match['other'] ?? '') !== '') { |
| 41 | throw new InvalidIntervalException('Rounding is only possible with single unit intervals.'); |
| 42 | } |
| 43 | $precision = (int) ($match['precision'] ?: 1); |
| 44 | $unit = $match['unit']; |
| 45 | } |
| 46 | return $this->roundUnit($unit, $precision, $function); |
| 47 | } |
| 48 | } |
| 49 |