| 1 |
<?php |
| 2 |
|
| 3 |
namespace Gettext\Languages\Exporter; |
| 4 |
|
| 5 |
use Exception; |
| 6 |
|
| 7 |
/** |
| 8 |
* Base class for all the exporters. |
| 9 |
*/ |
| 10 |
abstract class Exporter |
| 11 |
{ |
| 12 |
/** |
| 13 |
* @var array |
| 14 |
*/ |
| 15 |
private static $exporters; |
| 16 |
|
| 17 |
/** |
| 18 |
* Return the list of all the available exporters. Keys are the exporter handles, values are the exporter class names. |
| 19 |
* |
| 20 |
* @param bool $onlyForPublicUse if true, internal exporters will be omitted |
| 21 |
* |
| 22 |
* @return string[] |
| 23 |
*/ |
| 24 |
final public static function getExporters($onlyForPublicUse = false) |
| 25 |
{ |
| 26 |
if (!isset(self::$exporters)) { |
| 27 |
$exporters = array(); |
| 28 |
$m = null; |
| 29 |
foreach (scandir(__DIR__) as $f) { |
| 30 |
if (preg_match('/^(\w+)\.php$/', $f, $m)) { |
| 31 |
if ($f !== basename(__FILE__)) { |
| 32 |
$exporters[strtolower($m[1])] = $m[1]; |
| 33 |
} |
| 34 |
} |
| 35 |
} |
| 36 |
self::$exporters = $exporters; |
| 37 |
} |
| 38 |
if ($onlyForPublicUse) { |
| 39 |
$result = array(); |
| 40 |
foreach (self::$exporters as $handle => $class) { |
| 41 |
if (call_user_func(self::getExporterClassName($handle) . '::isForPublicUse') === true) { |
| 42 |
$result[$handle] = $class; |
| 43 |
} |
| 44 |
} |
| 45 |
} else { |
| 46 |
$result = self::$exporters; |
| 47 |
} |
| 48 |
|
| 49 |
return $result; |
| 50 |
} |
| 51 |
|
| 52 |
/** |
| 53 |
* Return the description of a specific exporter. |
| 54 |
* |
| 55 |
* @param string $exporterHandle the handle of the exporter |
| 56 |
* |
| 57 |
* @throws \Exception throws an Exception if $exporterHandle is not valid |
| 58 |
* |
| 59 |
* @return string |
| 60 |
*/ |
| 61 |
final public static function getExporterDescription($exporterHandle) |
| 62 |
{ |
| 63 |
$exporters = self::getExporters(); |
| 64 |
if (!isset($exporters[$exporterHandle])) { |
| 65 |
throw new Exception("Invalid exporter handle: '{$exporterHandle}'"); |
| 66 |
} |
| 67 |
|
| 68 |
return call_user_func(self::getExporterClassName($exporterHandle) . '::getDescription'); |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* Returns the fully qualified class name of a exporter given its handle. |
| 73 |
* |
| 74 |
* @param string $exporterHandle the exporter class handle |
| 75 |
* |
| 76 |
* @return string |
| 77 |
*/ |
| 78 |
final public static function getExporterClassName($exporterHandle) |
| 79 |
{ |
| 80 |
return __NAMESPACE__ . '\\' . ucfirst(strtolower($exporterHandle)); |
| 81 |
} |
| 82 |
|
| 83 |
/** |
| 84 |
* Convert a list of Language instances to string. |
| 85 |
* |
| 86 |
* @param \Gettext\Languages\Language[] $languages the Language instances to convert |
| 87 |
* @param array|null $options |
| 88 |
* |
| 89 |
* @return string |
| 90 |
*/ |
| 91 |
final public static function toString($languages, $options = null) |
| 92 |
{ |
| 93 |
if (isset($options) && is_array($options)) { |
| 94 |
if (isset($options['us-ascii']) && $options['us-ascii']) { |
| 95 |
$asciiList = array(); |
| 96 |
foreach ($languages as $language) { |
| 97 |
$asciiList[] = $language->getUSAsciiClone(); |
| 98 |
} |
| 99 |
$languages = $asciiList; |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
return static::toStringDo($languages); |
| 104 |
} |
| 105 |
|
| 106 |
/** |
| 107 |
* Save the Language instances to a file. |
| 108 |
* |
| 109 |
* @param \Gettext\Languages\Language[] $languages the Language instances to convert |
| 110 |
* @param array|null $options |
| 111 |
* |
| 112 |
* @throws \Exception |
| 113 |
*/ |
| 114 |
final public static function toFile($languages, $filename, $options = null) |
| 115 |
{ |
| 116 |
$data = self::toString($languages, $options); |
| 117 |
if (@file_put_contents($filename, $data) === false) { |
| 118 |
throw new Exception("Error writing data to '{$filename}'"); |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
/** |
| 123 |
* Is this exporter for public use? |
| 124 |
* |
| 125 |
* @return bool |
| 126 |
*/ |
| 127 |
public static function isForPublicUse() |
| 128 |
{ |
| 129 |
return true; |
| 130 |
} |
| 131 |
|
| 132 |
/** |
| 133 |
* Return a short description of the exporter. |
| 134 |
* |
| 135 |
* @return string |
| 136 |
*/ |
| 137 |
public static function getDescription() |
| 138 |
{ |
| 139 |
throw new Exception(get_called_class() . ' does not implement the method ' . __FUNCTION__); |
| 140 |
} |
| 141 |
|
| 142 |
/** |
| 143 |
* Convert a list of Language instances to string. |
| 144 |
* |
| 145 |
* @param \Gettext\Languages\Language[] $languages the Language instances to convert |
| 146 |
* |
| 147 |
* @return string |
| 148 |
*/ |
| 149 |
protected static function toStringDo($languages) |
| 150 |
{ |
| 151 |
throw new Exception(get_called_class() . ' does not implement the method ' . __FUNCTION__); |
| 152 |
} |
| 153 |
} |
| 154 |
|