|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Rougin\Combustor\Common; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Twig extension for CodeIgniter's inflector helper. |
|
7
|
|
|
* |
|
8
|
|
|
* @package Combustor |
|
9
|
|
|
* @author Rougin Royce Gutib <[email protected]> |
|
10
|
|
|
*/ |
|
11
|
|
|
class InflectorExtension extends \Twig_Extension |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* Get twig filters |
|
15
|
|
|
* |
|
16
|
|
|
* @return array filters |
|
17
|
|
|
*/ |
|
18
|
12 |
|
public function getFilters() |
|
19
|
|
|
{ |
|
20
|
|
|
return [ |
|
21
|
12 |
|
'plural' => new \Twig_SimpleFilter('plural', [ $this, 'toPluralFormat' ]), |
|
22
|
12 |
|
'singular' => new \Twig_SimpleFilter('singular', [ $this, 'toSingularFormat' ]), |
|
23
|
12 |
|
'title' => new \Twig_SimpleFilter('title', [ $this, 'toTitleCase' ]), |
|
24
|
12 |
|
'underscore' => new \Twig_SimpleFilter('underscore', [ $this, 'toUnderscoreCase' ]), |
|
25
|
12 |
|
]; |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Takes a singular word and makes it plural. |
|
30
|
|
|
* |
|
31
|
|
|
* @param string $input |
|
32
|
|
|
* @return string |
|
33
|
|
|
*/ |
|
34
|
9 |
|
public function toPluralFormat($input) |
|
35
|
|
|
{ |
|
36
|
9 |
|
return plural($input); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Takes a plural word and makes it singular. |
|
41
|
|
|
* |
|
42
|
|
|
* @param string $input |
|
43
|
|
|
* @return string |
|
44
|
|
|
*/ |
|
45
|
12 |
|
public function toSingularFormat($input) |
|
46
|
|
|
{ |
|
47
|
12 |
|
return singular($input); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Convert string to underscore case format |
|
52
|
|
|
* |
|
53
|
|
|
* @param string $input |
|
54
|
|
|
* @return string In underscore case |
|
55
|
|
|
*/ |
|
56
|
12 |
|
public function toUnderscoreCase($input) |
|
57
|
|
|
{ |
|
58
|
12 |
|
return underscore($input); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Convert string to title case format. |
|
63
|
|
|
* |
|
64
|
|
|
* @param string $input |
|
65
|
|
|
* @return string In title case |
|
66
|
|
|
*/ |
|
67
|
9 |
|
public function toTitleCase($input) |
|
68
|
|
|
{ |
|
69
|
9 |
|
return ucwords(humanize($input)); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Get twig extension name |
|
74
|
|
|
* |
|
75
|
|
|
* @return string |
|
76
|
|
|
*/ |
|
77
|
18 |
|
public function getName() |
|
78
|
|
|
{ |
|
79
|
18 |
|
return 'InflectorExtension'; |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
|