Completed
Pull Request — master (#17)
by Rougin
11:45
created

InflectorExtension   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 6
c 2
b 0
f 0
lcom 0
cbo 2
dl 0
loc 71
ccs 16
cts 16
cp 1
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A toPluralFormat() 0 4 1
A toSingularFormat() 0 4 1
A toUnderscoreCase() 0 4 1
A toTitleCase() 0 4 1
A getName() 0 4 1
A getFilters() 0 9 1
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