Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
5 / 5
CRAP
100.00% covered (success)
100.00%
1 / 1
FrmtrDateTimeAbstract
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
5 / 5
7
100.00% covered (success)
100.00%
1 / 1
 getCalendarType
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setCalendarType
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 getTimeZone
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setTimeZone
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 createFormatter
n/a
0 / 0
n/a
0 / 0
0
 format
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2
3/**
4 * @author: Doug Wilbourne (dougwilbourne@gmail.com)
5 */
6
7declare(strict_types=1);
8
9namespace pvc\frmtr\date_time;
10
11use DateTimeZone;
12use IntlDateFormatter;
13use pvc\frmtr\err\InvalidIntlCalendarTypeException;
14use pvc\frmtr\Frmtr;
15
16/**
17 * Class FrmtrDateTimeAbstract
18 * @extends Frmtr<float|int>
19 */
20abstract class FrmtrDateTimeAbstract extends Frmtr
21{
22    protected int $calendarType;
23
24    protected DateTimeZone $timeZone;
25
26    /**
27     * @var array<int>
28     */
29    private array $validCalendarTypes = [IntlDateFormatter::TRADITIONAL, IntlDateFormatter::GREGORIAN];
30
31    /**
32     * @return int
33     */
34    public function getCalendarType(): int
35    {
36        return $this->calendarType ?? IntlDateFormatter::GREGORIAN;
37    }
38
39    /**
40     * @param int $calendarType
41     */
42    public function setCalendarType(int $calendarType): void
43    {
44        if (!in_array($calendarType, $this->validCalendarTypes)) {
45            throw new InvalidIntlCalendarTypeException();
46        }
47        $this->calendarType = $calendarType;
48    }
49
50    /**
51     * @return DateTimeZone
52     * null defaults to the timezone returned by date_default_timezone_get
53     */
54    public function getTimeZone(): DateTimeZone
55    {
56        return $this->timeZone ?? new DateTimeZone(date_default_timezone_get());
57    }
58
59    /**
60     * @param DateTimeZone $timeZone
61     */
62    public function setTimeZone(DateTimeZone $timeZone): void
63    {
64        $this->timeZone = $timeZone;
65    }
66
67    /**
68     * createFormatter
69     * @return IntlDateFormatter
70     */
71    abstract protected function createFormatter(): IntlDateFormatter;
72
73    /**
74     * format
75     *
76     * The IntlDateFormatter class can take a wide variety of arguments.  However, this class attempts to strip away as
77     * much complexity as possible.  I believe dates/times should be stored as timestamps.  DateTime (and
78     * DateTimeImmutable) objects suffer from the fact that the timezone used in creating the DateTime object is
79     * essentially not used for any other purpose than creating a timestamp from the DateTime object.  For example
80     * DateTime::diff does not respect the timezone of the DateTime object.  The array produced by localtime() is
81     * just a more detailed representation the int/float produced by time().
82     *
83     * Because this class extends Frmtr, the argument is named 'value', but it would be better to name it 'timestamp'
84     *
85     * @param float|int $value
86     * @return string
87     */
88    public function format($value): string
89    {
90        return ($this->createFormatter()->format($value) ?: '');
91    }
92}