• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

webeweb / ciqual-library / 8307020934

16 Mar 2024 10:11AM UTC coverage: 99.245%. Remained the same
8307020934

push

github

webeweb
Update README

263 of 265 relevant lines covered (99.25%)

21.89 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

96.08
/src/Provider/XmlProvider.php
1
<?php
2

3
declare(strict_types = 1);
4

5
/*
6
 * This file is part of the ciqual-library package.
7
 *
8
 * (c) 2021 WEBEWEB
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13

14
namespace WBW\Library\Ciqual\Provider;
15

16
use DOMDocument;
17
use DOMElement;
18
use GuzzleHttp\Client;
19
use GuzzleHttp\Exception\GuzzleException;
20
use RuntimeException;
21
use Throwable;
22
use WBW\Library\Ciqual\Model\Table;
23
use WBW\Library\Ciqual\Serializer\XmlDeserializer;
24
use WBW\Library\Provider\Helper\GuzzleHelper;
25
use ZipArchive;
26

27
/**
28
 * XML provider.
29
 *
30
 * @author webeweb <https://github.com/webeweb>
31
 * @package WBW\Library\Ciqual\Provider
32
 */
33
class XmlProvider {
34

35
    /**
36
     * Filename "aliment".
37
     *
38
     * @var string
39
     */
40
    const FILENAME_ALIMENT = "alim_";
41

42
    /**
43
     * Filename "composition".
44
     *
45
     * @var string
46
     */
47
    const FILENAME_COMPOSITION = "compo_";
48

49
    /**
50
     * Filename "constituant".
51
     *
52
     * @var string
53
     */
54
    const FILENAME_CONSTITUANT = "const_";
55

56
    /**
57
     * Filename "groupe aliments".
58
     *
59
     * @var string
60
     */
61
    const FILENAME_GROUPE_ALIMENT = "alim_grp_";
62

63
    /**
64
     * Filename "source".
65
     *
66
     * @var string
67
     */
68
    const FILENAME_SOURCE = "sources_";
69

70
    /**
71
     * Deserialize XML.
72
     *
73
     * @param string $filename The filename.
74
     * @param Table $model The model.
75
     * @return void
76
     */
77
    protected static function deserializeXml(string $filename, Table $model): void {
78

79
        $methods  = static::enumMatches();
8✔
80
        $basename = basename($filename);
8✔
81

82
        $k = null;
8✔
83

84
        foreach (array_keys($methods) as $current) {
8✔
85

86
            if (0 === strpos($basename, $current)) {
8✔
87
                $k = $current;
8✔
88
                break;
8✔
89
            }
90
        }
91

92
        if (null === $k) {
8✔
93
            return;
8✔
94
        }
95

96
        $nsp = str_replace("Provider", "Serializer", __NAMESPACE__);
8✔
97
        $fct = "$nsp\\XmlDeserializer::deserialize$methods[$k]";
8✔
98
        $add = "add$methods[$k]";
8✔
99

100
        $xml = XmlDeserializer::xmlEntities($filename, "windows-1252");
8✔
101

102
        $document = new DOMDocument();
8✔
103
        $document->loadXML($xml);
8✔
104

105
        foreach ($document->documentElement->childNodes as $current) {
8✔
106

107
            if (false === ($current instanceof DOMElement)) {
8✔
108
                continue;
8✔
109
            }
110

111
            $model->$add(call_user_func($fct, $current));
8✔
112
        }
113
    }
2✔
114

115
    /**
116
     * Download a ZIP.
117
     *
118
     * @param string $url The URL.
119
     * @param string $filename The filename.
120
     * @return void
121
     * @throws Throwable Throws an exception if an error occurs.
122
     */
123
    public static function downloadZip(string $url, string $filename): void {
124

125
        $saveTo = GuzzleHelper::getStreamParameterName();
8✔
126
        $stream = fopen($filename, "w");
8✔
127

128
        $client = new Client([
8✔
129
            "headers"     => [
8✔
130
                "Accept"     => "application/zip",
6✔
131
                "User-Agent" => "webeweb/ciqual-library",
6✔
132
            ],
6✔
133
            $saveTo       => $stream,
8✔
134
            "synchronous" => true,
6✔
135
        ]);
6✔
136

137
        $client->request("GET", $url);
8✔
138
    }
2✔
139

140
    /**
141
     * Enumerate the matches.
142
     *
143
     * @return string[] Returns the matches enumeration.
144
     */
145
    protected static function enumMatches(): array {
146

147
        return [
6✔
148
            self::FILENAME_GROUPE_ALIMENT => "GroupeAliments",
8✔
149
            self::FILENAME_ALIMENT        => "Aliment",
8✔
150
            self::FILENAME_COMPOSITION    => "Composition",
8✔
151
            self::FILENAME_CONSTITUANT    => "Constituant",
8✔
152
            self::FILENAME_SOURCE         => "Source",
8✔
153
        ];
6✔
154
    }
155

156
    /**
157
     * Extract a ZIP.
158
     *
159
     * @param string $filename The filename.
160
     * @return Table Returns the table.
161
     * @throws RuntimeException Throws a runtime exception if an error occurs.
162
     */
163
    public static function extractZip(string $filename): Table {
164

165
        $zip = new ZipArchive();
8✔
166
        if (true !== $zip->open($filename)) {
8✔
167
            throw new RuntimeException("Open the ZIP archive $filename failed");
×
168
        }
169

170
        $directory = dirname($filename);
8✔
171

172
        if (false === $zip->extractTo($directory)) {
8✔
173
            throw new RuntimeException("Extract the ZIP archive $filename failed");
×
174
        }
175

176
        $model = new Table();
8✔
177

178
        $stream = opendir($directory);
8✔
179
        while (false !== ($current = readdir($stream))) {
8✔
180

181
            $file = implode(DIRECTORY_SEPARATOR, [$directory, $current]);
8✔
182
            static::deserializeXml($file, $model);
8✔
183
        }
184

185
        return $model;
8✔
186
    }
187
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc