Ticket #390: inflector.php

File inflector.php, 3.8 kB (added by atomless, 11 months ago)

edited inflector helper

Line 
1<?php defined('SYSPATH') or die('No direct script access.');
2/**
3 * Inflector helper class.
4 *
5 * $Id: inflector.php 1967 2008-02-06 21:34:07Z Shadowhand $
6 *
7 * @package    Core
8 * @author     Kohana Team
9 * @copyright  (c) 2007-2008 Kohana Team
10 * @license    http://kohanaphp.com/license.html
11 */
12class inflector_Core {
13
14    /**
15     * Checks if a word is defined as uncountable.
16     *
17     * @param   string   word to check
18     * @return  boolean
19     */
20    public static function uncountable($str)
21    {
22        static $uncountables = NULL;
23
24        if ($uncountables === NULL)
25        {
26            // Makes a mirrored array, eg: foo => foo
27            $uncountables = array_flip(Kohana::lang('inflector'));
28        }
29
30        return isset($uncountables[$str]);
31    }
32
33    /**
34     * Makes a plural word singular.
35     *
36     * @param   string  word to singularize
37     * @return  string
38     */
39    public static function singular($str, $count = NULL)
40    {
41        static $cache;
42
43        $str = trim($str);
44
45        // Cache key name
46        $key = $str.$count;
47
48        // We can just return uncountable words
49        if (inflector::uncountable($str))
50            return $str;
51
52        if (is_string($count) AND ctype_digit($count))
53        {
54            // Convert to integer when using a digit string
55            $count = (int) $count;
56        }
57
58        if ($cache === NULL)
59        {
60            // Initialize the cache
61            $cache = array();
62        }
63        else
64        {
65            // Already pluralized
66            if (isset($cache[$key]))
67                return $cache[$key];
68        }
69
70        // Do nothing with a single count
71        if (is_int($count) AND $count === 0 OR $count > 1)
72            return $str;
73
74        $end = substr($str, -3);
75
76        if ($end == 'ies')
77        {
78            $str = substr($str, 0, strlen($str) - 3).'y';
79        }
80        elseif (substr($str, -4)=='sses' OR $end == 'xes')
81        {
82            $str = substr($str, 0, strlen($str) - 2);
83        }
84        elseif (substr($str, -1) == 's')
85        {
86            $str = substr($str, 0, strlen($str) - 1);
87        }
88
89        return $cache[$key] = $str;
90    }
91
92    /**
93     * Makes a singular word plural.
94     *
95     * @param   string  word to pluralize
96     * @return  string
97     */
98    public static function plural($str, $count = NULL)
99    {
100        static $cache;
101
102        $str = trim($str);
103
104        // Cache key name
105        $key = $str.$count;
106
107        // We can just return uncountable words
108        if (inflector::uncountable($str))
109            return $str;
110
111        if (is_string($count) AND ctype_digit($count))
112        {
113            // Convert to integer when using a digit string
114            $count = (int) $count;
115        }
116
117        if ($cache === NULL)
118        {
119            // Initialize the cache
120            $cache = array();
121        }
122        else
123        {
124            // Already pluralized
125            if (isset($cache[$key]))
126                return $cache[$key];
127        }
128
129        // If the count is one, do not pluralize
130        if ($count === 1)
131            return $str;
132
133        $end = substr($str, -1);
134        $low = (strcmp($end, strtolower($end)) === 0) ? TRUE : FALSE;
135
136        if (preg_match('/[sxz]$/i', $str) OR preg_match('/[^aeioudgkprt]h$/i', $str))
137        {
138            $end = 'es';
139            $str .= ($low == FALSE) ? strtoupper($end) : $end;
140        }
141        elseif (preg_match('/[^aeiou]y$/i', $str))
142        {
143            $end = 'ies';
144            $end = ($low == FALSE) ? strtoupper($end) : $end;
145            $str = substr_replace($str, $end, -1);
146        }
147        else
148        {
149            $end = 's';
150            $str .= ($low == FALSE) ? strtoupper($end) : $end;
151        }
152
153        // Set the cache and return
154        return $cache[$key] = $str;
155    }
156
157    /**
158     * Makes a phrase camel case.
159     *
160     * @param   string  phrase to camelize
161     * @return  string
162     */
163    public static function camelize($str)
164    {
165        $str = 'x'.strtolower(trim($str));
166        $str = ucwords(preg_replace('/[\s_]+/', ' ', $str));
167
168        return substr(str_replace(' ', '', $str), 1);
169    }
170
171    /**
172     * Makes a phrase underscored instead of spaced.
173     *
174     * @param   string  phrase to underscore
175     * @return  string
176     */
177    public static function underscore($str)
178    {
179        return preg_replace('/\s+/', '_', trim($str));
180    }
181
182    /**
183     * Makes an underscored or dashed phrase human-reable.
184     *
185     * @param   string  phrase to make human-reable
186     * @return  string
187     */
188    public static function humanize($str)
189    {
190        return preg_replace('/[_-]+/', ' ', trim($str));
191    }
192
193} // End inflector