Subversion-Projekte lars-tiefland.ci

Revision

Revision 1257 | Revision 2107 | Zur aktuellen Revision | 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
 *
2049 lars 9
 * Copyright (c) 2014 - 2017, 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/)
2049 lars 32
 * @copyright	Copyright (c) 2014 - 2017, British Columbia Institute of Technology (http://bcit.ca/)
68 lars 33
 * @license	http://opensource.org/licenses/MIT	MIT License
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
 
66
		if ( ! is_countable($result))
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',
98
			'/([^us])s$/'		=> '\1'
99
		);
100
 
101
		foreach ($singular_rules as $rule => $replacement)
102
		{
103
			if (preg_match($rule, $result))
104
			{
105
				$result = preg_replace($rule, $replacement, $result);
106
				break;
107
			}
108
		}
109
 
110
		return $result;
111
	}
112
}
113
 
114
// --------------------------------------------------------------------
115
 
116
if ( ! function_exists('plural'))
117
{
118
	/**
119
	 * Plural
120
	 *
121
	 * Takes a singular word and makes it plural
122
	 *
123
	 * @param	string	$str	Input string
124
	 * @return	string
125
	 */
126
	function plural($str)
127
	{
128
		$result = strval($str);
129
 
130
		if ( ! is_countable($result))
131
		{
132
			return $result;
133
		}
134
 
135
		$plural_rules = array(
136
			'/(quiz)$/'                => '\1zes',      // quizzes
137
			'/^(ox)$/'                 => '\1\2en',     // ox
138
			'/([m|l])ouse$/'           => '\1ice',      // mouse, louse
139
			'/(matr|vert|ind)ix|ex$/'  => '\1ices',     // matrix, vertex, index
140
			'/(x|ch|ss|sh)$/'          => '\1es',       // search, switch, fix, box, process, address
141
			'/([^aeiouy]|qu)y$/'       => '\1ies',      // query, ability, agency
142
			'/(hive)$/'                => '\1s',        // archive, hive
143
			'/(?:([^f])fe|([lr])f)$/'  => '\1\2ves',    // half, safe, wife
144
			'/sis$/'                   => 'ses',        // basis, diagnosis
145
			'/([ti])um$/'              => '\1a',        // datum, medium
146
			'/(p)erson$/'              => '\1eople',    // person, salesperson
147
			'/(m)an$/'                 => '\1en',       // man, woman, spokesman
148
			'/(c)hild$/'               => '\1hildren',  // child
149
			'/(buffal|tomat)o$/'       => '\1\2oes',    // buffalo, tomato
150
			'/(bu|campu)s$/'           => '\1\2ses',    // bus, campus
151
			'/(alias|status|virus)$/'  => '\1es',       // alias
152
			'/(octop)us$/'             => '\1i',        // octopus
153
			'/(ax|cris|test)is$/'      => '\1es',       // axis, crisis
154
			'/s$/'                     => 's',          // no change (compatibility)
155
			'/$/'                      => 's',
156
		);
157
 
158
		foreach ($plural_rules as $rule => $replacement)
159
		{
160
			if (preg_match($rule, $result))
161
			{
162
				$result = preg_replace($rule, $replacement, $result);
163
				break;
164
			}
165
		}
166
 
167
		return $result;
168
	}
169
}
170
 
171
// --------------------------------------------------------------------
172
 
173
if ( ! function_exists('camelize'))
174
{
175
	/**
176
	 * Camelize
177
	 *
178
	 * Takes multiple words separated by spaces or underscores and camelizes them
179
	 *
180
	 * @param	string	$str	Input string
181
	 * @return	string
182
	 */
183
	function camelize($str)
184
	{
185
		return strtolower($str[0]).substr(str_replace(' ', '', ucwords(preg_replace('/[\s_]+/', ' ', $str))), 1);
186
	}
187
}
188
 
189
// --------------------------------------------------------------------
190
 
191
if ( ! function_exists('underscore'))
192
{
193
	/**
194
	 * Underscore
195
	 *
196
	 * Takes multiple words separated by spaces and underscores them
197
	 *
198
	 * @param	string	$str	Input string
199
	 * @return	string
200
	 */
201
	function underscore($str)
202
	{
203
		return preg_replace('/[\s]+/', '_', trim(MB_ENABLED ? mb_strtolower($str) : strtolower($str)));
204
	}
205
}
206
 
207
// --------------------------------------------------------------------
208
 
209
if ( ! function_exists('humanize'))
210
{
211
	/**
212
	 * Humanize
213
	 *
214
	 * Takes multiple words separated by the separator and changes them to spaces
215
	 *
216
	 * @param	string	$str		Input string
217
	 * @param 	string	$separator	Input separator
218
	 * @return	string
219
	 */
220
	function humanize($str, $separator = '_')
221
	{
222
		return ucwords(preg_replace('/['.preg_quote($separator).']+/', ' ', trim(MB_ENABLED ? mb_strtolower($str) : strtolower($str))));
223
	}
224
}
225
 
226
// --------------------------------------------------------------------
227
 
228
if ( ! function_exists('is_countable'))
229
{
230
	/**
231
	 * Checks if the given word has a plural version.
232
	 *
233
	 * @param	string	$word	Word to check
234
	 * @return	bool
235
	 */
236
	function is_countable($word)
237
	{
238
		return ! in_array(
239
			strtolower($word),
240
			array(
1257 lars 241
				'audio',
242
				'bison',
243
				'chassis',
244
				'compensation',
245
				'coreopsis',
246
				'data',
247
				'deer',
248
				'education',
249
				'emoji',
250
				'equipment',
251
				'fish',
252
				'furniture',
253
				'gold',
254
				'information',
255
				'knowledge',
256
				'love',
257
				'rain',
258
				'money',
259
				'moose',
260
				'nutrition',
261
				'offspring',
262
				'plankton',
263
				'pokemon',
264
				'police',
265
				'rice',
266
				'series',
267
				'sheep',
268
				'species',
269
				'swine',
270
				'traffic',
271
				'wheat'
68 lars 272
			)
273
		);
274
	}
275
}