Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
12345 anikendra 1
<?php
2
/**
3
 * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
4
 * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
5
 *
6
 * Licensed under The MIT License
7
 * For full copyright and license information, please see the LICENSE.txt
8
 * Redistributions of files must retain the above copyright notice.
9
 *
10
 * @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
11
 * @link          http://cakephp.org CakePHP(tm) Project
12
 * @package       Cake.Utility
13
 * @since         CakePHP(tm) v 0.2.9
14
 * @license       http://www.opensource.org/licenses/mit-license.php MIT License
15
 */
16
 
17
/**
18
 * Pluralize and singularize English words.
19
 *
20
 * Inflector pluralizes and singularizes English nouns.
21
 * Used by CakePHP's naming conventions throughout the framework.
22
 *
23
 * @package       Cake.Utility
24
 * @link          http://book.cakephp.org/2.0/en/core-utility-libraries/inflector.html
25
 */
26
class Inflector {
27
 
28
/**
29
 * Plural inflector rules
30
 *
31
 * @var array
32
 */
33
	protected static $_plural = array(
34
		'rules' => array(
35
			'/(s)tatus$/i' => '\1\2tatuses',
36
			'/(quiz)$/i' => '\1zes',
37
			'/^(ox)$/i' => '\1\2en',
38
			'/([m|l])ouse$/i' => '\1ice',
39
			'/(matr|vert|ind)(ix|ex)$/i' => '\1ices',
40
			'/(x|ch|ss|sh)$/i' => '\1es',
41
			'/([^aeiouy]|qu)y$/i' => '\1ies',
42
			'/(hive)$/i' => '\1s',
43
			'/(?:([^f])fe|([lre])f)$/i' => '\1\2ves',
44
			'/sis$/i' => 'ses',
45
			'/([ti])um$/i' => '\1a',
46
			'/(p)erson$/i' => '\1eople',
47
			'/(m)an$/i' => '\1en',
48
			'/(c)hild$/i' => '\1hildren',
49
			'/(buffal|tomat)o$/i' => '\1\2oes',
50
			'/(alumn|bacill|cact|foc|fung|nucle|radi|stimul|syllab|termin|vir)us$/i' => '\1i',
51
			'/us$/i' => 'uses',
52
			'/(alias)$/i' => '\1es',
53
			'/(ax|cris|test)is$/i' => '\1es',
54
			'/s$/' => 's',
55
			'/^$/' => '',
56
			'/$/' => 's',
57
		),
58
		'uninflected' => array(
59
			'.*[nrlm]ese',
60
			'.*deer',
61
			'.*fish',
62
			'.*measles',
63
			'.*ois',
64
			'.*pox',
65
			'.*sheep',
66
			'people',
67
			'feedback',
68
			'stadia'
69
		),
70
		'irregular' => array(
71
			'atlas' => 'atlases',
72
			'beef' => 'beefs',
73
			'brief' => 'briefs',
74
			'brother' => 'brothers',
75
			'cafe' => 'cafes',
76
			'child' => 'children',
77
			'cookie' => 'cookies',
78
			'corpus' => 'corpuses',
79
			'cow' => 'cows',
80
			'ganglion' => 'ganglions',
81
			'genie' => 'genies',
82
			'genus' => 'genera',
83
			'graffito' => 'graffiti',
84
			'hoof' => 'hoofs',
85
			'loaf' => 'loaves',
86
			'man' => 'men',
87
			'money' => 'monies',
88
			'mongoose' => 'mongooses',
89
			'move' => 'moves',
90
			'mythos' => 'mythoi',
91
			'niche' => 'niches',
92
			'numen' => 'numina',
93
			'occiput' => 'occiputs',
94
			'octopus' => 'octopuses',
95
			'opus' => 'opuses',
96
			'ox' => 'oxen',
97
			'penis' => 'penises',
98
			'person' => 'people',
99
			'sex' => 'sexes',
100
			'soliloquy' => 'soliloquies',
101
			'testis' => 'testes',
102
			'trilby' => 'trilbys',
103
			'turf' => 'turfs',
104
			'potato' => 'potatoes',
105
			'hero' => 'heroes',
106
			'tooth' => 'teeth',
107
			'goose' => 'geese',
108
			'foot' => 'feet'
109
		)
110
	);
111
 
112
/**
113
 * Singular inflector rules
114
 *
115
 * @var array
116
 */
117
	protected static $_singular = array(
118
		'rules' => array(
119
			'/(s)tatuses$/i' => '\1\2tatus',
120
			'/^(.*)(menu)s$/i' => '\1\2',
121
			'/(quiz)zes$/i' => '\\1',
122
			'/(matr)ices$/i' => '\1ix',
123
			'/(vert|ind)ices$/i' => '\1ex',
124
			'/^(ox)en/i' => '\1',
125
			'/(alias)(es)*$/i' => '\1',
126
			'/(alumn|bacill|cact|foc|fung|nucle|radi|stimul|syllab|termin|viri?)i$/i' => '\1us',
127
			'/([ftw]ax)es/i' => '\1',
128
			'/(cris|ax|test)es$/i' => '\1is',
129
			'/(shoe)s$/i' => '\1',
130
			'/(o)es$/i' => '\1',
131
			'/ouses$/' => 'ouse',
132
			'/([^a])uses$/' => '\1us',
133
			'/([m|l])ice$/i' => '\1ouse',
134
			'/(x|ch|ss|sh)es$/i' => '\1',
135
			'/(m)ovies$/i' => '\1\2ovie',
136
			'/(s)eries$/i' => '\1\2eries',
137
			'/([^aeiouy]|qu)ies$/i' => '\1y',
138
			'/(tive)s$/i' => '\1',
139
			'/(hive)s$/i' => '\1',
140
			'/(drive)s$/i' => '\1',
141
			'/([le])ves$/i' => '\1f',
142
			'/([^rfoa])ves$/i' => '\1fe',
143
			'/(^analy)ses$/i' => '\1sis',
144
			'/(analy|diagno|^ba|(p)arenthe|(p)rogno|(s)ynop|(t)he)ses$/i' => '\1\2sis',
145
			'/([ti])a$/i' => '\1um',
146
			'/(p)eople$/i' => '\1\2erson',
147
			'/(m)en$/i' => '\1an',
148
			'/(c)hildren$/i' => '\1\2hild',
149
			'/(n)ews$/i' => '\1\2ews',
150
			'/eaus$/' => 'eau',
151
			'/^(.*us)$/' => '\\1',
152
			'/s$/i' => ''
153
		),
154
		'uninflected' => array(
155
			'.*[nrlm]ese', '.*deer', '.*fish', '.*measles', '.*ois', '.*pox', '.*sheep', '.*ss', 'feedback'
156
		),
157
		'irregular' => array(
158
			'foes' => 'foe',
159
		)
160
	);
161
 
162
/**
163
 * Words that should not be inflected
164
 *
165
 * @var array
166
 */
167
	protected static $_uninflected = array(
168
		'Amoyese', 'bison', 'Borghese', 'bream', 'breeches', 'britches', 'buffalo', 'cantus',
169
		'carp', 'chassis', 'clippers', 'cod', 'coitus', 'Congoese', 'contretemps', 'corps',
170
		'debris', 'diabetes', 'djinn', 'eland', 'elk', 'equipment', 'Faroese', 'flounder',
171
		'Foochowese', 'gallows', 'Genevese', 'Genoese', 'Gilbertese', 'graffiti',
172
		'headquarters', 'herpes', 'hijinks', 'Hottentotese', 'information', 'innings',
173
		'jackanapes', 'Kiplingese', 'Kongoese', 'Lucchese', 'mackerel', 'Maltese', '.*?media',
174
		'metadata', 'mews', 'moose', 'mumps', 'Nankingese', 'news', 'nexus', 'Niasese',
175
		'Pekingese', 'Piedmontese', 'pincers', 'Pistoiese', 'pliers', 'Portuguese',
176
		'proceedings', 'rabies', 'research', 'rice', 'rhinoceros', 'salmon', 'Sarawakese', 'scissors',
177
		'sea[- ]bass', 'series', 'Shavese', 'shears', 'siemens', 'species', 'swine', 'testes',
178
		'trousers', 'trout', 'tuna', 'Vermontese', 'Wenchowese', 'whiting', 'wildebeest',
179
		'Yengeese'
180
	);
181
 
182
/**
183
 * Default map of accented and special characters to ASCII characters
184
 *
185
 * @var array
186
 */
187
	protected static $_transliteration = array(
188
		'/À|Á|Â|Ã|Å|Ǻ|Ā|Ă|Ą|Ǎ/' => 'A',
189
		'/Æ|Ǽ/' => 'AE',
190
		'/Ä/' => 'Ae',
191
		'/Ç|Ć|Ĉ|Ċ|Č/' => 'C',
192
		'/Ð|Ď|Đ/' => 'D',
193
		'/È|É|Ê|Ë|Ē|Ĕ|Ė|Ę|Ě/' => 'E',
194
		'/Ĝ|Ğ|Ġ|Ģ|Ґ/' => 'G',
195
		'/Ĥ|Ħ/' => 'H',
196
		'/Ì|Í|Î|Ï|Ĩ|Ī|Ĭ|Ǐ|Į|İ|І/' => 'I',
197
		'/IJ/' => 'IJ',
198
		'/Ĵ/' => 'J',
199
		'/Ķ/' => 'K',
200
		'/Ĺ|Ļ|Ľ|Ŀ|Ł/' => 'L',
201
		'/Ñ|Ń|Ņ|Ň/' => 'N',
202
		'/Ò|Ó|Ô|Õ|Ō|Ŏ|Ǒ|Ő|Ơ|Ø|Ǿ/' => 'O',
203
		'/Œ/' => 'OE',
204
		'/Ö/' => 'Oe',
205
		'/Ŕ|Ŗ|Ř/' => 'R',
206
		'/Ś|Ŝ|Ş|Ș|Š/' => 'S',
207
		'/ẞ/' => 'SS',
208
		'/Ţ|Ț|Ť|Ŧ/' => 'T',
209
		'/Þ/' => 'TH',
210
		'/Ù|Ú|Û|Ũ|Ū|Ŭ|Ů|Ű|Ų|Ư|Ǔ|Ǖ|Ǘ|Ǚ|Ǜ/' => 'U',
211
		'/Ü/' => 'Ue',
212
		'/Ŵ/' => 'W',
213
		'/Ý|Ÿ|Ŷ/' => 'Y',
214
		'/Є/' => 'Ye',
215
		'/Ї/' => 'Yi',
216
		'/Ź|Ż|Ž/' => 'Z',
217
		'/à|á|â|ã|å|ǻ|ā|ă|ą|ǎ|ª/' => 'a',
218
		'/ä|æ|ǽ/' => 'ae',
219
		'/ç|ć|ĉ|ċ|č/' => 'c',
220
		'/ð|ď|đ/' => 'd',
221
		'/è|é|ê|ë|ē|ĕ|ė|ę|ě/' => 'e',
222
		'/ƒ/' => 'f',
223
		'/ĝ|ğ|ġ|ģ|ґ/' => 'g',
224
		'/ĥ|ħ/' => 'h',
225
		'/ì|í|î|ï|ĩ|ī|ĭ|ǐ|į|ı|і/' => 'i',
226
		'/ij/' => 'ij',
227
		'/ĵ/' => 'j',
228
		'/ķ/' => 'k',
229
		'/ĺ|ļ|ľ|ŀ|ł/' => 'l',
230
		'/ñ|ń|ņ|ň|ʼn/' => 'n',
231
		'/ò|ó|ô|õ|ō|ŏ|ǒ|ő|ơ|ø|ǿ|º/' => 'o',
232
		'/ö|œ/' => 'oe',
233
		'/ŕ|ŗ|ř/' => 'r',
234
		'/ś|ŝ|ş|ș|š|ſ/' => 's',
235
		'/ß/' => 'ss',
236
		'/ţ|ț|ť|ŧ/' => 't',
237
		'/þ/' => 'th',
238
		'/ù|ú|û|ũ|ū|ŭ|ů|ű|ų|ư|ǔ|ǖ|ǘ|ǚ|ǜ/' => 'u',
239
		'/ü/' => 'ue',
240
		'/ŵ/' => 'w',
241
		'/ý|ÿ|ŷ/' => 'y',
242
		'/є/' => 'ye',
243
		'/ї/' => 'yi',
244
		'/ź|ż|ž/' => 'z',
245
	);
246
 
247
/**
248
 * Method cache array.
249
 *
250
 * @var array
251
 */
252
	protected static $_cache = array();
253
 
254
/**
255
 * The initial state of Inflector so reset() works.
256
 *
257
 * @var array
258
 */
259
	protected static $_initialState = array();
260
 
261
/**
262
 * Cache inflected values, and return if already available
263
 *
264
 * @param string $type Inflection type
265
 * @param string $key Original value
266
 * @param string $value Inflected value
267
 * @return string Inflected value, from cache
268
 */
269
	protected static function _cache($type, $key, $value = false) {
270
		$key = '_' . $key;
271
		$type = '_' . $type;
272
		if ($value !== false) {
273
			self::$_cache[$type][$key] = $value;
274
			return $value;
275
		}
276
		if (!isset(self::$_cache[$type][$key])) {
277
			return false;
278
		}
279
		return self::$_cache[$type][$key];
280
	}
281
 
282
/**
283
 * Clears Inflectors inflected value caches. And resets the inflection
284
 * rules to the initial values.
285
 *
286
 * @return void
287
 */
288
	public static function reset() {
289
		if (empty(self::$_initialState)) {
290
			self::$_initialState = get_class_vars('Inflector');
291
			return;
292
		}
293
		foreach (self::$_initialState as $key => $val) {
294
			if ($key !== '_initialState') {
295
				self::${$key} = $val;
296
			}
297
		}
298
	}
299
 
300
/**
301
 * Adds custom inflection $rules, of either 'plural', 'singular' or 'transliteration' $type.
302
 *
303
 * ### Usage:
304
 *
305
 * {{{
306
 * Inflector::rules('plural', array('/^(inflect)or$/i' => '\1ables'));
307
 * Inflector::rules('plural', array(
308
 *     'rules' => array('/^(inflect)ors$/i' => '\1ables'),
309
 *     'uninflected' => array('dontinflectme'),
310
 *     'irregular' => array('red' => 'redlings')
311
 * ));
312
 * Inflector::rules('transliteration', array('/å/' => 'aa'));
313
 * }}}
314
 *
315
 * @param string $type The type of inflection, either 'plural', 'singular' or 'transliteration'
316
 * @param array $rules Array of rules to be added.
317
 * @param bool $reset If true, will unset default inflections for all
318
 *        new rules that are being defined in $rules.
319
 * @return void
320
 */
321
	public static function rules($type, $rules, $reset = false) {
322
		$var = '_' . $type;
323
 
324
		switch ($type) {
325
			case 'transliteration':
326
				if ($reset) {
327
					self::$_transliteration = $rules;
328
				} else {
329
					self::$_transliteration = $rules + self::$_transliteration;
330
				}
331
				break;
332
 
333
			default:
334
				foreach ($rules as $rule => $pattern) {
335
					if (is_array($pattern)) {
336
						if ($reset) {
337
							self::${$var}[$rule] = $pattern;
338
						} else {
339
							if ($rule === 'uninflected') {
340
								self::${$var}[$rule] = array_merge($pattern, self::${$var}[$rule]);
341
							} else {
342
								self::${$var}[$rule] = $pattern + self::${$var}[$rule];
343
							}
344
						}
345
						unset($rules[$rule], self::${$var}['cache' . ucfirst($rule)]);
346
						if (isset(self::${$var}['merged'][$rule])) {
347
							unset(self::${$var}['merged'][$rule]);
348
						}
349
						if ($type === 'plural') {
350
							self::$_cache['pluralize'] = self::$_cache['tableize'] = array();
351
						} elseif ($type === 'singular') {
352
							self::$_cache['singularize'] = array();
353
						}
354
					}
355
				}
356
				self::${$var}['rules'] = $rules + self::${$var}['rules'];
357
		}
358
	}
359
 
360
/**
361
 * Return $word in plural form.
362
 *
363
 * @param string $word Word in singular
364
 * @return string Word in plural
365
 * @link http://book.cakephp.org/2.0/en/core-utility-libraries/inflector.html#Inflector::pluralize
366
 */
367
	public static function pluralize($word) {
368
		if (isset(self::$_cache['pluralize'][$word])) {
369
			return self::$_cache['pluralize'][$word];
370
		}
371
 
372
		if (!isset(self::$_plural['merged']['irregular'])) {
373
			self::$_plural['merged']['irregular'] = self::$_plural['irregular'];
374
		}
375
 
376
		if (!isset(self::$_plural['merged']['uninflected'])) {
377
			self::$_plural['merged']['uninflected'] = array_merge(self::$_plural['uninflected'], self::$_uninflected);
378
		}
379
 
380
		if (!isset(self::$_plural['cacheUninflected']) || !isset(self::$_plural['cacheIrregular'])) {
381
			self::$_plural['cacheUninflected'] = '(?:' . implode('|', self::$_plural['merged']['uninflected']) . ')';
382
			self::$_plural['cacheIrregular'] = '(?:' . implode('|', array_keys(self::$_plural['merged']['irregular'])) . ')';
383
		}
384
 
385
		if (preg_match('/(.*)\\b(' . self::$_plural['cacheIrregular'] . ')$/i', $word, $regs)) {
386
			self::$_cache['pluralize'][$word] = $regs[1] . substr($word, 0, 1) . substr(self::$_plural['merged']['irregular'][strtolower($regs[2])], 1);
387
			return self::$_cache['pluralize'][$word];
388
		}
389
 
390
		if (preg_match('/^(' . self::$_plural['cacheUninflected'] . ')$/i', $word, $regs)) {
391
			self::$_cache['pluralize'][$word] = $word;
392
			return $word;
393
		}
394
 
395
		foreach (self::$_plural['rules'] as $rule => $replacement) {
396
			if (preg_match($rule, $word)) {
397
				self::$_cache['pluralize'][$word] = preg_replace($rule, $replacement, $word);
398
				return self::$_cache['pluralize'][$word];
399
			}
400
		}
401
	}
402
 
403
/**
404
 * Return $word in singular form.
405
 *
406
 * @param string $word Word in plural
407
 * @return string Word in singular
408
 * @link http://book.cakephp.org/2.0/en/core-utility-libraries/inflector.html#Inflector::singularize
409
 */
410
	public static function singularize($word) {
411
		if (isset(self::$_cache['singularize'][$word])) {
412
			return self::$_cache['singularize'][$word];
413
		}
414
 
415
		if (!isset(self::$_singular['merged']['uninflected'])) {
416
			self::$_singular['merged']['uninflected'] = array_merge(
417
				self::$_singular['uninflected'],
418
				self::$_uninflected
419
			);
420
		}
421
 
422
		if (!isset(self::$_singular['merged']['irregular'])) {
423
			self::$_singular['merged']['irregular'] = array_merge(
424
				self::$_singular['irregular'],
425
				array_flip(self::$_plural['irregular'])
426
			);
427
		}
428
 
429
		if (!isset(self::$_singular['cacheUninflected']) || !isset(self::$_singular['cacheIrregular'])) {
430
			self::$_singular['cacheUninflected'] = '(?:' . implode('|', self::$_singular['merged']['uninflected']) . ')';
431
			self::$_singular['cacheIrregular'] = '(?:' . implode('|', array_keys(self::$_singular['merged']['irregular'])) . ')';
432
		}
433
 
434
		if (preg_match('/(.*)\\b(' . self::$_singular['cacheIrregular'] . ')$/i', $word, $regs)) {
435
			self::$_cache['singularize'][$word] = $regs[1] . substr($word, 0, 1) . substr(self::$_singular['merged']['irregular'][strtolower($regs[2])], 1);
436
			return self::$_cache['singularize'][$word];
437
		}
438
 
439
		if (preg_match('/^(' . self::$_singular['cacheUninflected'] . ')$/i', $word, $regs)) {
440
			self::$_cache['singularize'][$word] = $word;
441
			return $word;
442
		}
443
 
444
		foreach (self::$_singular['rules'] as $rule => $replacement) {
445
			if (preg_match($rule, $word)) {
446
				self::$_cache['singularize'][$word] = preg_replace($rule, $replacement, $word);
447
				return self::$_cache['singularize'][$word];
448
			}
449
		}
450
		self::$_cache['singularize'][$word] = $word;
451
		return $word;
452
	}
453
 
454
/**
455
 * Returns the given lower_case_and_underscored_word as a CamelCased word.
456
 *
457
 * @param string $lowerCaseAndUnderscoredWord Word to camelize
458
 * @return string Camelized word. LikeThis.
459
 * @link http://book.cakephp.org/2.0/en/core-utility-libraries/inflector.html#Inflector::camelize
460
 */
461
	public static function camelize($lowerCaseAndUnderscoredWord) {
462
		if (!($result = self::_cache(__FUNCTION__, $lowerCaseAndUnderscoredWord))) {
463
			$result = str_replace(' ', '', Inflector::humanize($lowerCaseAndUnderscoredWord));
464
			self::_cache(__FUNCTION__, $lowerCaseAndUnderscoredWord, $result);
465
		}
466
		return $result;
467
	}
468
 
469
/**
470
 * Returns the given camelCasedWord as an underscored_word.
471
 *
472
 * @param string $camelCasedWord Camel-cased word to be "underscorized"
473
 * @return string Underscore-syntaxed version of the $camelCasedWord
474
 * @link http://book.cakephp.org/2.0/en/core-utility-libraries/inflector.html#Inflector::underscore
475
 */
476
	public static function underscore($camelCasedWord) {
477
		if (!($result = self::_cache(__FUNCTION__, $camelCasedWord))) {
478
			$result = strtolower(preg_replace('/(?<=\\w)([A-Z])/', '_\\1', $camelCasedWord));
479
			self::_cache(__FUNCTION__, $camelCasedWord, $result);
480
		}
481
		return $result;
482
	}
483
 
484
/**
485
 * Returns the given underscored_word_group as a Human Readable Word Group.
486
 * (Underscores are replaced by spaces and capitalized following words.)
487
 *
488
 * @param string $lowerCaseAndUnderscoredWord String to be made more readable
489
 * @return string Human-readable string
490
 * @link http://book.cakephp.org/2.0/en/core-utility-libraries/inflector.html#Inflector::humanize
491
 */
492
	public static function humanize($lowerCaseAndUnderscoredWord) {
493
		if (!($result = self::_cache(__FUNCTION__, $lowerCaseAndUnderscoredWord))) {
494
			$result = ucwords(str_replace('_', ' ', $lowerCaseAndUnderscoredWord));
495
			self::_cache(__FUNCTION__, $lowerCaseAndUnderscoredWord, $result);
496
		}
497
		return $result;
498
	}
499
 
500
/**
501
 * Returns corresponding table name for given model $className. ("people" for the model class "Person").
502
 *
503
 * @param string $className Name of class to get database table name for
504
 * @return string Name of the database table for given class
505
 * @link http://book.cakephp.org/2.0/en/core-utility-libraries/inflector.html#Inflector::tableize
506
 */
507
	public static function tableize($className) {
508
		if (!($result = self::_cache(__FUNCTION__, $className))) {
509
			$result = Inflector::pluralize(Inflector::underscore($className));
510
			self::_cache(__FUNCTION__, $className, $result);
511
		}
512
		return $result;
513
	}
514
 
515
/**
516
 * Returns Cake model class name ("Person" for the database table "people".) for given database table.
517
 *
518
 * @param string $tableName Name of database table to get class name for
519
 * @return string Class name
520
 * @link http://book.cakephp.org/2.0/en/core-utility-libraries/inflector.html#Inflector::classify
521
 */
522
	public static function classify($tableName) {
523
		if (!($result = self::_cache(__FUNCTION__, $tableName))) {
524
			$result = Inflector::camelize(Inflector::singularize($tableName));
525
			self::_cache(__FUNCTION__, $tableName, $result);
526
		}
527
		return $result;
528
	}
529
 
530
/**
531
 * Returns camelBacked version of an underscored string.
532
 *
533
 * @param string $string String to convert.
534
 * @return string in variable form
535
 * @link http://book.cakephp.org/2.0/en/core-utility-libraries/inflector.html#Inflector::variable
536
 */
537
	public static function variable($string) {
538
		if (!($result = self::_cache(__FUNCTION__, $string))) {
539
			$camelized = Inflector::camelize(Inflector::underscore($string));
540
			$replace = strtolower(substr($camelized, 0, 1));
541
			$result = preg_replace('/\\w/', $replace, $camelized, 1);
542
			self::_cache(__FUNCTION__, $string, $result);
543
		}
544
		return $result;
545
	}
546
 
547
/**
548
 * Returns a string with all spaces converted to underscores (by default), accented
549
 * characters converted to non-accented characters, and non word characters removed.
550
 *
551
 * @param string $string the string you want to slug
552
 * @param string $replacement will replace keys in map
553
 * @return string
554
 * @link http://book.cakephp.org/2.0/en/core-utility-libraries/inflector.html#Inflector::slug
555
 */
556
	public static function slug($string, $replacement = '_') {
557
		$quotedReplacement = preg_quote($replacement, '/');
558
 
559
		$merge = array(
560
			'/[^\s\p{Zs}\p{Ll}\p{Lm}\p{Lo}\p{Lt}\p{Lu}\p{Nd}]/mu' => ' ',
561
			'/[\s\p{Zs}]+/mu' => $replacement,
562
			sprintf('/^[%s]+|[%s]+$/', $quotedReplacement, $quotedReplacement) => '',
563
		);
564
 
565
		$map = self::$_transliteration + $merge;
566
		return preg_replace(array_keys($map), array_values($map), $string);
567
	}
568
 
569
}
570
 
571
// Store the initial state
572
Inflector::reset();