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