summaryrefslogtreecommitdiffstats
path: root/modules/translation/library/Translation/Cli/TranslationCommand.php
blob: af3582c4550de5e8fd58f74c0f17b57043341b85 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
<?php
/* Icinga Web 2 | (c) 2014 Icinga Development Team | GPLv2+ */

namespace Icinga\Module\Translation\Cli;

use Exception;
use Icinga\Cli\Command;
use Icinga\Exception\IcingaException;
use Icinga\Module\Translation\Util\GettextTranslationHelper;

/**
 * Base class for translation commands
 */
class TranslationCommand extends Command
{
    /**
     * Get the gettext translation helper
     *
     * @param   string $locale
     *
     * @return  GettextTranslationHelper
     */
    public function getTranslationHelper($locale)
    {
        $helper = new GettextTranslationHelper($this->app, $locale);
        $helper->setConfig($this->Config());
        return $helper;
    }

    /**
     * Check whether the given locale code is valid
     *
     * @param   string  $code   The locale code to validate
     *
     * @return  string          The validated locale code
     *
     * @throws  Exception       In case the locale code is invalid
     */
    public function validateLocaleCode($code)
    {
        if (! preg_match('@[a-z]{2}_[A-Z]{2}@', $code)) {
            throw new IcingaException(
                'Locale code \'%s\' is not valid. Expected format is: ll_CC',
                $code
            );
        }

        return $code;
    }

    /**
     * Check whether the given module is available and enabled
     *
     * @param   string  $name   The module name to validate
     *
     * @return  string          The validated module name
     *
     * @throws  Exception       In case the given module is not available or not enabled
     */
    public function validateModuleName($name)
    {
        $enabledModules = $this->app->getModuleManager()->listEnabledModules();

        if (! in_array($name, $enabledModules)) {
            throw new IcingaException(
                'Module with name \'%s\' not found or is not enabled',
                $name
            );
        }

        return $name;
    }
}