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']), // 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
|
|
|
* @throws \Exception |
95
|
|
|
*/ |
96
|
|
|
protected function convertDate(string $str): string |
97
|
|
|
{ |
98
|
|
|
$date = new DateTime($str); |
99
|
|
|
|
100
|
|
|
return $date->format('d-m-Y'); |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|