Completed
Push — master ( 11ee78...8b3cbf )
by Thomas
04:46
created

NameUtils::toStudlyCase()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 5
nc 1
nop 1
1
<?php
2
namespace keeko\core\utils;
3
4
class NameUtils {
5
6
	/**
7
	 * Transforms a given input into StudlyCase
8
	 *
9
	 * @param string $input
10
	 * @return string
11
	 */
12
	public static function toStudlyCase($input) {
13
		$input = trim($input, '-_');
14
		return ucfirst(preg_replace_callback('/([A-Z-_][a-z]+)/', function($matches) {
15
			return ucfirst(str_replace(['-','_'], '',$matches[0]));
16
		}, $input));
17
	}
18
	
19
	/**
20
	 * Transforms a given input into camelCase
21
	 *
22
	 * @param string $input
23
	 * @return string
24
	 */
25
	public static function toCamelCase($input) {
26
		return lcfirst(self::toStudlyCase($input));
27
	}
28
	
29
	/**
30
	 * Transforms a given input into kebap-case
31
	 *
32
	 * @param string $input
33
	 * @return string
34
	 */
35
	public static function toKebapCase($input) {
36
		return self::dasherize($input);
37
	}
38
39
	/**
40
	 * Transforms a given input into snake_case
41
	 *
42
	 * @param string $input
43
	 * @return string
44
	 */
45
	public static function toSnakeCase($input) {
46
		return str_replace('-', '_', self::dasherize($input));
47
	}
48
	
49
	private static function dasherize($input) {
50
		return trim(strtolower(preg_replace_callback('/([A-Z _])/', function($matches) {
51
			$suffix = '';
52
			if (preg_match('/[A-Z]/', $matches[0])) {
53
				$suffix = $matches[0];
54
			}
55
			return '-' . $suffix;
56
		}, $input)), '-');
57
	}
58
	
59
	/**
60
	 * Returns the plural form of the input
61
	 *
62
	 * @param string $input
63
	 * @return string
64
	 */
65
	public static function pluralize($input) {
66
		if (self::$pluralizer === null) {
67
			self::$pluralizer = new StandardEnglishSingularizer();
68
		}
69
70
		return self::$pluralizer->getPluralForm($input);
71
	}
72
	
73
	/**
74
	 * Returns the singular form of the input
75
	 *
76
	 * @param string $input
77
	 * @return string
78
	 */
79
	public static function singularize($input) {
80
		if (self::$pluralizer === null) {
81
			self::$pluralizer = new StandardEnglishSingularizer();
82
		}
83
	
84
		return self::$pluralizer->getSingularForm($input);
85
	}
86
}