root/trunk/trax/vendor/trax/inflections.php

Revision 308, 7.0 KB (checked in by john, 3 years ago)

features and bug fixes from gocoffeego project

Line 
1<?php
2/**
3 *  File containing the Inflections class and default inflections
4 *
5 *  (PHP 5)
6 *
7 *  @package PHPonTrax
8 *  @version $Id: inflector.php 195 2006-04-03 22:27:26Z haas $
9 *  @copyright (c) 2005 John Peterson
10 *
11 *  Permission is hereby granted, free of charge, to any person obtaining
12 *  a copy of this software and associated documentation files (the
13 *  "Software"), to deal in the Software without restriction, including
14 *  without limitation the rights to use, copy, modify, merge, publish,
15 *  distribute, sublicense, and/or sell copies of the Software, and to
16 *  permit persons to whom the Software is furnished to do so, subject to
17 *  the following conditions:
18 *
19 *  The above copyright notice and this permission notice shall be
20 *  included in all copies or substantial portions of the Software.
21 *
22 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 *  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 *  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 *  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 *  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 *  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 */
30
31
32# Start Default Inflections
33
34Inflections::plural('/$/', 's');
35Inflections::plural('/s$/i', 's');
36Inflections::plural('/(ax|test)is$/i', '\1es');
37Inflections::plural('/(octop|vir)us$/i', '\1i');
38Inflections::plural('/(alias|status)$/i', '\1es');
39Inflections::plural('/(bu)s$/i', '\1ses');
40Inflections::plural('/(buffal|tomat)o$/i', '\1oes');
41Inflections::plural('/([ti])um$/i', '\1a');
42Inflections::plural('/sis$/i', 'ses');
43Inflections::plural('/(?:([^f])fe|([lr])f)$/i', '\1\2ves');
44Inflections::plural('/(hive)$/i', '\1s');
45Inflections::plural('/([^aeiouy]|qu)y$/i', '\1ies');
46Inflections::plural('/([^aeiouy]|qu)ies$/i', '\1y');
47Inflections::plural('/(x|ch|ss|sh)$/i', '\1es');
48Inflections::plural('/(matr|vert|ind)ix|ex$/i', '\1ices');
49Inflections::plural('/([m|l])ouse$/i', '\1ice');
50Inflections::plural('/^(ox)$/i', '\1en');
51Inflections::plural('/(quiz)$/i', '\1zes');
52
53Inflections::singular('/s$/i', '');
54Inflections::singular('/(n)ews$/i', '\1ews'); 
55Inflections::singular('/([ti])a$/i', '\1um'); 
56Inflections::singular('/((a)naly|(b)a|(d)iagno|(p)arenthe|(p)rogno|(s)ynop|(t)he)ses$/i', '\1\2sis'); 
57Inflections::singular('/(^analy)ses$/i', '\1sis'); 
58Inflections::singular('/([^f])ves$/i', '\1fe'); 
59Inflections::singular('/(hive)s$/i', '\1'); 
60Inflections::singular('/(tive)s$/i', '\1'); 
61Inflections::singular('/([lr])ves$/i', '\1f'); 
62Inflections::singular('/([^aeiouy]|qu)ies$/i', '\1y'); 
63Inflections::singular('/(s)eries$/i', '\1eries'); 
64Inflections::singular('/(m)ovies$/i', '\1ovie'); 
65Inflections::singular('/(x|ch|ss|sh)es$/i', '\1'); 
66Inflections::singular('/([m|l])ice$/i', '\1ouse'); 
67Inflections::singular('/(bus)es$/i', '\1'); 
68Inflections::singular('/(o)es$/i', '\1'); 
69Inflections::singular('/(shoe)s$/i', '\1'); 
70Inflections::singular('/(cris|ax|test)es$/i', '\1is'); 
71Inflections::singular('/([octop|vir])i$/i', '\1us'); 
72Inflections::singular('/(alias|status)es$/i', '\1'); 
73Inflections::singular('/^(ox)en/i', '\1'); 
74Inflections::singular('/(vert|ind)ices$/i', '\1ex'); 
75Inflections::singular('/(matr)ices$/i', '\1ix'); 
76Inflections::singular('/(quiz)zes$/i', '\1'); 
77 
78Inflections::irregular('person', 'people'); 
79Inflections::irregular('man', 'men'); 
80Inflections::irregular('child', 'children'); 
81Inflections::irregular('sex', 'sexes'); 
82Inflections::irregular('move', 'moves'); 
83Inflections::irregular('cow', 'kine'); 
84
85Inflections::uncountable('equipment', 'information', 'rice', 'money', 'species', 'series', 'fish', 'sheep');
86
87# End Default Inflections
88
89
90/**
91 *  Implement the Trax naming convention
92 *
93 *  Inflections is never instantiated.
94 */
95class Inflections {
96
97    public static 
98        $plurals = array(),
99        $singulars = array(),
100        $uncountables = array(),
101        $humans = array();
102
103    # Specifies a new pluralization rule and its replacement. The rule can either be a string or a regular expression.
104    # The replacement should always be a string that may include references to the matched data from the rule.
105    function plural($rule, $replacement) {
106        array_unshift(self::$plurals, array("rule" => $rule, "replacement" => $replacement));
107    }
108   
109    # Specifies a new singularization rule and its replacement. The rule can either be a string or a regular expression.
110    # The replacement should always be a string that may include references to the matched data from the rule.
111    function singular($rule, $replacement) {
112        array_unshift(self::$singulars, array("rule" => $rule, "replacement" => $replacement));
113    }
114   
115    # Specifies a new irregular that applies to both pluralization and singularization at the same time. This can only be used
116    # for strings, not regular expressions. You simply pass the irregular in singular and plural form.
117    #
118    # Examples:
119    #   Inflections::irregular('octopus', 'octopi')
120    #   Inflections::irregular('person', 'people')
121    function irregular($singular, $plural) {
122        self::plural('/('.preg_quote(substr($singular,0,1)).')'.preg_quote(substr($singular,1)).'$/i', '\1'.preg_quote(substr($plural,1)));
123        self::singular('/('.preg_quote(substr($plural,0,1)).')'.preg_quote(substr($plural,1)).'$/i', '\1'.preg_quote(substr($singular,1)));
124    }
125   
126    # Add uncountable words that shouldn't be attempted inflected.
127    #
128    # Examples:
129    #   Inflections::uncountable("money")
130    #   Inflections::uncountable("money", "information")
131    #   Inflections::uncountable(array("money", "information", "rice"))
132    function uncountable() {
133        $args = func_get_args();
134        if(is_array($args[0])) {
135            $args = $args[0];   
136        }
137        foreach($args as $word) {
138            self::$uncountables[] = $word;   
139        }     
140    }
141
142    # Specifies a humanized form of a string by a regular expression rule or by a string mapping.
143    # When using a regular expression based replacement, the normal humanize formatting is called after the replacement.
144    # When a string is used, the human form should be specified as desired (example: 'The name', not 'the_name')
145    #
146    # Examples:
147    #   Inflections::human("/_cnt$/i", "\1_count")
148    #   Inflections::human("legacy_col_person_name", "Name")
149    function human($rule, $replacement) {
150        array_unshift(self::$humans, array("rule" => $rule, "replacement" => $replacement));
151    }
152   
153    # Clears the loaded inflections within a given scope (functionault is :all). Give the scope as a symbol of the inflection type,
154    # the options are: "plurals", "singulars", "uncountables"
155    #
156    # Examples:
157    #   Inflections::clear("all")
158    #   Inflections::clear("plurals")
159    function clear($scope = "all") {
160        if($scope == "all") {
161            self::$plurals = self::$singulars = self::$uncountables = self::$humans = array();
162        } else {
163            self::$$scope = array();
164        }
165    }
166
167}
168
169?>
Note: See TracBrowser for help on using the browser.