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

AuthorConverterTrait::convertInstitutionnel()   A

Complexity

Conditions 5
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 4
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 9
rs 9.6111
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
trait AuthorConverterTrait
13
{
14
    protected function convertJsonLDAuthor(array $data, int $index): ?string
15
    {
16
        // author=Bob
17
        // Why restricted to index 1 ??
18
        if (1 === $index && $this->extractAuthorFromString($data)) {
19
            return $this->extractAuthorFromString($data);
20
        }
21
22
        if (isset($data['author']) && is_string($data['author']) && $index === 1) {
23
            return html_entity_decode($data['author']);
24
        }
25
26
        // author ['name'=>'Bob','@type'=>'Person']
27
        $simpleAuthor = $this->extractFromSimpleArray($data, $index);
28
        if ($simpleAuthor) {
29
            return $simpleAuthor;
30
        }
31
32
        // author [ 0 => ['name'=>'Bob'], 1=> ...]
33
        $extractAuthorFromArray = $this->extractAuthorFromIndexedArray($data, $index);
34
        if ($extractAuthorFromArray) {
35
            return $extractAuthorFromArray;
36
        }
37
38
        return null;
39
    }
40
41
    /**
42
     * author=Bob
43
     */
44
    protected function extractAuthorFromString(array $data): ?string
45
    {
46
        if (isset($data['author']) && is_string($data['author'])) {
47
            return html_entity_decode($data['author']);
48
        }
49
50
        return null;
51
    }
52
53
    /**
54
     * author ['name'=>'Bob','@type'=>'Person']
55
     */
56
    protected function extractFromSimpleArray(array $data, int $index): ?string
57
    {
58
        if (0 === $index
59
            && isset($data['author'])
60
            && isset($data['author']['name'])
61
            && (!isset($data['author']['@type'])
62
                || 'Person' === $data['author']['@type'])
63
        ) {
64
            if (is_string($data['author']['name'])) {
65
                return html_entity_decode($data['author']['name']);
66
            }
67
68
            return html_entity_decode($data['author']['name'][0]);
69
        }
70
71
        return null;
72
    }
73
74
    /**
75
     * author [ 0 => ['name'=>'Bob'], 1=> ...]
76
     */
77
    protected function extractAuthorFromIndexedArray(array $data, int $index): ?string
78
    {
79
        if (isset($data['author']) && isset($data['author'][$index])
80
            && (!isset($data['author'][$index]['@type'])
81
                || 'Person' === $data['author'][$index]['@type'])
82
        ) {
83
            if (isset($data['author'][$index]['name']) && is_string($data['author'][$index]['name'])) {
84
                return html_entity_decode($data['author'][$index]['name']);
85
            }
86
87
            // "author" => [ "@type" => "Person", "name" => [] ]
88
            if (isset($data['author'][$index]['name'][0])) {
89
                return html_entity_decode($data['author'][$index]['name'][0]);
90
            }
91
        }
92
93
        return null;
94
    }
95
96
    protected function convertInstitutionnel($data): ?string
97
    {
98
        if (isset($data['author']) && isset($data['author'][0]) && isset($data['author'][0]['@type'])
99
            && 'Person' !== $data['author'][0]['@type']
100
        ) {
101
            return html_entity_decode($data['author'][0]['name']);
102
        }
103
104
        return null;
105
    }
106
107
    /**
108
     * Used by OpenGraphMapper (so also ExternMapper).
109
     * If more than 2 authors, reduce to the 2 first names.
110
     */
111
    protected function shrinkMultiAuthors(?string $authors): ?string
112
    {
113
        if (empty($authors)) {
114
            return null;
115
        }
116
        // "Bob, Martin ; Yul, Bar ; ... ; ..."
117
        if (preg_match('#([^;]+;[^;]+);[^;]+;.+#', $authors, $matches)) {
118
            return $matches[1];
119
        }
120
        // "Bob Martin, Yul Bar, ..., ...,..."
121
        if (preg_match('#([^,]+,[^,]+),[^,]+,.+#', $authors, $matches)) {
122
            return $matches[1];
123
        }
124
125
        return $authors;
126
    }
127
128
    /**
129
     * If more than 2 authors, return "oui" for the bibliographic parameter "et al.".
130
     */
131
    protected function isAuthorsEtAl(?string $authors): bool
132
    {
133
        if (empty($authors)) {
134
            return false;
135
        }
136
        if (substr_count($authors, ',') >= 2 || substr_count($authors, ';') >= 1) {
137
            return true;
138
        }
139
140
        return false;
141
    }
142
143
    protected function cleanAuthor(?string $str = null): ?string
144
    {
145
        if ($str === null) {
146
            return null;
147
        }
148
        $str = $this->clean($str); // in MapperConverterTrait : todo extract
0 ignored issues
show
Bug introduced by
The method clean() does not exist on App\Domain\Publisher\Traits\AuthorConverterTrait. Did you maybe mean cleanAuthor()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

148
        /** @scrutinizer ignore-call */ 
149
        $str = $this->clean($str); // in MapperConverterTrait : todo extract

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.

Loading history...
149
        // "https://www.facebook.com/search/top/?q=..."
150
        if (preg_match('#^https?://.+#i', $str)) {
151
            return null;
152
        }
153
        // "Par Bob"
154
        if (preg_match('#^Par (.+)$#i', $str, $matches)) {
155
            return $matches[1];
156
        }
157
158
        return $str;
159
    }
160
161
    /**
162
     * Wikification des noms/acronymes d'agences de presse.
163
     * Note : utiliser APRES clean() et cleanAuthor() sinon bug "|"
164
     */
165
    protected function wikifyPressAgency(?string $str): ?string
166
    {
167
        if (empty($str)) {
168
            return null;
169
        }
170
        // skip potential wikilinks
171
        if (strpos($str, '[') !== false) {
172
            return $str;
173
        }
174
        $str = preg_replace('#\b(AFP)\b#i', '[[Agence France-Presse|AFP]]', $str);
175
        $str = str_replace('Reuters', '[[Reuters]]', $str);
176
        $str = str_replace('Associated Press', '[[Associated Press]]', $str);
177
        $str = preg_replace('#\b(PA)\b#', '[[Press Association|PA]]', $str);
178
        $str = preg_replace('#\b(AP)\b#', '[[Associated Press|AP]]', $str);
179
        $str = str_replace('Xinhua', '[[Xinhua]]', $str);
180
        $str = preg_replace('#\b(ATS)\b#', '[[Agence télégraphique suisse|ATS]]', $str);
181
182
        return preg_replace('#\b(PC|CP)\b#', '[[La Presse canadienne|PC]]', $str);
183
    }
184
}