-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathInflector.php
More file actions
94 lines (80 loc) · 2.09 KB
/
Inflector.php
File metadata and controls
94 lines (80 loc) · 2.09 KB
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
<?php
/**
* @copyright 2014 Integ S.A.
* @license http://opensource.org/licenses/MIT The MIT License (MIT)
* @author Javier Lorenzana <javier.lorenzana@gointegro.com>
*/
namespace GoIntegro\Inflector;
// Utils.
use Doctrine\Common\Inflector\Inflector as DoctrineInflector;
class Inflector
{
/**
* @param string $word
* @param boolean $firstToUpper
* @return string
*/
public static function camelize($word, $firstToUpper = FALSE)
{
$word = str_replace(
' ', '', ucwords(str_replace('-', ' ', $word))
);
if (!$firstToUpper) {
$word = lcfirst($word);
}
return $word;
}
/**
* @param string $word
* @param boolean $consecutive Whether to allow consecutive capitals.
* @return string
*/
public static function hyphenate($word, $consecutive = FALSE)
{
if (!$consecutive) {
$word = preg_replace('/([^^])([A-Z])/', '\\1-\\2', $word);
// Needs to happen after the first one.
$word = preg_replace('/([A-Z])([A-Z])/', '\\1-\\2', $word);
} else {
$word = preg_replace('/([^A-Z])([A-Z])/', '\\1-\\2', $word);
}
return strtolower($word);
}
/**
* @see DoctrineInflector::pluralize
*/
public static function pluralize($word)
{
return DoctrineInflector::pluralize($word);
}
/**
* @see DoctrineInflector::singularize
*/
public static function singularize($word)
{
return DoctrineInflector::singularize($word);
}
/**
* @param string $word
* @return string
*/
public static function typify($word)
{
$word = self::shortenClassName($word);
return self::pluralize(self::hyphenate($word));
}
/**
* @param string $name
* @return string
*/
private static function shortenClassName($name)
{
$position = strrpos($name, '\\');
if (FALSE === $position) {
$position = 0;
} else {
++$position;
}
return substr($name, $position);
}
}