xref: /webtrees/app/Factories/TimestampFactory.php (revision d97083fe315dad9b7d0a150d4fb5f563e57d1869)
1*d97083feSGreg Roach<?php
2*d97083feSGreg Roach
3*d97083feSGreg Roach/**
4*d97083feSGreg Roach * webtrees: online genealogy
5*d97083feSGreg Roach * Copyright (C) 2021 webtrees development team
6*d97083feSGreg Roach * This program is free software: you can redistribute it and/or modify
7*d97083feSGreg Roach * it under the terms of the GNU General Public License as published by
8*d97083feSGreg Roach * the Free Software Foundation, either version 3 of the License, or
9*d97083feSGreg Roach * (at your option) any later version.
10*d97083feSGreg Roach * This program is distributed in the hope that it will be useful,
11*d97083feSGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
12*d97083feSGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13*d97083feSGreg Roach * GNU General Public License for more details.
14*d97083feSGreg Roach * You should have received a copy of the GNU General Public License
15*d97083feSGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>.
16*d97083feSGreg Roach */
17*d97083feSGreg Roach
18*d97083feSGreg Roachdeclare(strict_types=1);
19*d97083feSGreg Roach
20*d97083feSGreg Roachnamespace Fisharebest\Webtrees\Factories;
21*d97083feSGreg Roach
22*d97083feSGreg Roachuse Fisharebest\Webtrees\Auth;
23*d97083feSGreg Roachuse Fisharebest\Webtrees\Contracts\TimestampFactoryInterface;
24*d97083feSGreg Roachuse Fisharebest\Webtrees\Contracts\TimestampInterface;
25*d97083feSGreg Roachuse Fisharebest\Webtrees\Contracts\UserInterface;
26*d97083feSGreg Roachuse Fisharebest\Webtrees\I18N;
27*d97083feSGreg Roachuse Fisharebest\Webtrees\Site;
28*d97083feSGreg Roachuse Fisharebest\Webtrees\Timestamp;
29*d97083feSGreg Roachuse LogicException;
30*d97083feSGreg Roach
31*d97083feSGreg Roachuse function date;
32*d97083feSGreg Roachuse function date_create_from_format;
33*d97083feSGreg Roachuse function time;
34*d97083feSGreg Roach
35*d97083feSGreg Roach/**
36*d97083feSGreg Roach * Create a timestamp object.
37*d97083feSGreg Roach */
38*d97083feSGreg Roachclass TimestampFactory implements TimestampFactoryInterface
39*d97083feSGreg Roach{
40*d97083feSGreg Roach    /**
41*d97083feSGreg Roach     * @param int                $timestamp
42*d97083feSGreg Roach     * @param UserInterface|null $user
43*d97083feSGreg Roach     *
44*d97083feSGreg Roach     * @return TimestampInterface
45*d97083feSGreg Roach     */
46*d97083feSGreg Roach    public function make(int $timestamp, UserInterface $user = null): TimestampInterface
47*d97083feSGreg Roach    {
48*d97083feSGreg Roach        $user     ??= Auth::user();
49*d97083feSGreg Roach        $timezone = $user->getPreference(UserInterface::PREF_TIME_ZONE, Site::getPreference('TIMEZONE'));
50*d97083feSGreg Roach        $locale   = I18N::locale()->code();
51*d97083feSGreg Roach
52*d97083feSGreg Roach        return new Timestamp($timestamp, $timezone, $locale);
53*d97083feSGreg Roach    }
54*d97083feSGreg Roach
55*d97083feSGreg Roach    /**
56*d97083feSGreg Roach     * @param string|null        $string YYYY-MM-DD HH:MM:SS (as provided by SQL).
57*d97083feSGreg Roach     * @param string             $format
58*d97083feSGreg Roach     * @param UserInterface|null $user
59*d97083feSGreg Roach     *
60*d97083feSGreg Roach     * @return TimestampInterface
61*d97083feSGreg Roach     */
62*d97083feSGreg Roach    public function fromString(?string $string, string $format = 'Y-m-d H:i:s', UserInterface $user = null): TimestampInterface
63*d97083feSGreg Roach    {
64*d97083feSGreg Roach        $string    ??= date($format);
65*d97083feSGreg Roach        $timestamp = date_create_from_format($format, $string);
66*d97083feSGreg Roach
67*d97083feSGreg Roach        if ($timestamp === false) {
68*d97083feSGreg Roach            throw new LogicException('date/time "' . $string . '" does not match pattern "' . $format . '"');
69*d97083feSGreg Roach        }
70*d97083feSGreg Roach
71*d97083feSGreg Roach        return $this->make($timestamp->getTimestamp(), $user);
72*d97083feSGreg Roach    }
73*d97083feSGreg Roach
74*d97083feSGreg Roach    /**
75*d97083feSGreg Roach     * @param UserInterface|null $user
76*d97083feSGreg Roach     *
77*d97083feSGreg Roach     * @return TimestampInterface
78*d97083feSGreg Roach     */
79*d97083feSGreg Roach    public function now(UserInterface $user = null): TimestampInterface
80*d97083feSGreg Roach    {
81*d97083feSGreg Roach        return $this->make(time(), $user);
82*d97083feSGreg Roach    }
83*d97083feSGreg Roach}
84