Passed
Push — master ( 7495f4...63074b )
by Dispositif
02:55
created

MapperConverterTrait::stripEmailAdress()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 7
rs 10
1
<?php
2
/*
3
 * This file is part of dispositif/wikibot application (@github)
4
 * 2019-2023 © Philippe M./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\Publisher\Traits;
11
12
use App\Domain\Enums\Language;
13
use DateTime;
14
use Exception;
15
16
/**
17
 * Typo cleaning, email delete, convert date to d-m-Y, etc.
18
 */
19
trait MapperConverterTrait
20
{
21
    protected function isAnArticle(?string $str): bool
22
    {
23
        return in_array($str, ['article', 'journalArticle']);
24
    }
25
26
    protected function convertDCpage(array $meta): ?string
27
    {
28
        if (isset($meta['citation_firstpage'])) {
29
            $page = $meta['citation_firstpage'];
30
            if (isset($meta['citation_lastpage'])) {
31
                $page .= '–' . $meta['citation_lastpage'];
32
            }
33
34
            return (string)$page;
35
        }
36
37
        return null;
38
    }
39
40
    /**
41
     * Note : à appliquer AVANT wikification (sinon bug sur | )
42
     *
43
     * @param string|null $str
44
     *
45
     * @return string|null
46
     */
47
    protected function clean(?string $str = null): ?string
48
    {
49
        if ($str === null) {
50
            return null;
51
        }
52
        $str = $this->stripEmailAdress($str);
53
54
        $str = str_replace(
55
            [
56
                '|',
57
                "\n",
58
                "\t",
59
                "\r",
60
                '&#x27;',
61
                '&#39;',
62
                '&#039;',
63
                '&apos;',
64
                "\n",
65
                "&#10;",
66
                "&eacute;",
67
                '©',
68
                '{{',
69
                '}}',
70
                '[[',
71
                ']]',
72
            ],
73
            [
74
                '/',
75
                ' ',
76
                ' ',
77
                '',
78
                "’",
79
                "'",
80
                "'",
81
                "'",
82
                '',
83
                ' ',
84
                "é",
85
                '',
86
                '',
87
                '',
88
                '',
89
                '',
90
            ],
91
            $str
92
        );
93
94
        $str = html_entity_decode($str);
95
        $str = strip_tags($str);
96
97
        return trim($str);
98
    }
99
100
    protected function stripEmailAdress(?string $str = null): ?string
101
    {
102
        if ($str === null) {
103
            return null;
104
        }
105
106
        return preg_replace('# ?[^ ]+@[^ ]+\.[A-Z]+#i', '', $str);
107
    }
108
109
    protected function convertOGtype2format(?string $ogType): ?string
110
    {
111
        if (empty($ogType)) {
112
            return null;
113
        }
114
        // og:type = default: website / video.movie / video.tv_show video.other / article, book, profile
115
        if (strpos($ogType, 'video') !== false) {
116
            return 'vidéo';
117
        }
118
        if (strpos($ogType, 'book') !== false) {
119
            return 'livre';
120
        }
121
122
        return null;
123
    }
124
125
    /**
126
     * https://developers.facebook.com/docs/internationalization#locales
127
     * @param string|null $lang
128
     *
129
     * @return string|null
130
     */
131
    protected function convertLangue(?string $lang = null): ?string
132
    {
133
        if (empty($lang)) {
134
            return null;
135
        }
136
        // en_GB
137
        if (preg_match('#^([a-z]{2})_[A-Z]{2}$#', $lang, $matches)) {
138
            return $matches[1];
139
        }
140
141
        return Language::all2wiki($lang);
142
    }
143
144
    protected function convertDate(?string $str): ?string
145
    {
146
        if (empty($str)) {
147
            return null;
148
        }
149
        $str = str_replace(' 00:00:00', '', $str);
150
        $str = str_replace('/', '-', $str);
151
152
        // "2012" - "1775-1783" (Gallica)
153
        if (preg_match('#^[12]\d{3}$#', $str) || preg_match('#^[12]\d{3}-[12]\d{3}$#', $str)) {
154
            return $str;
155
        }
156
157
        return $this->tryFormatDateOrComment($str);
158
    }
159
160
    protected function tryFormatDateOrComment(string $str): string
161
    {
162
        try {
163
            $date = new DateTime($str);
164
        } catch (Exception $e) {
165
            // 23/11/2015 00:00:00
166
            if (isset($this->log) && method_exists($this->log, 'notice')) {
167
                $this->log->notice('tryFormatDateOrComment failed with ' . $str);
168
            }
169
170
            return sprintf('<!-- %s -->', $str);
171
        }
172
173
        return $date->format('d-m-Y');
174
    }
175
}
176