Passed
Push — dev ( eba0dc...b8ced8 )
by Dispositif
02:52
created

WebMapper   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 111
Duplicated Lines 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
eloc 46
c 4
b 0
f 0
dl 0
loc 111
rs 10
wmc 22

5 Methods

Rating   Name   Duplication   Size   Complexity  
A convertInstitutionnel() 0 9 5
B convertAuteur() 0 21 10
A process() 0 30 2
A convertDate() 0 12 3
A wikifyPressAgency() 0 16 2
1
<?php
2
/**
3
 * This file is part of dispositif/wikibot application
4
 * 2019 © Philippe M. <[email protected]>
5
 * For the full copyright and MIT license information, please view the LICENSE file.
6
 */
7
8
declare(strict_types=1);
9
10
namespace App\Domain\Publisher;
11
12
use DateTime;
13
14
/**
15
 * Generic mapper for press/revue article on web.
16
 * Using JSON-LD and meta tags to obtain {article} data.
17
 * Class WebMapper
18
 *
19
 * @package App\Domain\Publisher
20
 */
21
class WebMapper implements MapperInterface
22
{
23
    public function process($data): array
24
    {
25
        if (!isset($data['JSON-LD'])) {
26
            return [];
27
        }
28
        $data = $data['JSON-LD'];
29
        //
30
        //        foreach ($data as $dat) {
31
        //            if ('NewsArticle' === $dat['@type']) {
32
        //                $data = $dat;
33
        //                goto mapping;
34
        //            }
35
        //            // todo ('WebPage' == @type)  ==> {{lien web}} ?
36
        //        }
37
        //
38
        //        return []; // exception ?
39
        //
40
        //        mapping:
41
42
        return [
43
            //            'langue' => 'fr',
44
            'périodique' => $data['publisher']['name'] ?? null,
45
            //           'acces' =>  $data['isAccessibleForFree'],
46
            'titre' => html_entity_decode($data['headline']),
47
            'lire en ligne' => $data['mainEntityOfPage']['@id'],
48
            'date' => $this->convertDate($data['datePublished'] ?? null), // 2020-03-19T19:13:01.000Z
49
            'auteur1' => $this->convertAuteur($data, 0),
50
            'auteur2' => $this->convertAuteur($data, 1),
51
            'auteur3' => $this->convertAuteur($data, 2),
52
            'auteur institutionnel' => $this->convertInstitutionnel($data),
53
        ];
54
    }
55
56
    protected function convertAuteur($data, $indice)
57
    {
58
        // author ['name'=>'Bob','@type'=>'Person']
59
        if (0 === $indice
60
            && isset($data['author'])
61
            && isset($data['author']['name'])
62
            && (!isset($data['author']['@type'])
63
                || 'Person' === $data['author']['@type'])
64
        ) {
65
            return html_entity_decode($data['author']['name']);
66
        }
67
68
        // author [ 0 => ['name'=>'Bob'], 1=> ...]
69
        if (isset($data['author']) && isset($data['author'][$indice])
70
            && (!isset($data['author'][$indice]['@type'])
71
                || 'Person' === $data['author'][$indice]['@type'])
72
        ) {
73
            return html_entity_decode($data['author'][$indice]['name']);
74
        }
75
76
        return null;
77
    }
78
79
    protected function convertInstitutionnel($data)
80
    {
81
        if (isset($data['author']) && isset($data['author'][0]) && isset($data['author'][0]['@type'])
82
            && 'Person' !== $data['author'][0]['@type']
83
        ) {
84
            return html_entity_decode($data['author'][0]['name']);
85
        }
86
87
        return null;
88
    }
89
90
    /**
91
     * @param string $str
92
     *
93
     * @return string
94
     */
95
    protected function convertDate(?string $str): ?string
96
    {
97
        if (empty($str)) {
98
            return null;
99
        }
100
        try {
101
            $date = new DateTime($str);
102
        } catch (\Exception $e) {
103
            return null;
104
        }
105
106
        return $date->format('d-m-Y');
107
    }
108
109
    /**
110
     * Wikification des noms/acronymes d'agences de presse.
111
     *
112
     * @param string $str
113
     *
114
     * @return string
115
     */
116
    protected function wikifyPressAgency(string $str): string
117
    {
118
        // skip potential wikilinks
119
        if (strpos($str, '[') !== false) {
120
            return $str;
121
        }
122
        $str = preg_replace('#\b(AFP)\b#', '[[Agence France-Presse|AFP]]', $str);
123
        $str = str_replace('Reuters', '[[Reuters]]', $str);
124
        $str = str_replace('Associated Press', '[[Associated Press]]', $str);
125
        $str = preg_replace('#\b(PA)\b#', '[[Press Association|PA]]', $str);
126
        $str = preg_replace('#\b(AP)\b#', '[[Associated Press|AP]]', $str);
127
        $str = str_replace('Xinhua', '[[Xinhua]]', $str);
128
        $str = preg_replace('#\b(ATS)\b#', '[[Agence télégraphique suisse|ATS]]', $str);
129
        $str = preg_replace('#\b(PC|CP)\b#', '[[La Presse canadienne|PC]]', $str);
130
131
        return $str;
132
    }
133
}
134