Completed
Push — master ( 6247dd...e88f06 )
by Peter
07:34
created

Strings::decamelize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 2
crap 1
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: peter
5
 * Date: 15.06.18
6
 * Time: 18:49
7
 */
8
9
namespace Maslosoft\Manganel\Helpers;
10
11
12
class Strings
13
{
14
	/**
15
	 *
16
	 * @param string $input
17
	 * @param string $separator
18
	 * @return string
19
	 */
20 1
	public static function decamelize($input, $separator = '-')
21
	{
22 1
		return ltrim(strtolower(preg_replace('/[A-Z]/', "$separator$0", $input)), $separator);
23
	}
24
25
	/**
26
	 * Will return CamelCased value from `input`.
27
	 *
28
	 * Example:
29
	 *
30
	 * ```
31
	 * camelize('my-html-id');
32
	 * ```
33
	 *
34
	 * Will return `MyHtmlId`.
35
	 *
36
	 * Can also accept different separator or make first letter lower case:
37
	 *
38
	 * ```
39
	 * camelize('\My\Php\ClassName', '\\', true);
40
	 * ```
41
	 *
42
	 * Will return `myPhpClassName`. This might be useful for generating HTML id's
43
	 * from PHP class names.
44
	 *
45
	 * @param string $input
46
	 * @param string $separator
47
	 * @return string
48
	 */
49
	public static function camelize($input, $separator = '-', $lcFirst = false)
50
	{
51
		assert(is_string($input));
52
		$spaced = str_replace($separator, ' ', $input);
53
		$result = str_replace(' ', '', ucwords($spaced));
54
		if ($lcFirst)
55
		{
56
			return lcfirst($result);
57
		}
58
		return $result;
59
	}
60
}