1<?php
2/**
3 * Copyright 2012-2017 Horde LLC (http://www.horde.org/)
4 *
5 * See the enclosed file COPYING for license information (LGPL). If you
6 * did not receive this file, see http://www.horde.org/licenses/lgpl21.
7 *
8 * @category Horde
9 * @license  http://www.horde.org/licenses/lgpl21 LGPL 2.1
10 * @package  Core
11 */
12
13/**
14 * A Horde_Injector based spellchecker factory.
15 *
16 * @author   Michael Slusarz <slusarz@horde.org>
17 * @category Horde
18 * @license  http://www.horde.org/licenses/lgpl21 LGPL 2.1
19 * @package  Core
20 * @since    2.1.0
21 */
22class Horde_Core_Factory_SpellChecker extends Horde_Core_Factory_Base
23{
24    /**
25     * Returns the spellchecker instance.
26     *
27     * @param array $args    Configuration arguments to override the
28     *                       defaults.
29     * @param string $input  Input text.  If set, allows language detection
30     *                       if not automatically set.
31     *
32     * @return Horde_SpellChecker  The spellchecker instance.
33     * @throws Horde_Exception
34     */
35    public function create(array $args = array(), $input = null)
36    {
37        global $conf, $language, $registry;
38
39        if (empty($conf['spell']['driver'])) {
40            throw new Horde_Exception('No spellcheck driver configured.');
41        }
42
43        $args = array_merge(
44            array('localDict' => array()),
45            Horde::getDriverConfig('spell', null),
46            $args
47        );
48
49        if (empty($args['locale'])) {
50            if (!is_null($input)) {
51                try {
52                    $args['locale'] = $this->_injector->getInstance('Horde_Core_Factory_LanguageDetect')->getLanguageCode($input);
53                } catch (Horde_Exception $e) {}
54            }
55
56            if (empty($args['locale']) && isset($language)) {
57                $args['locale'] = $language;
58            }
59        }
60
61        /* Add local dictionary words. */
62        try {
63            $args['localDict'] = array_merge(
64                $args['localDict'],
65                $registry->loadConfigFile('spelling.php', 'ignore_list', 'horde')->config['ignore_list']
66            );
67        } catch (Horde_Exception $e) {}
68
69        $classname  = 'Horde_SpellChecker_' . Horde_String::ucfirst(basename($conf['spell']['driver']));
70        if (!class_exists($classname)) {
71            throw new Horde_Exception('Spellcheck driver does not exist.');
72        }
73
74        return new $classname($args);
75    }
76
77}
78