-
Notifications
You must be signed in to change notification settings - Fork 0
/
singular_modifier.php
51 lines (50 loc) · 1.45 KB
/
singular_modifier.php
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
<?php
/**
* Smarty singular modifier plugin
*
* Type: modifier<br>
* Name: singular<br>
* Purpose: append a label to a string that is a number or a number OR
* in the case of an array, count and append label
* return singular or plural form based on numeric input .
* also, can count array if @ sign is prepended as in normal smarty usage.
* defaults to appending basic s modifier to singular form for creating plurals.
* but many times the plural is more complicated than a simple s, so plural can be specified too.
*
*
* @param string OR integer OR array
* @param string
* @param string
* @return string
*
* @author gyaan
*
* @examples
* {assign var='year' value=4}
* {$year|singular:' year'}
*
* {assign var='shelf' value='1'}
* {$shelf|singular:' shelf':' shelves'}
*
* Count ARRAY items using @ sign
* {$shelf|@singular:' shelf':' shelves'}
*/
function smarty_modifier_singular($arr_or_num, $singular_form='', $plural_form='')
{
// no plural form sent, just concat s to singular form
if ($plural_form == '') {
$plural_form = $singular_form . 's';
}
// what datatype we are dealing with
// if modifier is prepended with @ sign we get an array
// count it, concatenate string to the item we counted
if (is_array($arr_or_num)) {
$num = \hiq\util\Arrays::count($arr_or_num);
$form = ($num==1)?$singular_form:$plural_form;
}
if (is_numeric($arr_or_num)) {
$num = $arr_or_num;
$form = ($arr_or_num == 1)?$singular_form:$plural_form;;
}
return $num ." ". $form;
}