Completed
Pull Request — master (#202)
by personal
03:41 queued 42s
created

FormatingExtension::getFunctions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 8
rs 9.4285
cc 1
eloc 5
nc 1
nop 0
1
<?php
2
namespace Hal\Application\Formater\Twig;
3
4
use Hal\Application\Extension\ExtensionService;
5
use Hal\Application\Rule\Validator;
6
7
class FormatingExtension extends \Twig_Extension
8
{
9
    /**
10
     * @var Validator
11
     */
12
    private $validator;
13
14
    /**
15
     * Validator
16
     *
17
     * @param Validator $validator
18
     */
19
    function __construct(Validator $validator)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
20
    {
21
        $this->validator = $validator;
22
    }
23
24
    /**
25
     * @inheritdoc
26
     */
27
    public function getFilters()
28
    {
29
        return array(
30
            new \Twig_SimpleFilter('textify', array($this, 'textify'))
31
            , new \Twig_SimpleFilter('rule', array($this, 'rule'))
32
        );
33
    }
34
35
    /**
36
     * @inheritdoc
37
     */
38
    public function getFunctions()
39
    {
40
        return array(
41
            new \Twig_SimpleFunction('extensions_menu', array($this, 'extensionsMenu'), array('is_safe' => array('html')))
42
            , new \Twig_SimpleFunction('extensions_js', array($this, 'extensionsJs'), array('is_safe' => array('html')))
43
            , new \Twig_SimpleFunction('extensions_content', array($this, 'extensionsContent'), array('is_safe' => array('html')))
44
        );
45
    }
46
47
    /**
48
     * String as readable text
49
     *
50
     * @param $v
51
     * @return string
52
     */
53
    public function textify($v)
54
    {
55
        return ucfirst(preg_replace( '/([a-z0-9])([A-Z])/', "$1 $2", $v ));
56
    }
57
58
    /**
59
     * Check value according rule
60
     *
61
     * @param $key
62
     * @param $v
63
     * @return string
64
     */
65
    public function rule($v, $key)
66
    {
67
        return $this->validator->validate($key, $v);
68
    }
69
70
    /**
71
     * @param ExtensionService $extensions
72
     * @return string
73
     */
74
    public function extensionsMenu(ExtensionService $extensions)
75
    {
76
        $html = '';
77
        foreach($extensions->getRepository()->all() as $extension) {
78
            $helper = $extension->getReporterHtmlSummary();
79
            if(!$helper) {
80
                continue;
81
            }
82
            foreach($helper->getMenus() as $name => $label) {
83
                $html .= sprintf('<li id="link-%s"><a>%s</a></li>', $name, $label);
84
            }
85
        }
86
        return $html;
87
    }
88
89
    /**
90
     * @param ExtensionService $extensions
91
     * @return string
92
     */
93 View Code Duplication
    public function extensionsJs(ExtensionService $extensions)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
94
    {
95
        $html = '';
96
        foreach($extensions->getRepository()->all() as $extension) {
97
            $helper = $extension->getReporterHtmlSummary();
98
            if(!$helper) {
99
                continue;
100
            }
101
            $html .= $helper->renderJs();
102
        }
103
        return $html;
104
    }
105
106
    /**
107
     * @param ExtensionService $extensions
108
     * @return string
109
     */
110 View Code Duplication
    public function extensionsContent(ExtensionService $extensions)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
111
    {
112
        $html = '';
113
        foreach($extensions->getRepository()->all() as $extension) {
114
            $helper = $extension->getReporterHtmlSummary();
115
            if(!$helper) {
116
                continue;
117
            }
118
            $html .= $helper->renderHtml();
119
        }
120
        return $html;
121
    }
122
123
    /**
124
     * @inherit
125
     */
126
    public function getName()
127
    {
128
        return 'hal_formating_extension';
129
    }
130
}
131