Subversion-Projekte lars-tiefland.ci

Revision

Revision 2257 | Details | Vergleich mit vorheriger | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
68 lars 1
<?php
2
/**
3
 * CodeIgniter
4
 *
5
 * An open source application development framework for PHP
6
 *
7
 * This content is released under the MIT License (MIT)
8
 *
2414 lars 9
 * Copyright (c) 2014 - 2019, British Columbia Institute of Technology
68 lars 10
 *
11
 * Permission is hereby granted, free of charge, to any person obtaining a copy
12
 * of this software and associated documentation files (the "Software"), to deal
13
 * in the Software without restriction, including without limitation the rights
14
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15
 * copies of the Software, and to permit persons to whom the Software is
16
 * furnished to do so, subject to the following conditions:
17
 *
18
 * The above copyright notice and this permission notice shall be included in
19
 * all copies or substantial portions of the Software.
20
 *
21
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27
 * THE SOFTWARE.
28
 *
29
 * @package	CodeIgniter
30
 * @author	EllisLab Dev Team
31
 * @copyright	Copyright (c) 2008 - 2014, EllisLab, Inc. (https://ellislab.com/)
2414 lars 32
 * @copyright	Copyright (c) 2014 - 2019, British Columbia Institute of Technology (https://bcit.ca/)
33
 * @license	https://opensource.org/licenses/MIT	MIT License
68 lars 34
 * @link	https://codeigniter.com
35
 * @since	Version 1.0.0
36
 * @filesource
37
 */
38
defined('BASEPATH') OR exit('No direct script access allowed');
39
 
40
/**
41
 * CodeIgniter Inflector Helpers
42
 *
43
 * @package		CodeIgniter
44
 * @subpackage	Helpers
45
 * @category	Helpers
46
 * @author		EllisLab Dev Team
47
 * @link		https://codeigniter.com/user_guide/helpers/inflector_helper.html
48
 */
49
 
50
// --------------------------------------------------------------------
51
 
52
if ( ! function_exists('singular'))
53
{
54
	/**
55
	 * Singular
56
	 *
57
	 * Takes a plural word and makes it singular
58
	 *
59
	 * @param	string	$str	Input string
60
	 * @return	string
61
	 */
62
	function singular($str)
63
	{
64
		$result = strval($str);
65
 
2414 lars 66
		if ( ! word_is_countable($result))
68 lars 67
		{
68
			return $result;
69
		}
70
 
71
		$singular_rules = array(
72
			'/(matr)ices$/'		=> '\1ix',
73
			'/(vert|ind)ices$/'	=> '\1ex',
74
			'/^(ox)en/'		=> '\1',
75
			'/(alias)es$/'		=> '\1',
76
			'/([octop|vir])i$/'	=> '\1us',
77
			'/(cris|ax|test)es$/'	=> '\1is',
78
			'/(shoe)s$/'		=> '\1',
79
			'/(o)es$/'		=> '\1',
80
			'/(bus|campus)es$/'	=> '\1',
81
			'/([m|l])ice$/'		=> '\1ouse',
82
			'/(x|ch|ss|sh)es$/'	=> '\1',
83
			'/(m)ovies$/'		=> '\1\2ovie',
84
			'/(s)eries$/'		=> '\1\2eries',
85
			'/([^aeiouy]|qu)ies$/'	=> '\1y',
86
			'/([lr])ves$/'		=> '\1f',
87
			'/(tive)s$/'		=> '\1',
88
			'/(hive)s$/'		=> '\1',
89
			'/([^f])ves$/'		=> '\1fe',
90
			'/(^analy)ses$/'	=> '\1sis',
91
			'/((a)naly|(b)a|(d)iagno|(p)arenthe|(p)rogno|(s)ynop|(t)he)ses$/' => '\1\2sis',
92
			'/([ti])a$/'		=> '\1um',
93
			'/(p)eople$/'		=> '\1\2erson',
94
			'/(m)en$/'		=> '\1an',
95
			'/(s)tatuses$/'		=> '\1\2tatus',
96
			'/(c)hildren$/'		=> '\1\2hild',
97
			'/(n)ews$/'		=> '\1\2ews',
2107 lars 98
			'/(quiz)zes$/'		=> '\1',
68 lars 99
			'/([^us])s$/'		=> '\1'
100
		);
101
 
102
		foreach ($singular_rules as $rule => $replacement)
103
		{
104
			if (preg_match($rule, $result))
105
			{
106
				$result = preg_replace($rule, $replacement, $result);
107
				break;
108
			}
109
		}
110
 
111
		return $result;
112
	}
113
}
114
 
115
// --------------------------------------------------------------------
116
 
117
if ( ! function_exists('plural'))
118
{
119
	/**
120
	 * Plural
121
	 *
122
	 * Takes a singular word and makes it plural
123
	 *
124
	 * @param	string	$str	Input string
125
	 * @return	string
126
	 */
127
	function plural($str)
128
	{
129
		$result = strval($str);
130
 
2414 lars 131
		if ( ! word_is_countable($result))
68 lars 132
		{
133
			return $result;
134
		}
135
 
136
		$plural_rules = array(
137
			'/(quiz)$/'                => '\1zes',      // quizzes
138
			'/^(ox)$/'                 => '\1\2en',     // ox
139
			'/([m|l])ouse$/'           => '\1ice',      // mouse, louse
140
			'/(matr|vert|ind)ix|ex$/'  => '\1ices',     // matrix, vertex, index
141
			'/(x|ch|ss|sh)$/'          => '\1es',       // search, switch, fix, box, process, address
142
			'/([^aeiouy]|qu)y$/'       => '\1ies',      // query, ability, agency
143
			'/(hive)$/'                => '\1s',        // archive, hive
144
			'/(?:([^f])fe|([lr])f)$/'  => '\1\2ves',    // half, safe, wife
145
			'/sis$/'                   => 'ses',        // basis, diagnosis
146
			'/([ti])um$/'              => '\1a',        // datum, medium
147
			'/(p)erson$/'              => '\1eople',    // person, salesperson
148
			'/(m)an$/'                 => '\1en',       // man, woman, spokesman
149
			'/(c)hild$/'               => '\1hildren',  // child
150
			'/(buffal|tomat)o$/'       => '\1\2oes',    // buffalo, tomato
151
			'/(bu|campu)s$/'           => '\1\2ses',    // bus, campus
152
			'/(alias|status|virus)$/'  => '\1es',       // alias
153
			'/(octop)us$/'             => '\1i',        // octopus
154
			'/(ax|cris|test)is$/'      => '\1es',       // axis, crisis
155
			'/s$/'                     => 's',          // no change (compatibility)
156
			'/$/'                      => 's',
157
		);
158
 
159
		foreach ($plural_rules as $rule => $replacement)
160
		{
161
			if (preg_match($rule, $result))
162
			{
163
				$result = preg_replace($rule, $replacement, $result);
164
				break;
165
			}
166
		}
167
 
168
		return $result;
169
	}
170
}
171
 
172
// --------------------------------------------------------------------
173
 
174
if ( ! function_exists('camelize'))
175
{
176
	/**
177
	 * Camelize
178
	 *
179
	 * Takes multiple words separated by spaces or underscores and camelizes them
180
	 *
181
	 * @param	string	$str	Input string
182
	 * @return	string
183
	 */
184
	function camelize($str)
185
	{
186
		return strtolower($str[0]).substr(str_replace(' ', '', ucwords(preg_replace('/[\s_]+/', ' ', $str))), 1);
187
	}
188
}
189
 
190
// --------------------------------------------------------------------
191
 
192
if ( ! function_exists('underscore'))
193
{
194
	/**
195
	 * Underscore
196
	 *
197
	 * Takes multiple words separated by spaces and underscores them
198
	 *
199
	 * @param	string	$str	Input string
200
	 * @return	string
201
	 */
202
	function underscore($str)
203
	{
204
		return preg_replace('/[\s]+/', '_', trim(MB_ENABLED ? mb_strtolower($str) : strtolower($str)));
205
	}
206
}
207
 
208
// --------------------------------------------------------------------
209
 
210
if ( ! function_exists('humanize'))
211
{
212
	/**
213
	 * Humanize
214
	 *
215
	 * Takes multiple words separated by the separator and changes them to spaces
216
	 *
217
	 * @param	string	$str		Input string
218
	 * @param 	string	$separator	Input separator
219
	 * @return	string
220
	 */
221
	function humanize($str, $separator = '_')
222
	{
223
		return ucwords(preg_replace('/['.preg_quote($separator).']+/', ' ', trim(MB_ENABLED ? mb_strtolower($str) : strtolower($str))));
224
	}
225
}
226
 
227
// --------------------------------------------------------------------
228
 
2414 lars 229
if ( ! function_exists('word_is_countable'))
68 lars 230
{
231
	/**
232
	 * Checks if the given word has a plural version.
233
	 *
234
	 * @param	string	$word	Word to check
235
	 * @return	bool
236
	 */
2414 lars 237
	function word_is_countable($word)
68 lars 238
	{
239
		return ! in_array(
240
			strtolower($word),
241
			array(
1257 lars 242
				'audio',
243
				'bison',
244
				'chassis',
245
				'compensation',
246
				'coreopsis',
247
				'data',
248
				'deer',
249
				'education',
250
				'emoji',
251
				'equipment',
252
				'fish',
253
				'furniture',
254
				'gold',
255
				'information',
256
				'knowledge',
257
				'love',
258
				'rain',
259
				'money',
260
				'moose',
261
				'nutrition',
262
				'offspring',
263
				'plankton',
264
				'pokemon',
265
				'police',
266
				'rice',
267
				'series',
268
				'sheep',
269
				'species',
270
				'swine',
271
				'traffic',
272
				'wheat'
68 lars 273
			)
274
		);
275
	}
276
}
2414 lars 277
 
278
// --------------------------------------------------------------------
279
 
280
if ( ! function_exists('is_countable'))
281
{
282
	function is_countable($word)
283
	{
284
		trigger_error('is_countable() is a native PHP function since version 7.3.0; use word_is_countable() instead', E_USER_WARNING);
285
		return word_is_countable($word);
286
	}
287
}