milionkostek /system/helpers/inflector.php

Language PHP Lines 191
MD5 Hash 638888d050ddbf0aa3d6dd5f9b2ab60b Estimated Cost $2,484 (why?)
Repository https://github.com/MHordecki/milionkostek.git View Raw File View Project SPDX
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
<?php defined('SYSPATH') or die('No direct script access.');
/**
 * Inflector helper class.
 *
 * $Id: inflector.php 2512 2008-04-16 17:56:11Z JAAulde $
 *
 * @package    Core
 * @author     Kohana Team
 * @copyright  (c) 2007-2008 Kohana Team
 * @license    http://kohanaphp.com/license.html
 */
class inflector_Core {

	/**
	 * Checks if a word is defined as uncountable.
	 *
	 * @param   string   word to check
	 * @return  boolean
	 */
	public static function uncountable($str)
	{
		static $uncountables = NULL;

		if ($uncountables === NULL)
		{
			// Makes a mirrored array, eg: foo => foo
			$uncountables = array_flip(Kohana::lang('inflector'));
		}

		return isset($uncountables[$str]);
	}

	/**
	 * Makes a plural word singular.
	 *
	 * @param   string  word to singularize
	 * @return  string
	 */
	public static function singular($str, $count = NULL)
	{
		static $cache;

		$str = trim($str);

		// Cache key name
		$key = $str.$count;

		// We can just return uncountable words
		if (inflector::uncountable($str))
			return $str;

		if (is_string($count) AND ctype_digit($count))
		{
			// Convert to integer when using a digit string
			$count = (int) $count;
		}

		if ($cache === NULL)
		{
			// Initialize the cache
			$cache = array();
		}
		else
		{
			// Already pluralized
			if (isset($cache[$key]))
				return $cache[$key];
		}

		// Do nothing with a single count
		if (is_int($count) AND $count === 0 OR $count > 1)
			return $str;

		if (substr($str, -3) === 'ies')
		{
			$str = substr($str, 0, strlen($str) - 3).'y';
		}
		elseif (substr($str, -4) === 'sses' OR substr($str, -3) === 'xes')
		{
			$str = substr($str, 0, strlen($str) - 2);
		}
		elseif (substr($str, -1) === 's')
		{
			$str = substr($str, 0, strlen($str) - 1);
		}

		return $cache[$key] = $str;
	}

	/**
	 * Makes a singular word plural.
	 *
	 * @param   string  word to pluralize
	 * @return  string
	 */
	public static function plural($str, $count = NULL)
	{
		static $cache;

		$str = trim($str);

		// Cache key name
		$key = $str.$count;

		// We can just return uncountable words
		if (inflector::uncountable($str))
			return $str;

		if (is_string($count) AND ctype_digit($count))
		{
			// Convert to integer when using a digit string
			$count = (int) $count;
		}

		if ($cache === NULL)
		{
			// Initialize the cache
			$cache = array();
		}
		else
		{
			// Already pluralized
			if (isset($cache[$key]))
				return $cache[$key];
		}

		// If the count is one, do not pluralize
		if ($count === 1)
			return $str;

		$end = substr($str, -1);
		$low = (strcmp($end, strtolower($end)) === 0) ? TRUE : FALSE;

		if (preg_match('/[sxz]$/i', $str) OR preg_match('/[^aeioudgkprt]h$/i', $str))
		{
			$end = 'es';
			$str .= ($low == FALSE) ? strtoupper($end) : $end;
		}
		elseif (preg_match('/[^aeiou]y$/i', $str))
		{
			$end = 'ies';
			$end = ($low == FALSE) ? strtoupper($end) : $end;
			$str = substr_replace($str, $end, -1);
		}
		else
		{
			$end = 's';
			$str .= ($low == FALSE) ? strtoupper($end) : $end;
		}

		// Set the cache and return
		return $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
Back to Top