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 Carbon\Traits;
12

13
use Carbon\CarbonInterval;
14
use Carbon\Exceptions\InvalidIntervalException;
15
use DateInterval;
16

17
/**
18
 * Trait to call rounding methods to interval or the interval of a period.
19
 */
20
trait IntervalRounding
21
{
22 1
    protected function callRoundMethod(string $method, array $parameters)
23
    {
24 1
        $action = substr($method, 0, 4);
25

26 1
        if ($action !== 'ceil') {
27 1
            $action = substr($method, 0, 5);
28
        }
29

30 1
        if (\in_array($action, ['round', 'floor', 'ceil'])) {
31 1
            return $this->{$action.'Unit'}(substr($method, \strlen($action)), ...$parameters);
32
        }
33

34 1
        return null;
35
    }
36

37 1
    protected function roundWith($precision, $function)
38
    {
39 1
        $unit = 'second';
40

41 1
        if ($precision instanceof DateInterval) {
42 1
            $precision = (string) CarbonInterval::instance($precision);
43
        }
44

45 1
        if (\is_string($precision) && preg_match('/^\s*(?<precision>\d+)?\s*(?<unit>\w+)(?<other>\W.*)?$/', $precision, $match)) {
46 1
            if (trim($match['other'] ?? '') !== '') {
47 1
                throw new InvalidIntervalException('Rounding is only possible with single unit intervals.');
48
            }
49

50 1
            $precision = (int) ($match['precision'] ?: 1);
51 1
            $unit = $match['unit'];
52
        }
53

54 1
        return $this->roundUnit($unit, $precision, $function);
55
    }
56
}

Read our documentation on viewing source code .

Loading