Completed
Push — 3.5 ( 1a9180...1bec8a )
by Daniel
24s
created

SS_Transliterator::useStrTr()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 16
nc 1
nop 1
dl 0
loc 19
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Support class for converting unicode strings into a suitable 7-bit ASCII equivalent.
5
 *
6
 * Usage:
7
 *
8
 * <code>
9
 * $tr = new SS_Transliterator();
10
 * $ascii = $tr->toASCII($unicode);
11
 * </code>
12
 *
13
 * @package framework
14
 * @subpackage model
15
 */
16
class SS_Transliterator extends Object {
17
	/**
18
	 * @config
19
	 * @var boolean Allow the use of iconv() to perform transliteration.  Set to false to disable.
20
	 * Even if this variable is true, iconv() won't be used if it's not installed.
21
	 */
22
	private static $use_iconv = false;
23
24
	/**
25
	 * Convert the given utf8 string to a safe ASCII source
26
	 */
27
	public function toASCII($source) {
28
		if(function_exists('iconv') && $this->config()->use_iconv) return $this->useIconv($source);
29
		else return $this->useStrTr($source);
30
	}
31
32
	/**
33
	 * Transliteration using strtr() and a lookup table
34
	 */
35
	protected function useStrTr($source) {
36
		$table = array(
37
			'Š'=>'S', 'š'=>'s', 'Đ'=>'Dj', 'đ'=>'dj', 'Ž'=>'Z', 'ž'=>'z', 'Č'=>'C', 'č'=>'c', 'Ć'=>'C', 'ć'=>'c',
38
			'À'=>'A', 'Á'=>'A', 'Â'=>'A', 'Ã'=>'A', 'Ä'=>'Ae', 'Å'=>'A', 'Æ'=>'A', 'Ç'=>'C', 'È'=>'E', 'É'=>'E',
39
			'Ê'=>'E', 'Ë'=>'E', 'Ì'=>'I', 'Í'=>'I', 'Î'=>'I', 'Ï'=>'I', 'Ñ'=>'N', 'Ò'=>'O', 'Ó'=>'O', 'Ô'=>'O',
40
			'Õ'=>'O', 'Ö'=>'Oe', 'Ø'=>'O', 'Ù'=>'U', 'Ú'=>'U', 'Û'=>'U', 'Ü'=>'Ue', 'Ý'=>'Y', 'Þ'=>'B', 'ß'=>'ss',
41
			'à'=>'a', 'á'=>'a', 'â'=>'a', 'ã'=>'a', 'ä'=>'ae', 'å'=>'a', 'æ'=>'ae', 'ç'=>'c', 'è'=>'e', 'é'=>'e',
42
			'ê'=>'e', 'ë'=>'e', 'ì'=>'i', 'í'=>'i', 'î'=>'i', 'ï'=>'i', 'ð'=>'o', 'ñ'=>'n', 'ò'=>'o', 'ó'=>'o',
43
			'ô'=>'o', 'õ'=>'o', 'ö'=>'oe', 'ø'=>'o', 'ù'=>'u', 'ú'=>'u', 'û'=>'u', 'ü'=>'ue', 'ý'=>'y', 'ý'=>'y',
44
			'þ'=>'b', 'ÿ'=>'y', 'Ŕ'=>'R', 'ŕ'=>'r',
45
			'Ā'=>'A', 'ā'=>'a', 'Ē'=>'E', 'ē'=>'e', 'Ī'=>'I', 'ī'=>'i', 'Ō'=>'O', 'ō'=>'o', 'Ū'=>'U', 'ū'=>'u',
46
			'œ'=>'oe', 'ß'=>'ss', 'ij'=>'ij', 'ą'=>'a','ę'=>'e', 'ė'=>'e', 'į'=>'i','ų'=>'u','ū'=>'u', 'Ą'=>'A',
47
			'Ę'=>'E', 'Ė'=>'E', 'Į'=>'I','Ų'=>'U','Ū'=>'U',
48
			"ľ"=>"l", "Ľ"=>"L", "ť"=>"t", "Ť"=>"T", "ů"=>"u", "Ů"=>"U",
49
			'ł'=>'l', 'Ł'=>'L', 'ń'=>'n', 'Ń'=>'N', 'ś'=>'s', 'Ś'=>'S', 'ź'=>'z', 'Ź'=>'Z', 'ż'=>'z', 'Ż'=>'Z',
50
		);
51
52
		return strtr($source, $table);
53
	}
54
55
	/**
56
	 * Transliteration using iconv()
57
	 */
58
	protected function useIconv($source) {
59
		return iconv("utf-8", "us-ascii//IGNORE//TRANSLIT", $source);
60
	}
61
}
62