diff options
| author | Bharat Mediratta <bharat@menalto.com> | 2008-12-15 08:56:18 +0000 |
|---|---|---|
| committer | Bharat Mediratta <bharat@menalto.com> | 2008-12-15 08:56:18 +0000 |
| commit | 3b35e8b91ce94c292b46a296d034542ac5f0f6da (patch) | |
| tree | 7a1444b5e16475b66300be105b8fc39e5117c881 /kohana/helpers/inflector.php | |
| parent | 628058b4ed5aefb543ceb6ca9d3b87828c66bef1 (diff) | |
Refresh kohana from upstream svn trunk r3771.
During this process, remove a considerable number of files from kohana
that we will not be needing in Gallery3, including the following files
and directories:
kohana/application
kohana/example.htaccess
kohana/index.php
kohana/install.php
kohana/kohana.png
kohana/modules/archive
kohana/modules/auth
kohana/modules/flot
kohana/modules/gmaps
kohana/modules/kodoc
kohana/modules/payment
kohana/modules/smarty
kohana/modules/unit_test/i18n
kohana/modules/unit_test/tests/Example_Test.php
kohana/modules/unit_test/tests/Valid_Test.php
kohana/system/config/captcha.php
kohana/system/controllers/captcha.php
kohana/system/fonts
kohana/system/i18n
kohana/system/libraries/Calendar.php
kohana/system/libraries/Calendar_Event.php
kohana/system/libraries/Captcha.php
kohana/system/libraries/Tagcloud.php
kohana/system/vendor
kohana/system/views/pagination
kohana/system/views/kohana_calendar.php
Diffstat (limited to 'kohana/helpers/inflector.php')
| -rw-r--r-- | kohana/helpers/inflector.php | 193 |
1 files changed, 193 insertions, 0 deletions
diff --git a/kohana/helpers/inflector.php b/kohana/helpers/inflector.php new file mode 100644 index 00000000..b619eddb --- /dev/null +++ b/kohana/helpers/inflector.php @@ -0,0 +1,193 @@ +<?php defined('SYSPATH') OR die('No direct access allowed.'); +/** + * Inflector helper class. + * + * $Id$ + * + * @package Core + * @author Kohana Team + * @copyright (c) 2007-2008 Kohana Team + * @license http://kohanaphp.com/license.html + */ +class inflector_Core { + + // Cached inflections + protected static $cache = array(); + + // Uncountable and irregular words + protected static $uncountable; + protected static $irregular; + + /** + * Checks if a word is defined as uncountable. + * + * @param string word to check + * @return boolean + */ + public static function uncountable($str) + { + if (self::$uncountable === NULL) + { + // Cache uncountables + self::$uncountable = Kohana::config('inflector.uncountable'); + + // Make uncountables mirroed + self::$uncountable = array_combine(self::$uncountable, self::$uncountable); + } + + return isset(self::$uncountable[strtolower($str)]); + } + + /** + * Makes a plural word singular. + * + * @param string word to singularize + * @param integer number of things + * @return string + */ + public static function singular($str, $count = NULL) + { + // Remove garbage + $str = strtolower(trim($str)); + + if (is_string($count)) + { + // Convert to integer when using a digit string + $count = (int) $count; + } + + // Do nothing with a single count + if ($count === 0 OR $count > 1) + return $str; + + // Cache key name + $key = 'singular_'.$str.$count; + + if (isset(self::$cache[$key])) + return self::$cache[$key]; + + if (inflector::uncountable($str)) + return self::$cache[$key] = $str; + + if (empty(self::$irregular)) + { + // Cache irregular words + self::$irregular = Kohana::config('inflector.irregular'); + } + + if ($irregular = array_search($str, self::$irregular)) + { + $str = $irregular; + } + elseif (preg_match('/[sxz]es$/', $str) OR preg_match('/[^aeioudgkprt]hes$/', $str)) + { + // Remove "es" + $str = substr($str, 0, -2); + } + elseif (preg_match('/[^aeiou]ies$/', $str)) + { + $str = substr($str, 0, -3).'y'; + } + elseif (substr($str, -1) === 's' AND substr($str, -2) !== 'ss') + { + $str = substr($str, 0, -1); + } + + return self::$cache[$key] = $str; + } + + /** + * Makes a singular word plural. + * + * @param string word to pluralize + * @return string + */ + public static function plural($str, $count = NULL) + { + // Remove garbage + $str = strtolower(trim($str)); + + if (is_string($count)) + { + // Convert to integer when using a digit string + $count = (int) $count; + } + + // Do nothing with singular + if ($count === 1) + return $str; + + // Cache key name + $key = 'plural_'.$str.$count; + + if (isset(self::$cache[$key])) + return self::$cache[$key]; + + if (inflector::uncountable($str)) + return self::$cache[$key] = $str; + + if (empty(self::$irregular)) + { + // Cache irregular words + self::$irregular = Kohana::config('inflector.irregular'); + } + + if (isset(self::$irregular[$str])) + { + $str = self::$irregular[$str]; + } + elseif (preg_match('/[sxz]$/', $str) OR preg_match('/[^aeioudgkprt]h$/', $str)) + { + $str .= 'es'; + } + elseif (preg_match('/[^aeiou]y$/', $str)) + { + // Change "y" to "ies" + $str = substr_replace($str, 'ies', -1); + } + else + { + $str .= 's'; + } + + // Set the cache and return + return self::$cache[$key] = $str; + } + + /** + * Makes a phrase camel case. + * + * @param string phrase to camelize + * @return string + */ + public static function camelize($str) + { + $str = 'x'.strtolower(trim($str)); + $str = ucwords(preg_replace('/[\s_]+/', ' ', $str)); + + return substr(str_replace(' ', '', $str), 1); + } + + /** + * Makes a phrase underscored instead of spaced. + * + * @param string phrase to underscore + * @return string + */ + public static function underscore($str) + { + return preg_replace('/\s+/', '_', trim($str)); + } + + /** + * Makes an underscored or dashed phrase human-reable. + * + * @param string phrase to make human-reable + * @return string + */ + public static function humanize($str) + { + return preg_replace('/[_-]+/', ' ', trim($str)); + } + +} // End inflector
\ No newline at end of file |
