helpers.php ➔ capitalize()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 2
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of the ICanBoogie package.
5
 *
6
 * (c) Olivier Laviale <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace ICanBoogie;
13
14
/**
15
 * Default inflector locale.
16
 */
17
const INFLECTOR_DEFAULT_LOCALE = 'en';
18
19
if (!function_exists(__NAMESPACE__ . '\downcase'))
20
{
21
	/**
22
	 * Returns an lowercase string.
23
	 */
24
	function downcase(string $str): string
25
	{
26
		return mb_strtolower($str);
27
	}
28
}
29
30
if (!function_exists(__NAMESPACE__ . '\upcase'))
31
{
32
	/**
33
	 * Returns an uppercase string.
34
	 */
35
	function upcase(string $str): string
36
	{
37
		return mb_strtoupper($str);
38
	}
39
}
40
41
if (!function_exists(__NAMESPACE__ . '\capitalize'))
42
{
43
	/**
44
	 * Returns a copy of str with the first character converted to uppercase and the
45
	 * remainder to lowercase.
46
	 *
47
	 * @param bool $preserve_str_end Whether the string end should be preserved or downcased.
48
	 */
49
	function capitalize(string $str, bool $preserve_str_end = false): string
50
	{
51
		$end = mb_substr($str, 1);
52
53
		if (!$preserve_str_end) {
54
			$end = downcase($end);
55
		}
56
57
		return upcase(mb_substr($str, 0, 1)) . $end;
58
	}
59
}
60
61
/**
62
 * Forwards calls to `Inflector::get()->pluralize()`.
63
 */
64
function pluralize(string $word, string $locale = INFLECTOR_DEFAULT_LOCALE): string
65
{
66
	return Inflector::get($locale)->pluralize($word);
67
}
68
69
/**
70
 * Forwards calls to `Inflector::get()->singularize()`.
71
 */
72
function singularize(string $word, string $locale = INFLECTOR_DEFAULT_LOCALE): string
73
{
74
	return Inflector::get($locale)->singularize($word);
75
}
76
77
/**
78
 * Forwards calls to `Inflector::get()->camelize()`.
79
 */
80
function camelize(string $str, bool $uppercase_first_letter = false, string $locale = INFLECTOR_DEFAULT_LOCALE): string
81
{
82
	return Inflector::get($locale)->camelize($str, $uppercase_first_letter);
83
}
84
85
/**
86
 * Forwards calls to `Inflector::get()->underscore()`.
87
 */
88
function underscore(string $camel_cased_word, string $locale = INFLECTOR_DEFAULT_LOCALE): string
89
{
90
	return Inflector::get($locale)->underscore($camel_cased_word);
91
}
92
93
/**
94
 * Forwards calls to `Inflector::get()->hyphenate()`.
95
 */
96
function hyphenate(string $str, string $locale = INFLECTOR_DEFAULT_LOCALE): string
97
{
98
	return Inflector::get($locale)->hyphenate($str);
99
}
100
101
/**
102
 * Forwards calls to `Inflector::get()->humanize()`.
103
 */
104
function humanize(string $lower_case_and_underscored_word, string $locale = INFLECTOR_DEFAULT_LOCALE): string
105
{
106
	return Inflector::get($locale)->humanize($lower_case_and_underscored_word);
107
}
108
109
/**
110
 * Forwards calls to `Inflector::get()->titleize()`.
111
 */
112
function titleize(string $str, string $locale = INFLECTOR_DEFAULT_LOCALE): string
113
{
114
	return Inflector::get($locale)->titleize($str);
115
}
116