Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
Carbon
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
2 / 2
4
100.00% covered (success)
100.00%
1 / 1
 createFromDatetime
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
3
 formatDuration
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3declare(strict_types=1);
4
5namespace Engelsystem\Helpers;
6
7use Carbon\CarbonInterval;
8
9class Carbon extends \Carbon\Carbon
10{
11    public const DATETIME_LOCAL = '!Y-m-d\TH:i';
12
13    public const DATETIME_FALLBACK = '!Y-m-d H:i';
14
15    public const DATETIME_FORMATS = [
16        self::DATETIME_LOCAL,
17        self::DATETIME_FALLBACK,
18        self::DEFAULT_TO_STRING_FORMAT,
19    ];
20
21    /**
22     * Parses HTML datetime-local and ISO date/time strings.
23     *
24     * @return self|null Carbon if parseable, else null
25     * @see self::DATETIME_FORMATS
26     */
27    public static function createFromDatetime(string $value): ?\Carbon\Carbon
28    {
29        foreach (self::DATETIME_FORMATS as $datetimeFormat) {
30            if (self::canBeCreatedFromFormat($value, $datetimeFormat)) {
31                return self::createFromFormat($datetimeFormat, $value);
32            }
33        }
34
35        return null;
36    }
37
38    /**
39     * Formats a CarbonInterval into a human-readable duration string consisting of hours and minutes.
40     * Format is defined in the localization files under 'general.duration.format'.
41     *
42     * @param CarbonInterval $interval The interval to format
43     * @param string $format The format string, e.g. '%dh %02dm'
44     * @return string The formatted duration string
45     */
46    public static function formatDuration(CarbonInterval $interval, string $format): string
47    {
48        $interval->cascade();
49        $hours = floor($interval->totalHours);
50        $minutes = $interval->minutes;
51
52        return sprintf($format, $hours, $minutes);
53    }
54}