Completed
Push — test ( 5bf343...41c779 )
by Temitope
03:44
created

Inflector   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0
Metric Value
wmc 7
lcom 0
cbo 0
dl 0
loc 60
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
C pluralize() 0 48 7
1
<?php
2
3
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
4
// +----------------------------------------------------------------------+
5
// | Akelos PHP Application Framework                                     |
6
// +----------------------------------------------------------------------+
7
// | Copyright (c) 2002-2006, Akelos Media, S.L.  http://www.akelos.com/  |
8
// | Released under the GNU Lesser General Public License                 |
9
// +----------------------------------------------------------------------+
10
// | You should have received the following files along with this library |
11
// | - COPYRIGHT (Additional copyright notice)                            |
12
// | - DISCLAIMER (Disclaimer of warranty)                                |
13
// | - README (Important information regarding this library)              |
14
// +----------------------------------------------------------------------+
15
/**
16
 * Inflector for pluralize and singularize English nouns.
17
 *
18
 * This Inflector is a port of Ruby on Rails Inflector.
19
 *
20
 * It can be really helpful for developers that want to
21
 * create frameworks based on naming conventions rather than
22
 * configurations.
23
 *
24
 * It was ported to PHP for the Akelos Framework, a
25
 * multilingual Ruby on Rails like framework for PHP that will
26
 * be launched soon.
27
 *
28
 * @author Bermi Ferrer Martinez
29
 * @copyright Copyright (c) 2002-2006, Akelos Media, S.L. http://www.akelos.org
30
 * @license GNU Lesser General Public License
31
 * @since 0.1
32
 * @version $Revision 0.1 $
33
 */
34
35
namespace Laztopaz\PotatoORM;
36
37
trait Inflector
38
{
39
	/**
40
	 * Pluralizes English nouns.
41
	 *
42
	 * @access public
43
	 * @static
44
	 * @param    string    $word    English noun to pluralize
45
	 * @return string Plural noun
46
	 */
47
	public static function pluralize($word)
48
	{
49
		$plural = array(
50
			'/(quiz)$/i'               => "$1zes",
51
			'/^(ox)$/i'                => "$1en",
52
			'/([m|l])ouse$/i'          => "$1ice",
53
			'/(matr|vert|ind)ix|ex$/i' => "$1ices",
54
			'/(x|ch|ss|sh)$/i'         => "$1es",
55
			'/([^aeiouy]|qu)y$/i'      => "$1ies",
56
			'/(hive)$/i'               => "$1s",
57
			'/(?:([^f])fe|([lr])f)$/i' => "$1$2ves",
58
			'/(shea|lea|loa|thie)f$/i' => "$1ves",
59
			'/sis$/i'                  => "ses",
60
			'/([ti])um$/i'             => "$1a",
61
			'/(tomat|potat|ech|her|vet)o$/i'=> "$1oes",
62
			'/(bu)s$/i'                => "$1ses",
63
			'/(alias)$/i'              => "$1es",
64
			'/(octop)us$/i'            => "$1i",
65
			'/(ax|test)is$/i'          => "$1es",
66
			'/(us)$/i'                 => "$1es",
67
			'/s$/i'                    => "s",
68
			'/$/'                      => "s"
69
		);
70
		$uncountable = array('equipment', 'information', 'rice', 'money', 'species', 'series', 'fish', 'sheep');
71
		$irregular = array(
72
			'person' => 'people',
73
			'man' => 'men',
74
			'child' => 'children',
75
			'sex' => 'sexes',
76
			'move' => 'moves');
77
		$lowercased_word = strtolower($word);
78
		foreach ($uncountable as $_uncountable){
79
			if(substr($lowercased_word,(-1*strlen($_uncountable))) == $_uncountable){
80
				return $word;
81
			}
82
		}
83
		foreach ($irregular as $_plural=> $_singular){
84
			if (preg_match('/('.$_plural.')$/i', $word, $arr)) {
85
				return preg_replace('/('.$_plural.')$/i', substr($arr[0],0,1).substr($_singular,1), $word);
86
			}
87
		}
88
		foreach ($plural as $rule => $replacement) {
89
			if (preg_match($rule, $word)) {
90
				return preg_replace($rule, $replacement, $word);
91
			}
92
		}
93
		return false;
94
	}
95
	
96
}