Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
37 / 37
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
Json
100.00% covered (success)
100.00%
37 / 37
100.00% covered (success)
100.00%
2 / 2
20
100.00% covered (success)
100.00%
1 / 1
 createFile
100.00% covered (success)
100.00%
14 / 14
100.00% covered (success)
100.00%
1 / 1
10
 createFragment
100.00% covered (success)
100.00%
23 / 23
100.00% covered (success)
100.00%
1 / 1
10
1<?php
2declare(strict_types=1);
3/**
4 * Pop PHP Framework (https://www.popphp.org/)
5 *
6 * @link       https://github.com/popphp/popphp-framework
7 * @author     Nick Sagona, III <dev@noladev.com>
8 * @copyright  Copyright (c) 2009-2027 NOLA Interactive, LLC.
9 * @license    https://www.popphp.org/license     New BSD License
10 */
11
12/**
13 * @namespace
14 */
15namespace Pop\I18n\Format;
16
17/**
18 * I18n JSON format class
19 *
20 * @category   Pop
21 * @package    Pop_I18n
22 * @author     Nick Sagona, III <dev@noladev.com>
23 * @copyright  Copyright (c) 2009-2027 NOLA Interactive, LLC.
24 * @license    https://www.popphp.org/license     New BSD License
25 * @version    4.1.0
26 */
27class Json
28{
29
30    /**
31     * Create an XML language file from an array of data.
32     * The format of the parameters should be as follows:
33     *
34     * $lang = [
35     *     'src'    => 'en',
36     *     'output' => 'de',
37     *     'name'   => 'German',
38     *     'native' => 'Deutsch'
39     * ];
40     *
41     * $locales = [
42     *     [
43     *         'region' => 'DE',
44     *         'name'   => 'Germany',
45     *         'native' => 'Deutschland',
46     *         'text' => [
47     *             [
48     *                 'source' => 'This field is required.',
49     *                 'output' => 'Dieses Feld ist erforderlich.'
50     *             ], ...
51     *         ]
52     *     ], ...
53     * ];
54     *
55     * @param  array  $lang
56     * @param  array  $locales
57     * @param  string $file
58     * @throws Exception
59     * @return void
60     */
61    public static function createFile(array $lang, array $locales, string $file): void
62    {
63        // Validate the $lang parameter
64        if (!isset($lang['src']) || !isset($lang['output'])) {
65            throw new Exception("Error: The language parameter must have at least the 'src' and 'output' keys defined.");
66        }
67
68        // Validate the $locales parameter
69        foreach ($locales as $locale) {
70            if (!isset($locale['region'])) {
71                throw new Exception("Error: The locales parameter must have at least the 'region' key defined in each locale.");
72            }
73            if (!isset($locale['text'])) {
74                throw new Exception("Error: The locales parameter must have at least the 'text' key defined in each locale.");
75            }
76            if (!is_array($locale['text'])) {
77                throw new Exception("Error: The parameter key 'text' in each locale must be an array.");
78            }
79            foreach ($locale['text'] as $text) {
80                if (!isset($text['source']) || !isset($text['output'])) {
81                    throw new Exception("Error: The 'source' and 'output' keys must be defined in each 'text' array.");
82                }
83            }
84        }
85
86        $lang['locale'] = $locales;
87
88        // Save JSON file
89        file_put_contents($file, json_encode(['language' => $lang], JSON_PRETTY_PRINT));
90    }
91
92    /**
93     * Create an language file fragment from a source file and an output file,
94     * each entry separated by a new line
95     *
96     * @param  string  $source
97     * @param  string  $output
98     * @param  ?string $target
99     * @throws Exception
100     * @return void
101     */
102    public static function createFragment(string $source, string $output, ?string $target = null): void
103    {
104        if (!file_exists($source)) {
105            throw new Exception('Error: The source file does not exist.');
106        }
107        if (!file_exists($output)) {
108            throw new Exception('Error: The output file does not exist.');
109        }
110
111        $sourceLines = explode(PHP_EOL, file_get_contents($source));
112        $outputLines = explode(PHP_EOL, file_get_contents($output));
113
114        $targetDir = ($target !== null) ? $target : dirname($output);
115
116        if (!file_exists($targetDir)) {
117            throw new Exception('Error: The target directory does not exist.');
118        }
119
120        if (str_contains($output, '/')) {
121            $lang = substr($output, (strrpos($output, '/') + 1));
122            $lang = substr($lang, 0, strpos($lang, '.'));
123        } else if (str_contains($output, "\\")) {
124            $lang = substr($output, (strrpos($output, "\\") + 1));
125            $lang = substr($lang, 0, strpos($lang, '.'));
126        } else {
127            $lang = substr($output, 0, strpos($output, '.'));
128        }
129
130        $json = '                "text"   : [' . PHP_EOL;
131
132        foreach ($outputLines as $key => $value) {
133            if (!empty($value) && !empty($sourceLines[$key])) {
134                $json .= '                    {' . PHP_EOL . '                        "source" : ' . json_encode($sourceLines[$key]) . ',' . PHP_EOL .
135                    '                        "output" : ' . json_encode($value) . PHP_EOL . '                    },' . PHP_EOL;
136            }
137        }
138
139        $json .= '                ]' . PHP_EOL;
140
141        file_put_contents($targetDir . DIRECTORY_SEPARATOR . $lang . '.json', $json);
142    }
143
144}