1 <?php
2 /**
3 * @package Joomla.Platform
4 * @subpackage Language
5 *
6 * @copyright Copyright (C) 2005 - 2017 Open Source Matters, Inc. All rights reserved.
7 * @license GNU General Public License version 2 or later; see LICENSE
8 */
9
10 defined('JPATH_PLATFORM') or die;
11
12 /**
13 * Stemmer base class.
14 *
15 * @since 12.1
16 */
17 abstract class JLanguageStemmer
18 {
19 /**
20 * An internal cache of stemmed tokens.
21 *
22 * @var array
23 * @since 12.1
24 */
25 protected $cache = array();
26
27 /**
28 * @var array JLanguageStemmer instances.
29 * @since 12.1
30 */
31 protected static $instances = array();
32
33 /**
34 * Method to get a stemmer, creating it if necessary.
35 *
36 * @param string $adapter The type of stemmer to load.
37 *
38 * @return JLanguageStemmer A JLanguageStemmer instance.
39 *
40 * @since 12.1
41 * @throws RuntimeException on invalid stemmer.
42 */
43 public static function getInstance($adapter)
44 {
45 // Only create one stemmer for each adapter.
46 if (isset(self::$instances[$adapter]))
47 {
48 return self::$instances[$adapter];
49 }
50
51 // Setup the adapter for the stemmer.
52 $class = 'JLanguageStemmer' . ucfirst(trim($adapter));
53
54 // Check if a stemmer exists for the adapter.
55 if (!class_exists($class))
56 {
57 // Throw invalid adapter exception.
58 throw new RuntimeException(JText::sprintf('JLIB_STEMMER_INVALID_STEMMER', $adapter));
59 }
60
61 self::$instances[$adapter] = new $class;
62
63 return self::$instances[$adapter];
64 }
65
66 /**
67 * Method to stem a token and return the root.
68 *
69 * @param string $token The token to stem.
70 * @param string $lang The language of the token.
71 *
72 * @return string The root token.
73 *
74 * @since 12.1
75 */
76 abstract public function stem($token, $lang);
77 }
78