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
|
|
|
|
11
|
|
|
namespace App\Domain\Models\Wiki; |
12
|
|
|
|
13
|
|
|
use Exception; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Convert wiki-template in another wiki-template. |
17
|
|
|
* Class TemplateConverter |
18
|
|
|
* |
19
|
|
|
* @package App\Domain\Models\Wiki |
20
|
|
|
*/ |
21
|
|
|
class TemplateConverter |
22
|
|
|
{ |
23
|
|
|
const PARAM_OUVRAGE_TO_ARTICLE |
24
|
|
|
= [ |
25
|
|
|
'tome' => 'volume', |
26
|
|
|
'numéro chapitre' => 'numéro', |
27
|
|
|
'pages totales' => 'pages', |
28
|
|
|
]; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Conversion {ouvrage} en {article}. |
32
|
|
|
* todo Move factory ? |
33
|
|
|
* |
34
|
|
|
* @param OuvrageTemplate $ouvrage |
35
|
|
|
* |
36
|
|
|
* @return ArticleTemplate|null |
37
|
|
|
* @throws Exception |
38
|
|
|
*/ |
39
|
|
|
public static function ArticleFromOuvrage(OuvrageTemplate $ouvrage): ?ArticleTemplate |
40
|
|
|
{ |
41
|
|
|
$article = \App\Domain\WikiTemplateFactory::create('article'); |
42
|
|
|
try { |
43
|
|
|
$data = self::convertDataOuvrage2Article($ouvrage->toArray()); |
44
|
|
|
|
45
|
|
|
$article->hydrate($data); |
46
|
|
|
|
47
|
|
|
$articleInfos = array_merge(['ConvertFromOuvrage' => 1], $ouvrage->getInfos()); |
48
|
|
|
$article->setInfos($articleInfos); |
|
|
|
|
49
|
|
|
} catch (\Throwable $e) { |
50
|
|
|
return null; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
if ($article instanceof ArticleTemplate |
54
|
|
|
&& self::hasNeededParams($article) |
55
|
|
|
) { |
56
|
|
|
return $article; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
return null; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* TODO : refac/move in AbstractTemplate |
64
|
|
|
* |
65
|
|
|
* @param ArticleTemplate $article |
66
|
|
|
* |
67
|
|
|
* @return bool |
68
|
|
|
* @throws Exception |
69
|
|
|
*/ |
70
|
|
|
public static function hasNeededParams(ArticleTemplate $article): bool |
71
|
|
|
{ |
72
|
|
|
$needed = ['titre', 'périodique', 'date']; |
73
|
|
|
foreach ($needed as $need) { |
74
|
|
|
if (empty($article->getParam($need))) { |
75
|
|
|
echo "Article : paramètre obligatoire '$need' manquant"; |
76
|
|
|
|
77
|
|
|
return false; |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
return true; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Convert param names between wiki-templates. |
86
|
|
|
* |
87
|
|
|
* @param array $ouvrageData |
88
|
|
|
* |
89
|
|
|
* @return array |
90
|
|
|
*/ |
91
|
|
|
private static function convertDataOuvrage2Article(array $ouvrageData): array |
92
|
|
|
{ |
93
|
|
|
$data = []; |
94
|
|
|
foreach ($ouvrageData as $param => $value) { |
95
|
|
|
// Conversion du nom de paramètre |
96
|
|
|
// + esquive des doublons qui supprimeraient une value |
97
|
|
|
if (key_exists($param, self::PARAM_OUVRAGE_TO_ARTICLE) |
98
|
|
|
&& !key_exists(self::PARAM_OUVRAGE_TO_ARTICLE[$param], $ouvrageData) |
99
|
|
|
) { |
100
|
|
|
$data[self::PARAM_OUVRAGE_TO_ARTICLE[$param]] = $value; |
101
|
|
|
continue; |
102
|
|
|
} |
103
|
|
|
// Sinon : on conserve param/value, même si param invalide dans Article |
104
|
|
|
$data[$param] = $value; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
$data = self::convertDateForArticle($data); |
108
|
|
|
|
109
|
|
|
return $data; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
public static function convertDateForArticle(array $data): array |
113
|
|
|
{ |
114
|
|
|
// generate 'date' |
115
|
|
|
if (empty($data['date'])) { |
116
|
|
|
$data['date'] = trim( |
117
|
|
|
sprintf( |
118
|
|
|
'%s %s %s', |
119
|
|
|
$data['jour'] ?? '', |
120
|
|
|
$data['mois'] ?? '', |
121
|
|
|
$data['année'] ?? '' |
122
|
|
|
) |
123
|
|
|
); |
124
|
|
|
unset($data['jour']); |
125
|
|
|
unset($data['mois']); |
126
|
|
|
unset($data['année']); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
return $data; |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.