Passed
Push — master ( 961eb0...5d24b7 )
by Dispositif
08:38
created

DateUtil::dateEnglish2french()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 32
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 26
nc 1
nop 1
dl 0
loc 32
ccs 0
cts 32
cp 0
crap 2
rs 9.504
c 1
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A DateUtil::cleanDate() 0 3 1
A DateUtil::fromWikiSignature() 0 13 2
1
<?php
2
/*
3
 * This file is part of dispositif/wikibot application (@github)
4
 * 2019/2020 © Philippe/Irønie  <[email protected]>
5
 * For the full copyright and MIT license information, view the license file.
6
 */
7
8
declare(strict_types=1);
9
10
namespace App\Domain\Utils;
11
12
use DateTime;
13
use DateTimeZone;
14
use Exception;
15
16
/**
17
 * see TYPO https://fr.wikipedia.org/wiki/Wikip%C3%A9dia:AutoWikiBrowser/Typos#Dates
18
 * Class DateUtil.
19
 */
20
class DateUtil
21
{
22
    /**
23
     * todo
24
     *
25
     * @param string $string
26
     *
27
     * @return DateTime
28
     * @throws Exception
29
     */
30
    public static function fromWikiSignature(string $string)
31
    {
32
        // 1 janvier 2020 à 17:44 (CET) => 1 January 2020 à 17:44 (CET)
33
        $string = self::french2English(trim($string));
34
35
        $timezone = new DateTimeZone('Europe/Paris'); // CET, CEST
36
        if (preg_match('/\(UTC\)/', $string)) {
37
            $timezone = new DateTimeZone('UTC');
38
        }
39
        $string = preg_replace('/\([A-Z]{3,4}\)/', '', $string); // strip CET,CEST,UTC
40
        // convert fuseau ? https://stackoverflow.com/questions/5746531/utc-date-time-string-to-timezone
41
42
        return DateTime::createFromFormat('d M Y \à H\:i', trim($string), $timezone);
43
    }
44
45
    public function cleanDate(string $date)
46
    {
47
        return $this->english2french($date);
48
    }
49
50
    public static function english2french(string $date)
51
    {
52
        return str_replace(
53
            [
54
                'January',
55
                'February',
56
                'March',
57
                'April',
58
                'May',
59
                'June',
60
                'July',
61
                'August',
62
                'September',
63
                'October',
64
                'November',
65
                'December',
66
            ],
67
            [
68
                'janvier',
69
                'février',
70
                'mars',
71
                'avril',
72
                'mai',
73
                'juin',
74
                'juillet',
75
                'août',
76
                'septembre',
77
                'octobre',
78
                'novembre',
79
                'décembre',
80
            ],
81
            $date
82
        );
83
    }
84
85
    public static function french2English(string $date)
86
    {
87
        return str_replace(
88
            [
89
                'janvier',
90
                'février',
91
                'mars',
92
                'avril',
93
                'mai',
94
                'juin',
95
                'juillet',
96
                'août',
97
                'septembre',
98
                'octobre',
99
                'novembre',
100
                'décembre',
101
            ],
102
            [
103
                'January',
104
                'February',
105
                'March',
106
                'April',
107
                'May',
108
                'June',
109
                'July',
110
                'August',
111
                'September',
112
                'October',
113
                'November',
114
                'December',
115
            ],
116
            $date
117
        );
118
    }
119
}
120