Refactoring

This commit is contained in:
gamonoid
2017-09-03 20:39:22 +02:00
parent af40881847
commit a7274d3cfd
5075 changed files with 238202 additions and 16291 deletions

View File

@@ -0,0 +1,55 @@
<?php
namespace Gettext\Languages\Exporter;
class Docs extends Html
{
/**
* @see Exporter::toStringDo
*/
protected static function toStringDo($languages)
{
$result = <<<EOT
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="author" content="Michele Locati">
<title>gettext plural rules - built from CLDR</title>
<meta name="description" content="List of all language rules for gettext .po files automatically generated from the Unicode CLDR data" />
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css">
<link rel="stylesheet" href="style.css">
</head>
<body>
<a href="https://github.com/mlocati/cldr-to-gettext-plural-rules" class="hidden-xs"><img style="position: fixed; top: 0; right: 0; border: 0; z-index: 2000" src="https://s3.amazonaws.com/github/ribbons/forkme_right_red_aa0000.png" alt="Fork me on GitHub"></a>
<div class="container-fluid">
EOT;
$result .= static::buildTable($languages, true);
$result .= <<<EOT
</div>
<script src="http://code.jquery.com/jquery-1.11.2.min.js"></script>
<script src="script.js"></script>
</body>
</html>
EOT;
return $result;
}
/**
* @see Exporter::isForPublicUse
*/
public static function isForPublicUse()
{
return false;
}
/**
* @see Exporter::getDescription
*/
public static function getDescription()
{
return 'Build the page http://mlocati.github.io/cldr-to-gettext-plural-rules';
}
}

View File

@@ -0,0 +1,128 @@
<?php
namespace Gettext\Languages\Exporter;
use Exception;
use Gettext\Languages\Language;
/**
* Base class for all the exporters.
*/
abstract class Exporter
{
/**
* @var array
*/
private static $exporters;
/**
* Return the list of all the available exporters. Keys are the exporter handles, values are the exporter class names.
* @param bool $onlyForPublicUse If true, internal exporters will be omitted.
* @return string[]
*/
final public static function getExporters($onlyForPublicUse = false)
{
if (!isset(self::$exporters)) {
$exporters = array();
$m = null;
foreach (scandir(__DIR__) as $f) {
if (preg_match('/^(\w+)\.php$/', $f, $m)) {
if ($f !== basename(__FILE__)) {
$exporters[strtolower($m[1])] = $m[1];
}
}
}
self::$exporters = $exporters;
}
if ($onlyForPublicUse) {
$result = array();
foreach (self::$exporters as $handle => $class) {
if (call_user_func(self::getExporterClassName($handle).'::isForPublicUse') === true) {
$result[$handle] = $class;
}
}
} else {
$result = self::$exporters;
}
return $result;
}
/**
* Return the description of a specific exporter.
* @param string $exporterHandle The handle of the exporter.
* @throws Exception Throws an Exception if $exporterHandle is not valid.
* @return string
*/
final public static function getExporterDescription($exporterHandle)
{
$exporters = self::getExporters();
if (!isset($exporters[$exporterHandle])) {
throw new Exception("Invalid exporter handle: '$exporterHandle'");
}
return call_user_func(self::getExporterClassName($exporterHandle).'::getDescription');
}
/**
* Returns the fully qualified class name of a exporter given its handle.
* @param string $exporterHandle The exporter class handle.
* @return string
*/
final public static function getExporterClassName($exporterHandle)
{
return __NAMESPACE__.'\\'.ucfirst(strtolower($exporterHandle));
}
/**
* Convert a list of Language instances to string.
* @param Language[] $languages The Language instances to convert.
* @return string
*/
protected static function toStringDo($languages)
{
throw new Exception(get_called_class().' does not implement the method '.__FUNCTION__);
}
/**
* Convert a list of Language instances to string.
* @param Language[] $languages The Language instances to convert.
* @return string
*/
final public static function toString($languages, $options = null)
{
if (isset($options) && is_array($options)) {
if (isset($options['us-ascii']) && $options['us-ascii']) {
$asciiList = array();
foreach ($languages as $language) {
$asciiList[] = $language->getUSAsciiClone();
}
$languages = $asciiList;
}
}
return static::toStringDo($languages);
}
/**
* Save the Language instances to a file.
* @param Language[] $languages The Language instances to convert.
* @throws Exception
*/
final public static function toFile($languages, $filename, $options = null)
{
$data = self::toString($languages, $options);
if (@file_put_contents($filename, $data) === false) {
throw new Exception("Error writing data to '$filename'");
}
}
/**
* Is this exporter for public use?
* @return bool
*/
public static function isForPublicUse()
{
return true;
}
/**
* Return a short description of the exporter.
* @return string
*/
public static function getDescription()
{
throw new Exception(get_called_class().' does not implement the method '.__FUNCTION__);
}
}

View File

@@ -0,0 +1,61 @@
<?php
namespace Gettext\Languages\Exporter;
class Html extends Exporter
{
/**
* @see Exporter::toStringDo
*/
protected static function toStringDo($languages)
{
return self::buildTable($languages, false);
}
protected static function h($str)
{
return htmlspecialchars($str, ENT_COMPAT, 'UTF-8');
}
protected static function buildTable($languages, $forDocs)
{
$prefix = $forDocs ? ' ' : '';
$lines = array();
$lines[] = $prefix.'<table'.($forDocs ? ' class="table table-bordered table-condensed table-striped"' : '').'>';
$lines[] = $prefix.' <thead>';
$lines[] = $prefix.' <tr>';
$lines[] = $prefix.' <th>Language code</th>';
$lines[] = $prefix.' <th>Language name</th>';
$lines[] = $prefix.' <th># plurals</th>';
$lines[] = $prefix.' <th>Formula</th>';
$lines[] = $prefix.' <th>Plurals</th>';
$lines[] = $prefix.' </tr>';
$lines[] = $prefix.' </thead>';
$lines[] = $prefix.' <tbody>';
foreach ($languages as $lc) {
$lines[] = $prefix.' <tr>';
$lines[] = $prefix.' <td>'.$lc->id.'</td>';
$name = self::h($lc->name);
if (isset($lc->supersededBy)) {
$name .= '<br /><small><span>Superseded by</span> '.$lc->supersededBy.'</small>';
}
$lines[] = $prefix.' <td>'.$name.'</td>';
$lines[] = $prefix.' <td>'.count($lc->categories).'</td>';
$lines[] = $prefix.' <td>'.self::h($lc->formula).'</td>';
$cases = array();
foreach ($lc->categories as $c) {
$cases[] = '<li><span>'.$c->id.'</span><code>'.self::h($c->examples).'</code></li>';
}
$lines[] = $prefix.' <td><ol'.($forDocs ? ' class="cases"' : '').' start="0">'.implode('', $cases).'</ol></td>';
$lines[] = $prefix.' </tr>';
}
$lines[] = $prefix.' </tbody>';
$lines[] = $prefix.'</table>';
return implode("\n", $lines);
}
/**
* @see Exporter::getDescription
*/
public static function getDescription()
{
return 'Build a HTML table';
}
}

View File

@@ -0,0 +1,63 @@
<?php
namespace Gettext\Languages\Exporter;
class Json extends Exporter
{
/**
* Return the options for json_encode.
* @return int
*/
protected static function getEncodeOptions()
{
$result = 0;
if (defined('\JSON_UNESCAPED_SLASHES')) {
$result |= \JSON_UNESCAPED_SLASHES;
}
if (defined('\JSON_UNESCAPED_UNICODE')) {
$result |= \JSON_UNESCAPED_UNICODE;
}
return $result;
}
/**
* @see Exporter::toStringDo
*/
protected static function toStringDo($languages)
{
$list = array();
foreach ($languages as $language) {
$item = array();
$item['name'] = $language->name;
if (isset($language->supersededBy)) {
$item['supersededBy'] = $language->supersededBy;
}
if (isset($language->script)) {
$item['script'] = $language->script;
}
if (isset($language->territory)) {
$item['territory'] = $language->territory;
}
if (isset($language->baseLanguage)) {
$item['baseLanguage'] = $language->baseLanguage;
}
$item['formula'] = $language->formula;
$item['plurals'] = count($language->categories);
$item['cases'] = array();
$item['examples'] = array();
foreach ($language->categories as $category) {
$item['cases'][] = $category->id;
$item['examples'][$category->id] = $category->examples;
}
$list[$language->id] = $item;
}
return json_encode($list, static::getEncodeOptions());
}
/**
* @see Exporter::getDescription
*/
public static function getDescription()
{
return 'Build a compressed JSON-encoded file';
}
}

View File

@@ -0,0 +1,55 @@
<?php
namespace Gettext\Languages\Exporter;
class Php extends Exporter
{
/**
* @see Exporter::toStringDo
*/
protected static function toStringDo($languages)
{
$lines = array();
$lines[] = '<?php';
$lines[] = 'return array(';
foreach ($languages as $lc) {
$lines[] = ' \''.$lc->id.'\' => array(';
$lines[] = ' \'name\' => \''.addslashes($lc->name).'\',';
if (isset($lc->supersededBy)) {
$lines[] = ' \'supersededBy\' => \''.$lc->supersededBy.'\',';
}
if (isset($lc->script)) {
$lines[] = ' \'script\' => \''.addslashes($lc->script).'\',';
}
if (isset($lc->territory)) {
$lines[] = ' \'territory\' => \''.addslashes($lc->territory).'\',';
}
if (isset($lc->baseLanguage)) {
$lines[] = ' \'baseLanguage\' => \''.addslashes($lc->baseLanguage).'\',';
}
$lines[] = ' \'formula\' => \''.$lc->formula.'\',';
$lines[] = ' \'plurals\' => '.count($lc->categories).',';
$catNames = array();
foreach ($lc->categories as $c) {
$catNames[] = "'{$c->id}'";
}
$lines[] = ' \'cases\' => array('.implode(', ', $catNames).'),';
$lines[] = ' \'examples\' => array(';
foreach ($lc->categories as $c) {
$lines[] = ' \''.$c->id.'\' => \''.$c->examples.'\',';
}
$lines[] = ' ),';
$lines[] = ' ),';
}
$lines[] = ');';
$lines[] = '';
return implode("\n", $lines);
}
/**
* @see Exporter::getDescription
*/
public static function getDescription()
{
return 'Build a PHP array';
}
}

View File

@@ -0,0 +1,31 @@
<?php
namespace Gettext\Languages\Exporter;
use Exception;
class Po extends Exporter
{
/**
* @see Exporter::toStringDo
*/
protected static function toStringDo($languages)
{
if (count($languages) !== 1) {
throw new Exception('The '.get_called_class().' exporter can only export one language');
}
$language = $languages[0];
$lines = array();
$lines[] = '"Language: '.$language->id.'\n"';
$lines[] = '"Plural-Forms: nplurals='.count($language->categories).'; plural='.$language->formula.'\n"';
$lines[] = '';
return implode("\n", $lines);
}
/**
* @see Exporter::getDescription
*/
public static function getDescription()
{
return 'Build a string to be used for gettext .po files';
}
}

View File

@@ -0,0 +1,25 @@
<?php
namespace Gettext\Languages\Exporter;
use Exception;
class Prettyjson extends Json
{
/**
* @see Json::getEncodeOptions
*/
protected static function getEncodeOptions()
{
if (!(defined('\JSON_PRETTY_PRINT') && defined('\JSON_UNESCAPED_SLASHES') && defined('\JSON_UNESCAPED_UNICODE'))) {
throw new Exception('PHP 5.4 or later is required to export uncompressed JSON');
}
return \JSON_PRETTY_PRINT | \JSON_UNESCAPED_SLASHES | \JSON_UNESCAPED_UNICODE;
}
/**
* @see Exporter::getDescription
*/
public static function getDescription()
{
return 'Build an uncompressed JSON-encoded file (PHP 5.4 or later is needed)';
}
}

View File

@@ -0,0 +1,54 @@
<?php
namespace Gettext\Languages\Exporter;
class Xml extends Exporter
{
/**
* @see Exporter::toStringDo
*/
protected static function toStringDo($languages)
{
$xml = new \DOMDocument('1.0', 'UTF-8');
$xml->loadXML('<languages
xmlns="https://github.com/mlocati/cldr-to-gettext-plural-rules"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://github.com/mlocati/cldr-to-gettext-plural-rules http://mlocati.github.io/cldr-to-gettext-plural-rules/GettextLanguages.xsd"
/>');
$xLanguages = $xml->firstChild;
foreach ($languages as $language) {
$xLanguage = $xml->createElement('language');
$xLanguage->setAttribute('id', $language->id);
$xLanguage->setAttribute('name', $language->name);
if (isset($language->supersededBy)) {
$xLanguage->setAttribute('supersededBy', $language->supersededBy);
}
if (isset($language->script)) {
$xLanguage->setAttribute('script', $language->script);
}
if (isset($language->territory)) {
$xLanguage->setAttribute('territory', $language->territory);
}
if (isset($language->baseLanguage)) {
$xLanguage->setAttribute('baseLanguage', $language->baseLanguage);
}
$xLanguage->setAttribute('formula', $language->formula);
foreach ($language->categories as $category) {
$xCategory = $xml->createElement('category');
$xCategory->setAttribute('id', $category->id);
$xCategory->setAttribute('examples', $category->examples);
$xLanguage->appendChild($xCategory);
}
$xLanguages->appendChild($xLanguage);
}
$xml->formatOutput = true;
return $xml->saveXML();
}
/**
* @see Exporter::getDescription
*/
public static function getDescription()
{
return 'Build an XML file - schema available at http://mlocati.github.io/cldr-to-gettext-plural-rules/GettextLanguages.xsd';
}
}