1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Petkopara\CrudGeneratorBundle\Twig; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Class CrudTemplateExtension |
7
|
|
|
* @package PetkoparaCrudGeneratorBundle\Twig |
8
|
|
|
* |
9
|
|
|
* This class adds two Twig filters: "humanize_lc" and "humanize_uc". Both of them |
10
|
|
|
* split camelCase and snake_case strings on their uppercase letters and "_" character, |
11
|
|
|
* respectively, to form a more human readable sentence. |
12
|
|
|
* |
13
|
|
|
* The _lc variant returns all words in the sentence in lower case. The _uc variant |
14
|
|
|
* makes all words in the sentence lowercase, except the first word which has its |
15
|
|
|
* first letter uppercase. |
16
|
|
|
* |
17
|
|
|
*/ |
18
|
|
|
class CrudTemplateExtension extends \Twig_Extension |
19
|
|
|
{ |
20
|
|
|
|
21
|
2 |
|
public function getFilters() |
22
|
|
|
{ |
23
|
|
|
return array( |
24
|
2 |
|
new \Twig_SimpleFilter('humanize_lc', array($this, 'humanize_lc')), |
25
|
2 |
|
new \Twig_SimpleFilter('humanize_uc', array($this, 'humanize_uc')) |
26
|
2 |
|
); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Splits the incoming $text string on all uppercase letters and "_" characters, |
31
|
|
|
* and returns the resulting words as a sentence. All words in the output sentence |
32
|
|
|
* are lowercase. |
33
|
|
|
* |
34
|
|
|
* @param string $text |
35
|
|
|
* @return string |
36
|
|
|
*/ |
37
|
6 |
|
public function humanize_lc($text) |
38
|
|
|
{ |
39
|
6 |
|
return strtolower(trim(preg_replace(array('/([A-Z])/', '/[_\s]+/'), array('_$1', ' '), $text))); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Splits the incoming $text string on all uppercase letters and "_" characters, |
44
|
|
|
* and returns the resulting words as a sentence. The first character of the first |
45
|
|
|
* word is set to uppercase, all other letters are set to lowercase. |
46
|
|
|
* |
47
|
|
|
* @param string $text |
48
|
|
|
* @return string |
49
|
|
|
*/ |
50
|
6 |
|
public function humanize_uc($text) |
51
|
|
|
{ |
52
|
6 |
|
return ucfirst($this->humanize_lc($text)); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
} |