1
|
|
|
<?php |
2
|
|
|
namespace keeko\core\utils; |
3
|
|
|
|
4
|
|
|
use Propel\Common\Pluralizer\StandardEnglishPluralizer; |
5
|
|
|
|
6
|
|
|
class StandardEnglishSingularizer extends StandardEnglishPluralizer { |
7
|
|
|
|
8
|
|
|
public function getSingularForm($plural) { |
|
|
|
|
9
|
|
|
// save some time in the case that singular and plural are the same |
10
|
|
|
if (in_array(strtolower($plural), $this->uncountable)) { |
11
|
|
|
return $plural; |
12
|
|
|
} |
13
|
|
|
|
14
|
|
|
// check for irregular singular words |
15
|
|
|
$irregular = array_flip($this->irregular); |
16
|
|
|
|
17
|
|
|
// some additions |
18
|
|
|
$irregular['indices'] = 'index'; |
|
|
|
|
19
|
|
|
$irregular['vertices'] = 'vertex'; |
20
|
|
|
$irregular['matrices'] = 'matrix'; |
21
|
|
|
|
22
|
|
View Code Duplication |
foreach ($irregular as $pattern => $result) { |
|
|
|
|
23
|
|
|
$searchPattern = '/' . $pattern . '$/i'; |
24
|
|
|
if (preg_match($searchPattern, $plural)) { |
25
|
|
|
return preg_replace($searchPattern, $result, $plural); |
26
|
|
|
} |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
// check for irregular singular suffixes |
30
|
|
|
$plurals = array_flip($this->plural); |
31
|
|
|
|
32
|
|
|
// some modifications |
33
|
|
|
unset($plurals['\1ices']); |
34
|
|
|
unset($plurals['\1i']); |
35
|
|
|
unset($plurals['\1oes']); |
36
|
|
|
|
37
|
|
|
$plurals['(alumn|bacill|cact|foc|fung|nucle|radi|stimul|syllab|termin|vir)i'] = '\1us'; |
38
|
|
|
$plurals['(buffal|tomat)oes'] = '\1o'; |
|
|
|
|
39
|
|
|
|
40
|
|
View Code Duplication |
foreach ($plurals as $pattern => $result) { |
|
|
|
|
41
|
|
|
$searchPattern = '/' . $pattern . '$/i'; |
42
|
|
|
if (preg_match($searchPattern, $plural)) { |
43
|
|
|
return preg_replace($searchPattern, $result, $plural); |
44
|
|
|
} |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
// fallback to naive singularization |
48
|
|
|
return substr($plural, 0, -1); |
49
|
|
|
} |
50
|
|
|
} |
Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a
@return
annotation as described here.