source of /sprout/Helpers/Inflector.php
Copyright (C) 2017 Karmabunny Pty Ltd.
This file is a part of SproutCMS.
SproutCMS is free software: you can redistribute it and/or modify it under the terms
of the GNU General Public License as published by the Free Software Foundation, either
version 2 of the License, or (at your option) any later version.
For more information, visit <http://getsproutcms.com>.
This class was originally from Kohana 2.3.4
Copyright 2007-2008 Kohana Team <?php /** * Copyright (C) 2017 Karmabunny Pty Ltd. * * This file is a part of SproutCMS. * * SproutCMS is free software: you can redistribute it and/or modify it under the terms * of the GNU General Public License as published by the Free Software Foundation, either * version 2 of the License, or (at your option) any later version. * * For more information, visit <http://getsproutcms.com>. * * This class was originally from Kohana 2.3.4 * Copyright 2007-2008 Kohana Team */ namespace Sprout\Helpers; use Kohana; /** * Language inflection such as pluralisation. */ class Inflector { // 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 (Inflector::$uncountable === NULL) { // Cache uncountables Inflector::$uncountable = Kohana::config('inflector.uncountable'); // Make uncountables mirroed Inflector ::$uncountable = array_combine(Inflector ::$uncountable, Inflector ::$uncountable); } } /** * 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 { // 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(Inflector ::$cache[$key])) return Inflector::$cache[$key]; if (Inflector::uncountable($str)) return Inflector::$cache[$key] = $str; if (empty(Inflector ::$irregular)) { // Cache irregular words Inflector::$irregular = Kohana::config('inflector.irregular'); } { $str = $irregular; } { // Remove "es" } { $str = substr($str, 0, -3).'y'; } elseif (substr($str, -1) === 's' AND substr($str, -2) !== 'ss') { } return Inflector::$cache[$key] = $str; } /** * Makes a singular word plural. * * @param string word to pluralize * @return string */ public static function plural($str, $count = NULL) { // Remove garbage { // 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(Inflector ::$cache[$key])) return Inflector::$cache[$key]; if (Inflector::uncountable($str)) return Inflector::$cache[$key] = $str; if (empty(Inflector ::$irregular)) { // Cache irregular words Inflector::$irregular = Kohana::config('inflector.irregular'); } if (isset(Inflector ::$irregular[$str])) { $str = Inflector::$irregular[$str]; } { $str .= 'es'; } { // Change "y" to "ies" } else { $str .= 's'; } // Set the cache and return return Inflector::$cache[$key] = $str; } /** * Pluralises a number and word combination, e.g. [1, 'pig'] => '1 pig'; [0, 'pig'] => '0 pigs' * @param int $num The number in question * @param string $word The word to pluralise if $num is not 1 */ public static function numPlural($num, $word) { if ($num == 1) return "1 {$word}"; return $num . ' ' . self::plural($word); } /** * Makes a phrase camel case. * * @param string phrase to camelize * @return string */ public static function camelize($str) { } /** * Makes a phrase underscored instead of spaced. * * @param string phrase to underscore * @return string */ public static function underscore($str) { } /** * Makes an underscored or dashed phrase human-reable. * * @param string phrase to make human-reable * @return string */ public static function humanize($str) { } } // End inflector
|