1<?php
2
3namespace Gettext\Languages\Exporter;
4
5use Exception;
6
7/**
8 * Base class for all the exporters.
9 */
10abstract 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     *
88     * @return string
89     */
90    final public static function toString($languages, $options = null)
91    {
92        if (isset($options) && is_array($options)) {
93            if (isset($options['us-ascii']) && $options['us-ascii']) {
94                $asciiList = array();
95                foreach ($languages as $language) {
96                    $asciiList[] = $language->getUSAsciiClone();
97                }
98                $languages = $asciiList;
99            }
100        }
101
102        return static::toStringDo($languages);
103    }
104
105    /**
106     * Save the Language instances to a file.
107     *
108     * @param \Gettext\Languages\Language[] $languages the Language instances to convert
109     *
110     * @throws \Exception
111     */
112    final public static function toFile($languages, $filename, $options = null)
113    {
114        $data = self::toString($languages, $options);
115        if (@file_put_contents($filename, $data) === false) {
116            throw new Exception("Error writing data to '${filename}'");
117        }
118    }
119
120    /**
121     * Is this exporter for public use?
122     *
123     * @return bool
124     */
125    public static function isForPublicUse()
126    {
127        return true;
128    }
129
130    /**
131     * Return a short description of the exporter.
132     *
133     * @return string
134     */
135    public static function getDescription()
136    {
137        throw new Exception(get_called_class() . ' does not implement the method ' . __FUNCTION__);
138    }
139
140    /**
141     * Convert a list of Language instances to string.
142     *
143     * @param \Gettext\Languages\Language[] $languages the Language instances to convert
144     *
145     * @return string
146     */
147    protected static function toStringDo($languages)
148    {
149        throw new Exception(get_called_class() . ' does not implement the method ' . __FUNCTION__);
150    }
151}
152