CarbonFormat::fromEnumeration()   C
last analyzed

Complexity

Conditions 7
Paths 7

Size

Total Lines 28
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 56

Importance

Changes 0
Metric Value
dl 0
loc 28
ccs 0
cts 18
cp 0
rs 6.7272
c 0
b 0
f 0
cc 7
eloc 15
nc 7
nop 1
crap 56
1
<?php
2
/**
3
 * This file is part of Railt package.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
declare(strict_types=1);
9
10
namespace Railt\CarbonExtension\In;
11
12
use Carbon\Carbon;
13
14
/**
15
 * Class CarbonFormat
16
 */
17
class CarbonFormat
18
{
19
    /**
20
     * RFC3339 date format.
21
     * Example: `2005-08-15T15:52:01+00:00`
22
     */
23
    public const DEFAULT = 'RFC3339';
24
25
    /**
26
     * Simple Date format.
27
     * Example: `2005-08-15`
28
     */
29
    public const DATE = 'DATE';
30
31
    /**
32
     * Simple Time format.
33
     * Example: `15:52:01`
34
     */
35
    public const TIME = 'TIME';
36
37
    /**
38
     * Simple DateTime format.
39
     * Example: `2005-08-15 15:52:01`
40
     */
41
    public const DATE_TIME = 'DATE_TIME';
42
43
    /**
44
     * RFC 7231 date format.
45
     * Example: `Mon, 15 Aug 2005 15:52:01 GMT`
46
     */
47
    public const RFC7231 = 'RFC7231';
48
49
    /**
50
     * ISO-8601 date format.
51
     * Example: `2005-08-15T15:52:01+00:00`
52
     *
53
     * Note: This format is an alias of the RFC 3339 specification:
54
     * @see https://www.iso.org/iso-8601-date-and-time-format.html
55
     * @see https://www.ietf.org/rfc/rfc3339.txt
56
     */
57
    public const ISO8601 = 'ISO8601';
58
59
    /**
60
     * Human readable string.
61
     * Example: `2 days ago`
62
     */
63
    public const HUMAN_READABLE = 'HUMAN_READABLE';
64
65
    /**
66
     * @param string $value
67
     * @return string
68
     */
69
    public function fromEnumeration(string $value): string
70
    {
71
        switch ($value) {
72
            /**
73
             * Compatibility bugfix.
74
             * @see http://php.net/manual/en/class.datetime.php
75
             */
76
            case static::ISO8601:
77
                return Carbon::RFC3339;
78
79
            case static::RFC7231:
80
                return Carbon::RFC7231_FORMAT;
81
82
            case static::DATE_TIME:
83
                return 'Y-m-d H:i:s';
84
85
            case static::DATE:
86
                return 'Y-m-d';
87
88
            case static::TIME:
89
                return 'H:i:s';
90
91
            case static::HUMAN_READABLE:
92
                return static::HUMAN_READABLE;
93
        }
94
95
        return \constant(Carbon::class . '::' . $value);
96
    }
97
}
98