| 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 |
* {@inheritdoc} |
| 19 |
* |
| 20 |
* @see \Gettext\Languages\Exporter\Exporter::supportsFormulasWithAndWithoutParenthesis() |
| 21 |
*/ |
| 22 |
public static function supportsFormulasWithAndWithoutParenthesis() |
| 23 |
{ |
| 24 |
return true; |
| 25 |
} |
| 26 |
|
| 27 |
/** |
| 28 |
* Return the options for json_encode. |
| 29 |
* |
| 30 |
* @return int |
| 31 |
*/ |
| 32 |
protected static function getEncodeOptions() |
| 33 |
{ |
| 34 |
$result = 0; |
| 35 |
if (defined('\JSON_UNESCAPED_SLASHES')) { |
| 36 |
$result |= \JSON_UNESCAPED_SLASHES; |
| 37 |
} |
| 38 |
if (defined('\JSON_UNESCAPED_UNICODE')) { |
| 39 |
$result |= \JSON_UNESCAPED_UNICODE; |
| 40 |
} |
| 41 |
|
| 42 |
return $result; |
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
* {@inheritdoc} |
| 47 |
* |
| 48 |
* @see \Gettext\Languages\Exporter\Exporter::toStringDoWithOptions() |
| 49 |
*/ |
| 50 |
protected static function toStringDoWithOptions($languages, array $options) |
| 51 |
{ |
| 52 |
$list = array(); |
| 53 |
foreach ($languages as $language) { |
| 54 |
$item = array(); |
| 55 |
$item['name'] = $language->name; |
| 56 |
if (isset($language->supersededBy)) { |
| 57 |
$item['supersededBy'] = $language->supersededBy; |
| 58 |
} |
| 59 |
if (isset($language->script)) { |
| 60 |
$item['script'] = $language->script; |
| 61 |
} |
| 62 |
if (isset($language->territory)) { |
| 63 |
$item['territory'] = $language->territory; |
| 64 |
} |
| 65 |
if (isset($language->baseLanguage)) { |
| 66 |
$item['baseLanguage'] = $language->baseLanguage; |
| 67 |
} |
| 68 |
if (!empty($options['both-formulas'])) { |
| 69 |
$item['formulas'] = array( |
| 70 |
'standard' => $language->buildFormula(true), |
| 71 |
'php' => $language->formula, |
| 72 |
); |
| 73 |
} else { |
| 74 |
$item['formula'] = $language->formula; |
| 75 |
} |
| 76 |
$item['plurals'] = count($language->categories); |
| 77 |
$item['cases'] = array(); |
| 78 |
$item['examples'] = array(); |
| 79 |
foreach ($language->categories as $category) { |
| 80 |
$item['cases'][] = $category->id; |
| 81 |
$item['examples'][$category->id] = $category->examples; |
| 82 |
} |
| 83 |
$list[$language->id] = $item; |
| 84 |
} |
| 85 |
|
| 86 |
return json_encode($list, static::getEncodeOptions()); |
| 87 |
} |
| 88 |
} |
| 89 |
|