GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 58404d...00b39a )
by Bram
10:57
created

Renderer   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 184
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 9

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 22
lcom 1
cbo 9
dl 0
loc 184
rs 10
c 1
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A setRulePluginManager() 0 4 1
A getRulePluginManager() 0 4 1
A preRenderForm() 0 22 3
A buildInlineJavascript() 0 19 4
B addValidationAttributesForElement() 0 29 5
A getRule() 0 12 3
A addRules() 0 7 2
A addMessages() 0 7 2
A reset() 0 5 1
1
<?php
2
/**
3
 * Renderer for the jquery.validate plugin
4
 *
5
 * @category  StrokerForm
6
 * @package   StrokerForm\Renderer
7
 * @copyright 2012 Bram Gerritsen
8
 * @version   SVN: $Id$
9
 */
10
11
namespace StrokerForm\Renderer\JqueryValidate;
12
13
use StrokerForm\Renderer\JqueryValidate\Rule\RulePluginManager;
14
use Zend\Form\Element\Email;
15
use Zend\I18n\Translator\TranslatorAwareInterface;
16
use Zend\Json\Json;
17
use Zend\Validator\EmailAddress;
18
use Zend\Validator\Regex;
19
use Zend\View\Renderer\PhpRenderer as View;
20
use Zend\Form\FormInterface;
21
use StrokerForm\Renderer\AbstractValidateRenderer;
22
use Zend\Validator\ValidatorInterface;
23
use Zend\Form\ElementInterface;
24
25
class Renderer extends AbstractValidateRenderer
26
{
27
    /**
28
     * @var array
29
     */
30
    protected $skipValidators = array(
31
        'Explode',
32
        'Upload'
33
    );
34
35
    /**
36
     * @var array
37
     */
38
    protected $rules = array();
39
40
    /**
41
     * @var array
42
     */
43
    protected $messages = array();
44
45
    /**
46
     * @var RulePluginManager
47
     */
48
    protected $rulePluginManager;
49
50
    /**
51
     * @param RulePluginManager $rulePluginManager
52
     */
53
    public function setRulePluginManager(RulePluginManager $rulePluginManager)
54
    {
55
        $this->rulePluginManager = $rulePluginManager;
56
    }
57
58
    /**
59
     * @return RulePluginManager
60
     */
61
    public function getRulePluginManager()
62
    {
63
        return $this->rulePluginManager;
64
    }
65
66
    /**
67
     * Executed before the ZF2 view helper renders the element
68
     *
69
     * @param string $formAlias
70
     * @param \Zend\View\Renderer\PhpRenderer $view
71
     * @param \Zend\Form\FormInterface $form
72
     * @param array $options
73
     * @return FormInterface
74
     */
75
    public function preRenderForm($formAlias, View $view, FormInterface $form = null, array $options = array())
76
    {
77
        $form = parent::preRenderForm($formAlias, $view, $form, $options);
78
79
        /** @var $options Options */
80
        $options = $this->getOptions();
81
82
        $inlineScript = $view->plugin('inlineScript');
83
        $inlineScript->appendScript($this->buildInlineJavascript($form, $options));
84
85
        if ($options->getIncludeAssets()) {
86
            $assetBaseUri = $this->getHttpRouter()->assemble(array(), array('name' => 'strokerform-asset'));
87
            $inlineScript->appendFile($assetBaseUri . '/jquery_validate/js/jquery.validate.js');
88
            $inlineScript->appendFile($assetBaseUri . '/jquery_validate/js/custom_rules.js');
89
            if ($options->isUseTwitterBootstrap() === true) {
90
                $inlineScript->appendFile($assetBaseUri . '/jquery_validate/js/jquery.validate.bootstrap.js');
91
            }
92
        }
93
        
94
        $this->reset();
95
        return $form;
96
    }
97
98
    /**
99
     * @param  \Zend\Form\FormInterface $form
100
     * @param Options $options
101
     * @return string
102
     */
103
    protected function buildInlineJavascript(FormInterface $form, Options $options)
104
    {
105
        $validateOptions = array();
106
        foreach ($options->getValidateOptions() as $key => $value) {
107
            $value = (is_string($value)) ? $value : var_export($value, true);
108
            $validateOptions[] = '"' . $key . '": ' . $value;
109
        }
110
111
        return sprintf(
112
            $options->getInitializeTrigger(),
113
            sprintf(
114
                '$(\'form[name="%s"]\').each(function() { $(this).validate({%s"rules":%s,"messages":%s}); });',
115
                $form->getName(),
116
                count($validateOptions) > 0 ? implode(',', $validateOptions) . ',' : '',
117
                Json::encode($this->rules),
118
                Json::encode($this->messages)
119
            )
120
        );
121
    }
122
123
    /**
124
     * @param string $formAlias
125
     * @param \Zend\Form\ElementInterface        $element
126
     * @param \Zend\Validator\ValidatorInterface $validator
127
     * @return mixed|void
128
     */
129
    protected function addValidationAttributesForElement($formAlias, ElementInterface $element, ValidatorInterface $validator = null)
130
    {
131
        if (in_array($this->getValidatorClassName($validator), $this->skipValidators)) {
132
            return;
133
        }
134
        if ($element instanceof Email && $validator instanceof Regex) {
135
            $validator = new EmailAddress();
136
        }
137
138
        $rule = $this->getRule($validator);
139
        if ($rule !== null) {
140
            $rules = $rule->getRules($validator, $element);
141
            $messages = $rule->getMessages($validator);
142
        } else {
143
            //fallback ajax
144
            $ajaxUri = $this->getHttpRouter()->assemble(array('form' => $formAlias), array('name' => 'strokerform-ajax-validate'));
145
            $rules = array(
146
                'remote' => array(
147
                    'url' => $ajaxUri,
148
                    'type' => 'POST'
149
                )
150
            );
151
            $messages = array();
152
        }
153
154
        $elementName = $this->getElementName($element);
155
        $this->addRules($elementName, $rules);
156
        $this->addMessages($elementName, $messages);
157
    }
158
159
    /**
160
     * @param  \Zend\Validator\ValidatorInterface $validator
161
     * @return null|Rule\AbstractRule
162
     */
163
    public function getRule(ValidatorInterface $validator = null)
164
    {
165
        $validatorName = lcfirst($this->getValidatorClassName($validator));
166
        if ($this->getRulePluginManager()->has($validatorName)) {
167
            $rule = $this->getRulePluginManager()->get($validatorName);
168
            if ($rule instanceof TranslatorAwareInterface) {
169
                $rule->setTranslatorTextDomain($this->getTranslatorTextDomain());
170
            }
171
            return $rule;
172
        }
173
        return null;
174
    }
175
176
    /**
177
     * @param string $elementName
178
     * @param array $rules
179
     */
180
    protected function addRules($elementName, array $rules = array())
181
    {
182
        if (!isset($this->rules[$elementName])) {
183
            $this->rules[$elementName] = array();
184
        }
185
        $this->rules[$elementName] = array_merge($this->rules[$elementName], $rules);
186
    }
187
188
    /**
189
     * @param string $elementName
190
     * @param array $messages
191
     */
192
    protected function addMessages($elementName, array $messages = array())
193
    {
194
        if (!isset($this->messages[$elementName])) {
195
            $this->messages[$elementName] = array();
196
        }
197
        $this->messages[$elementName] = array_merge($this->messages[$elementName], $messages);
198
    }
199
200
    /**
201
     * Resets previously set rules and messages, if you have multiple forms on one request
202
     */
203
    protected function reset()
204
    {
205
        $this->rules = array();
206
        $this->messages = array();
207
    }
208
}
209