Passed
Pull Request — master (#2930)
by Rafael
34:33 queued 28:23
created

FormatService   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 91
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 11
eloc 19
dl 0
loc 91
ccs 23
cts 23
cp 1
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A timestampToIso() 0 3 1
A timestampToUtcIso() 0 3 1
A getFormattedDate() 0 8 2
A isoToTimestamp() 0 5 2
A utcIsoToTimestamp() 0 6 2
A format() 0 10 3
1
<?php
2
namespace ApacheSolrForTypo3\Solr\System\DateTime;
3
4
/***************************************************************
5
 *  Copyright notice
6
 *
7
 *  (c) 2011-2016 Timo Schmidt <[email protected]>
8
 *  All rights reserved
9
 *
10
 *  This script is part of the TYPO3 project. The TYPO3 project is
11
 *  free software; you can redistribute it and/or modify
12
 *  it under the terms of the GNU General Public License as published by
13
 *  the Free Software Foundation; either version 3 of the License, or
14
 *  (at your option) any later version.
15
 *
16
 *  The GNU General Public License can be found at
17
 *  http://www.gnu.org/copyleft/gpl.html.
18
 *
19
 *  This script is distributed in the hope that it will be useful,
20
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
21
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22
 *  GNU General Public License for more details.
23
 *
24
 *  This copyright notice MUST APPEAR in all copies of the script!
25
 ***************************************************************/
26
27
use DateTime;
28
use DateTimeZone;
29
30
/**
31
 * Testcase to check if the configuration object can be used as expected
32
 *
33
 * @author Hendrik Putzek <[email protected]>
34
 * @author Timo Hund <[email protected]>
35
 */
36
class FormatService
37
{
38
    const SOLR_ISO_DATETIME_FORMAT = 'Y-m-d\TH:i:s\Z';
39
40
    /**
41
     * @see http://php.net/manual/de/function.date.php for formatting options
42
     * @param string $input the passed date string
43
     * @param string $inputFormat the input format that should be used for parsing
44
     * @param string $outputFormat The output format, when nothing is passed
45
     * $GLOBALS['TYPO3_CONF_VARS']['SYS']['ddmmyy'] will be used or Y-m-d when nothing is configured
46
     * @param DateTimeZone $timezone
47
     * @return \DateTime|string
48
     */
49 7
    public function format($input = '', $inputFormat = 'Y-m-d\TH:i:s\Z', $outputFormat = '', $timezone = null)
50
    {
51 7
        if ($outputFormat === '') {
52
            // when no value was passed we us the TYPO3 configured or fallback to Y-m-d
53 5
            $outputFormat = $GLOBALS['TYPO3_CONF_VARS']['SYS']['ddmmyy'] ?: 'Y-m-d';
54
        }
55
56
        // try to create DateTime object
57 7
        $timezone = $timezone ?? new DateTimeZone(date_default_timezone_get());
58 7
        return $this->getFormattedDate($input, $inputFormat, $outputFormat, $timezone);
59
    }
60
61
    /**
62
     * Converts a date from unix timestamp to ISO 8601 format.
63
     *
64
     * @param int $timestamp unix timestamp
65
     * @return string the date in ISO 8601 format
66
     */
67 58
    public function timestampToIso($timestamp)
68
    {
69 58
        return date(self::SOLR_ISO_DATETIME_FORMAT, $timestamp);
70
    }
71
72
    /**
73
     * Converts a date from ISO 8601 format to unix timestamp.
74
     *
75
     * @param string $isoTime date in ISO 8601 format
76
     * @return int unix timestamp
77
     */
78 3
    public function isoToTimestamp($isoTime)
79
    {
80 3
        $dateTime = \DateTime::createFromFormat(self::SOLR_ISO_DATETIME_FORMAT,
81 3
            $isoTime);
82 3
        return $dateTime ? (int)$dateTime->format('U') : 0;
0 ignored issues
show
introduced by
$dateTime is of type DateTime, thus it always evaluated to true.
Loading history...
83
    }
84
85
    /**
86
     * Converts a date from unix timestamp to ISO 8601 format in UTC timezone.
87
     *
88
     * @param int $timestamp unix timestamp
89
     * @return string the date in ISO 8601 format
90
     */
91 3
    public function timestampToUtcIso($timestamp)
92
    {
93 3
        return gmdate(self::SOLR_ISO_DATETIME_FORMAT, $timestamp);
94
    }
95
96
    /**
97
     * Converts a date from ISO 8601 format in UTC timezone to unix timestamp.
98
     *
99
     * @param string $isoTime date in ISO 8601 format
100
     * @return int unix timestamp
101
     */
102 3
    public function utcIsoToTimestamp($isoTime)
103
    {
104 3
        $utcTimeZone = new \DateTimeZone('UTC');
105 3
        $dateTime = \DateTime::createFromFormat(self::SOLR_ISO_DATETIME_FORMAT,
106 3
            $isoTime, $utcTimeZone);
107 3
        return $dateTime ? (int)$dateTime->format('U') : 0;
0 ignored issues
show
introduced by
$dateTime is of type DateTime, thus it always evaluated to true.
Loading history...
108
    }
109
110
    /**
111
     * Applies the formatting using DateTime.
112
     *
113
     * @param string $input
114
     * @param string $inputFormat
115
     * @param string $outputFormat
116
     * @param DateTimeZone $timezone
117
     * @return \DateTime|string
118
     */
119 7
    protected function getFormattedDate($input, $inputFormat, $outputFormat, DateTimeZone $timezone)
120
    {
121 7
        $formattedDate = DateTime::createFromFormat($inputFormat, $input, $timezone);
122 7
        if ($formattedDate) {
0 ignored issues
show
introduced by
$formattedDate is of type DateTime, thus it always evaluated to true.
Loading history...
123 4
            return $formattedDate->format($outputFormat);
124
        }
125
126 3
        return $input;
127
    }
128
}
129