Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
27 / 27
100.00% covered (success)
100.00%
1 / 1
CRAP
100.00% covered (success)
100.00%
1 / 1
ExtendedIniReader
100.00% covered (success)
100.00%
27 / 27
100.00% covered (success)
100.00%
1 / 1
9
100.00% covered (success)
100.00%
1 / 1
 getTextDomain
100.00% covered (success)
100.00%
27 / 27
100.00% covered (success)
100.00%
1 / 1
9
1<?php
2
3/**
4 * Helper class to load .ini files from disk.
5 *
6 * PHP version 8
7 *
8 * Copyright (C) Villanova University 2010.
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2,
12 * as published by the Free Software Foundation.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
22 *
23 * @category VuFind
24 * @package  Translator
25 * @author   Demian Katz <demian.katz@villanova.edu>
26 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
27 * @link     https://vufind.org Main Site
28 */
29
30namespace VuFind\I18n\Translator\Loader;
31
32use Laminas\I18n\Translator\TextDomain;
33
34use function is_array;
35
36/**
37 * Helper class to load .ini files from disk.
38 *
39 * @category VuFind
40 * @package  Translator
41 * @author   Demian Katz <demian.katz@villanova.edu>
42 * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
43 * @link     https://vufind.org Main Site
44 */
45class ExtendedIniReader
46{
47    /**
48     * Parse a language file.
49     *
50     * @param string|array $input         Either a filename to read (passed as a
51     * string) or a set of data to convert into a TextDomain (passed as an array)
52     * @param bool         $convertBlanks Should we convert blank strings to
53     * zero-width non-joiners?
54     *
55     * @return TextDomain
56     */
57    public function getTextDomain($input, $convertBlanks = true)
58    {
59        $data = new TextDomain();
60
61        // Manually parse the language file:
62        $contents = is_array($input) ? $input : file($input);
63        if (is_array($contents)) {
64            foreach ($contents as $current) {
65                // Split the string on the equals sign, keeping a max of two chunks:
66                $parts = explode('=', $current, 2);
67                // Trim off outermost single quotes, if any, from keys (these are
68                // needed by Lokalise in some cases for keys with numeric values)
69                $key = preg_replace(
70                    '/^\'?(.*?)\'?$/',
71                    '$1',
72                    trim($parts[0])
73                );
74                if ($key !== '' && !str_starts_with($key, ';')) {
75                    // Trim outermost matching single or double quotes off the value if present:
76                    if (isset($parts[1])) {
77                        $value = stripslashes(
78                            preg_replace(
79                                '/^(["\'])?(.*?)\1?$/',
80                                '$2',
81                                trim($parts[1])
82                            )
83                        );
84
85                        // Store the key/value pair (allow empty values -- sometimes
86                        // we want to replace a language token with a blank string,
87                        // but Laminas translator doesn't support them so replace
88                        // with a zero-width non-joiner):
89                        if ($convertBlanks && $value === '') {
90                            $value = html_entity_decode(
91                                '&#x200C;',
92                                ENT_NOQUOTES,
93                                'UTF-8'
94                            );
95                        }
96                        $data[$key] = $value;
97                    }
98                }
99            }
100        }
101
102        return $data;
103    }
104}