MessageParser   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 111
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 3
dl 0
loc 111
ccs 31
cts 31
cp 1
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getMessage() 0 13 2
A fakeValidationData() 0 9 1
A fakeRequiredIfData() 0 10 2
A fakeFileData() 0 10 2
A createUploadedFile() 0 4 1
1
<?php
2
3
namespace Proengsoft\JsValidation\Javascript;
4
5
use Proengsoft\JsValidation\JsValidatorFactory;
6
use Proengsoft\JsValidation\Support\DelegatedValidator;
7
use Proengsoft\JsValidation\Support\UseDelegatedValidatorTrait;
8
use Symfony\Component\HttpFoundation\File\UploadedFile;
9
10
class MessageParser
11
{
12
    use UseDelegatedValidatorTrait;
13
14
    /**
15
     * Whether to escape messages using htmlentities.
16
     *
17
     * @var bool
18
     */
19
    protected $escape;
20
21
    /**
22
     * Create a new JsValidation instance.
23
     *
24
     * @param \Proengsoft\JsValidation\Support\DelegatedValidator $validator
25
     * @param bool $escape
26 168
     */
27
    public function __construct(DelegatedValidator $validator, $escape = false)
28 168
    {
29 168
        $this->validator = $validator;
30 168
        $this->escape = $escape;
31
    }
32
33
    /**
34
     *  Replace javascript error message place-holders with actual values.
35
     *
36
     * @param string $attribute
37
     * @param string $rule
38
     * @param array  $parameters
39
     * @return mixed
40 48
     */
41
    public function getMessage($attribute, $rule, $parameters)
42 48
    {
43
        $attribute = str_replace(JsValidatorFactory::ASTERISK, '*', $attribute);
44 48
45 48
        $data = $this->fakeValidationData($attribute, $rule, $parameters);
46
47 48
        $message = $this->validator->getMessage($attribute, $rule);
48
        $message = $this->validator->makeReplacements($message, $attribute, $rule, $parameters);
49 48
50
        $this->validator->setData($data);
51
52
        return $this->escape ? e($message) : $message;
53
    }
54
55
    /**
56
     * Creates fake data needed to parse messages
57
     * Returns original data.
58
     *
59
     * @param string $attribute
60
     * @param string $rule
61 48
     * @param $parameters
62
     * @return array
63 48
     */
64
    protected function fakeValidationData($attribute, $rule, $parameters)
65 48
    {
66 48
        $data = $this->validator->getData();
67
68 48
        $this->fakeFileData($data, $attribute);
69
        $this->fakeRequiredIfData($data, $rule, $parameters);
70
71
        return $data;
72
    }
73
74
    /**
75
     * Generate fake data to get RequiredIf message.
76
     *
77
     * @param $data
78
     * @param $rule
79 48
     * @param $parameters
80
     * @return void
81 48
     */
82 36
    private function fakeRequiredIfData($data, $rule, $parameters)
83
    {
84
        if ($rule !== 'RequiredIf') {
85 12
            return;
86 12
        }
87 12
88 12
        $newData = $data;
89
        $newData[$parameters[0]] = $parameters[1];
90
        $this->validator->setData($newData);
91
    }
92
93
    /**
94
     * Generate fake data to get file type messages.
95
     *
96
     * @param $data
97 48
     * @param $attribute
98
     * @return void
99 48
     */
100 24
    private function fakeFileData($data, $attribute)
101
    {
102
        if (! $this->validator->hasRule($attribute, ['Mimes', 'Image'])) {
103 24
            return;
104 24
        }
105 24
106 24
        $newFiles = $data;
107
        $newFiles[$attribute] = $this->createUploadedFile();
108
        $this->validator->setData($newFiles);
109
    }
110
111
    /**
112
     * Create fake UploadedFile to generate file messages.
113 24
     *
114
     * @return UploadedFile
115 24
     */
116
    protected function createUploadedFile()
117
    {
118
        return new UploadedFile('fakefile', 'fakefile', null, UPLOAD_ERR_NO_FILE, true);
119
    }
120
}
121