Completed
Push — master ( 9d02b0...47261c )
by Timo
11s
created

FormatService::format()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 4.8437

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 11
ccs 5
cts 8
cp 0.625
rs 9.2
cc 4
eloc 5
nc 6
nop 4
crap 4.8437
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 2 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
39
    /**
40
     * @see http://php.net/manual/de/function.date.php for formatting options
41
     * @param string $input the passed date string
42
     * @param string $inputFormat the input format that should be used for parsing
43
     * @param string $outputFormat The output format, when nothing is passed
44
     * $GLOBALS['TYPO3_CONF_VARS']['SYS']['ddmmyy'] will be used or Y-m-d when nothing is configured
45
     * @param DateTimeZone $timezone
46
     * @return \DateTime|string
47
     */
48 3
    public function format($input = '', $inputFormat = 'Y-m-d\TH:i:s\Z', $outputFormat = '', $timezone = null)
49
    {
50 3
        if ($outputFormat === '') {
51
            // when no value was passed we us the TYPO3 configured or fallback to Y-m-d
52 1
            $outputFormat = $GLOBALS['TYPO3_CONF_VARS']['SYS']['ddmmyy'] ?: 'Y-m-d';
53
        }
54
55
        // try to create DateTime object
56 3
        $timezone = is_null($timezone) ?  new DateTimeZone(date_default_timezone_get()) : $timezone;
57 3
        return $this->getFormattedDate($input, $inputFormat, $outputFormat, $timezone);
58
    }
59
60
    /**
61
     * Applies the formatting using DateTime.
62
     *
63
     * @param string $input
64
     * @param string $inputFormat
65
     * @param string $outputFormat
66
     * @param DateTimeZone $timezone
67
     * @return \DateTime|string
68
     */
69 3
    protected function getFormattedDate($input, $inputFormat, $outputFormat, DateTimeZone $timezone)
70
    {
71 3
        $formattedDate = DateTime::createFromFormat($inputFormat, $input, $timezone);
72 3
        if ($formattedDate) {
73 3
            return $formattedDate->format($outputFormat);
74
        }
75
76
        return $input;
77
    }
78
}
79