| 1 |
<?php |
| 2 |
|
| 3 |
namespace Gettext\Languages\Exporter; |
| 4 |
|
| 5 |
class Json extends Exporter |
| 6 |
{ |
| 7 |
/** |
| 8 |
* {@inheritdoc} |
| 9 |
* |
| 10 |
* @see \Gettext\Languages\Exporter\Exporter::getDescription() |
| 11 |
*/ |
| 12 |
public static function getDescription() |
| 13 |
{ |
| 14 |
return 'Build a compressed JSON-encoded file'; |
| 15 |
} |
| 16 |
|
| 17 |
/** |
| 18 |
* Return the options for json_encode. |
| 19 |
* |
| 20 |
* @return int |
| 21 |
*/ |
| 22 |
protected static function getEncodeOptions() |
| 23 |
{ |
| 24 |
$result = 0; |
| 25 |
if (defined('\JSON_UNESCAPED_SLASHES')) { |
| 26 |
$result |= \JSON_UNESCAPED_SLASHES; |
| 27 |
} |
| 28 |
if (defined('\JSON_UNESCAPED_UNICODE')) { |
| 29 |
$result |= \JSON_UNESCAPED_UNICODE; |
| 30 |
} |
| 31 |
|
| 32 |
return $result; |
| 33 |
} |
| 34 |
|
| 35 |
/** |
| 36 |
* {@inheritdoc} |
| 37 |
* |
| 38 |
* @see \Gettext\Languages\Exporter\Exporter::toStringDo() |
| 39 |
*/ |
| 40 |
protected static function toStringDo($languages) |
| 41 |
{ |
| 42 |
$list = array(); |
| 43 |
foreach ($languages as $language) { |
| 44 |
$item = array(); |
| 45 |
$item['name'] = $language->name; |
| 46 |
if (isset($language->supersededBy)) { |
| 47 |
$item['supersededBy'] = $language->supersededBy; |
| 48 |
} |
| 49 |
if (isset($language->script)) { |
| 50 |
$item['script'] = $language->script; |
| 51 |
} |
| 52 |
if (isset($language->territory)) { |
| 53 |
$item['territory'] = $language->territory; |
| 54 |
} |
| 55 |
if (isset($language->baseLanguage)) { |
| 56 |
$item['baseLanguage'] = $language->baseLanguage; |
| 57 |
} |
| 58 |
$item['formula'] = $language->formula; |
| 59 |
$item['plurals'] = count($language->categories); |
| 60 |
$item['cases'] = array(); |
| 61 |
$item['examples'] = array(); |
| 62 |
foreach ($language->categories as $category) { |
| 63 |
$item['cases'][] = $category->id; |
| 64 |
$item['examples'][$category->id] = $category->examples; |
| 65 |
} |
| 66 |
$list[$language->id] = $item; |
| 67 |
} |
| 68 |
|
| 69 |
return json_encode($list, static::getEncodeOptions()); |
| 70 |
} |
| 71 |
} |
| 72 |
|