Completed
Branch master (86f356)
by Dispositif
02:51
created

GoogleBookMapper::convertIsbn()   A

Complexity

Conditions 6
Paths 6

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 10
c 0
b 0
f 0
nc 6
nop 1
dl 0
loc 19
rs 9.2222
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 Scriptotek\GoogleBooks\Volume;
13
14
/**
15
 * Google Books API data mapping.
16
 * Doc : https://developers.google.com/books/docs/v1/reference/volumes
17
 * Class GoogleBookMapper.
18
 */
19
class GoogleBookMapper extends AbstractBookMapper implements MapperInterface
20
{
21
    // raw URL or wiki-template ?
22
    const MODE_RAW_URL       = true;
23
    const GOOGLE_URL_REPLACE = 'https://books.google.com/books?id=%s';
24
    // sous-titre non ajoutés :
25
    const SUBTITLE_FILTER = ['roman', 'récit', 'poèmes', 'biographie'];
26
27
    /**
28
     * @param $volume Volume
29
     *
30
     * @return array
31
     */
32
    public function process($volume): array
33
    {
34
        return [
35
            // language : data not accurate !
36
            // 'langue' => $this->langFilterByIsbn($volume),
37
            'auteur1' => $volume->authors[0],
38
            'auteur2' => $volume->authors[1] ?? null,
39
            'auteur3' => $volume->authors[2] ?? null,
40
            'titre' => $volume->title,
41
            'sous-titre' => $this->filterSubtitle($volume),
42
            'année' => $this->convertDate2Year($volume->publishedDate),
43
            'pages totales' => (string)$volume->pageCount ?? null,
44
            'isbn' => $this->convertIsbn($volume),
45
            'présentation en ligne' => $this->presentationEnLigne($volume),
46
            'lire en ligne' => $this->lireEnLigne($volume),
47
        ];
48
    }
49
50
    /**
51
     * @param $volume
52
     *
53
     * @return string|null
54
     */
55
    private function filterSubtitle($volume): ?string
56
    {
57
        // "biographie" ?
58
        if (empty($volume->subtitle) || in_array(mb_strtolower($volume->subtitle), self::SUBTITLE_FILTER)) {
59
            return null;
60
        }
61
62
        return $volume->subtitle;
63
    }
64
65
    /**
66
     * @param $data
67
     *
68
     * @return string|null
69
     */
70
    private function convertDate2Year($data)
71
    {
72
        if (!isset($data)) {
73
            return null;
74
        }
75
        if (preg_match('/[^0-9]?([12][0-9]{3})[^0-9]?/', $data, $matches) > 0) {
76
            return (string)$matches[1];
77
        }
78
79
        return null;
80
    }
81
82
    /**
83
     * @param Volume $volume
84
     *
85
     * @return string|null
86
     */
87
    private function convertIsbn(Volume $volume): ?string
88
    {
89
        if (!isset($volume->industryIdentifiers)) {
90
            return null;
91
        }
92
        // so isbn-13 replace isbn-10
93
        // todo refac algo (if 2x isbn13?)
94
        $isbn = null;
95
        $ids = (array)$volume->industryIdentifiers;
96
        foreach ($ids as $id) {
97
            if (!isset($isbn) && in_array($id->type, ['ISBN_10', 'ISBN_13'])) {
98
                $isbn = $id->identifier;
99
            }
100
            if ('ISBN_13' === $id->type) {
101
                $isbn = $id->identifier;
102
            }
103
        }
104
105
        return $isbn;
106
    }
107
108
    /**
109
     * @param Volume $volume
110
     *
111
     * @return string|null
112
     */
113
    private function presentationEnLigne(Volume $volume): ?string
114
    {
115
        if (empty($volume->id) || !in_array($volume->accessInfo->viewability, ['PARTIAL'])) {
116
            return null;
117
        }
118
119
        return $this->returnGoogleRef($volume);
120
    }
121
122
    /**
123
     * @param Volume $volume
124
     *
125
     * @return string|null
126
     */
127
    private function lireEnLigne(Volume $volume): ?string
128
    {
129
        if (empty($volume->id) || !in_array($volume->accessInfo->viewability, ['ALL_PAGES'])) {
130
            return null;
131
        }
132
133
        return $this->returnGoogleRef($volume);
134
    }
135
136
    /**
137
     * @param Volume $volume
138
     *
139
     * @return string
140
     */
141
    private function returnGoogleRef(Volume $volume): string
142
    {
143
        if (self::MODE_RAW_URL) {
144
            return sprintf(self::GOOGLE_URL_REPLACE, $volume->id);
145
        }
146
147
        return sprintf('{{Google Livres|%s}}', $volume->id);
148
    }
149
150
    /**
151
     * Language data not consistant.
152
     * -> comparing by ISBN code language.
153
     */
154
    //    private function langFilterByIsbn(Volume $volume): ?string
155
    //    {
156
    //        $isbn = $this->convertIsbn($volume);
157
    //        if ($isbn) {
158
    //            try {
159
    //                $isbnMachine = new IsbnFacade($isbn);
160
    //                $isbnMachine->validate();
161
    //                $isbnLang = $isbnMachine->getCountryShortName();
162
    //            } catch (\Throwable $e) {
163
    //                unset($e);
164
    //            }
165
    //        }
166
    //        if(isset($isbnLang)) {
167
    //            echo "(ISBN lang: ".$isbnLang.")\n";
168
    //        }else{
169
    //            echo "(no ISBN lang)\n";
170
    //        }
171
    //
172
    //        $gooLang = $volume->language;
173
    //        // filtering lang because seems inconsistant
174
    //        if (isset($isbnLang) && !empty($gooLang) && $gooLang !== $isbnLang) {
175
    //            echo sprintf(
176
    //                "*** Filtering Google langue=%s because ISBN langue=%s ",
177
    //                $gooLang,
178
    //                $isbnLang
179
    //            );
180
    //
181
    //            return null;
182
    //        }
183
    //
184
    //        return $gooLang;
185
    //    }
186
}
187