Completed
Pull Request — master (#206)
by Albert
02:28
created

MessageParser::createUploadedFile()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Proengsoft\JsValidation\Javascript;
4
5
use Proengsoft\JsValidation\Support\DelegatedValidator;
6
use Symfony\Component\HttpFoundation\File\UploadedFile;
7
use Proengsoft\JsValidation\Support\UseDelegatedValidatorTrait;
8
9
class MessageParser
10
{
11
    use UseDelegatedValidatorTrait;
12
13
    /**
14
     * Create a new JsValidation instance.
15
     *
16
     * @param \Proengsoft\JsValidation\Support\DelegatedValidator $validator
17
     */
18 13
    public function __construct(DelegatedValidator $validator)
19
    {
20 13
        $this->validator = $validator;
21 13
    }
22
23
    /**
24
     *  Replace javascript error message place-holders with actual values.
25
     *
26
     * @param string $attribute
27
     * @param string $rule
28
     * @param array  $parameters
29
     *
30
     * @return mixed
31
     */
32 3
    public function getMessage($attribute, $rule, $parameters)
33
    {
34 3
        $data = $this->fakeValidationData($attribute, $rule, $parameters);
35
36 3
        $message = $this->validator->getMessage($attribute, $rule);
37 3
        $message = $this->validator->doReplacements($message, $attribute, $rule, $parameters);
38
39 3
        $this->validator->setData($data);
40
41 3
        return $message;
42
    }
43
44
    /**
45
     * Creates fake data needed to parse messages
46
     * Returns original data.
47
     *
48
     * @param string $attribute
49
     * @param string $rule
50
     * @param $parameters
51
     *
52
     * @return array
53
     */
54 3
    protected function fakeValidationData($attribute, $rule, $parameters)
55
    {
56 3
        $data = $this->validator->getData();
57
58 3
        $this->fakeFileData($data, $attribute);
59 3
        $this->fakeRequiredIfData($data, $rule, $parameters);
60
61 3
        return $data;
62
    }
63
64
    /**
65
     * Generate fake data to get RequiredIf message.
66
     *
67
     * @param $data
68
     * @param $rule
69
     * @param $parameters
70
     */
71 3
    private function fakeRequiredIfData($data, $rule, $parameters)
72
    {
73 3
        if ($rule !== 'RequiredIf') {
74 2
            return;
75
        }
76
77 1
        $newData = $data;
78 1
        $newData[$parameters[0]] = $parameters[1];
79 1
        $this->validator->setData($newData);
80 1
    }
81
82
    /**
83
     * Generate fake data to get file type messages.
84
     *
85
     * @param $data
86
     * @param $attribute
87
     */
88 3
    private function fakeFileData($data, $attribute)
89
    {
90 3
        if (! $this->validator->hasRule($attribute, array('Mimes', 'Image'))) {
91 2
            return;
92
        }
93
94 1
        $newFiles = $data;
95 1
        $newFiles[$attribute] = $this->createUploadedFile();
96 1
        $this->validator->setData($newFiles);
97 1
    }
98
99
    /**
100
     * Create fake UploadedFile to generate file messages.
101
     *
102
     * @return UploadedFile
103
     */
104 1
    protected function createUploadedFile()
105
    {
106 1
        return new UploadedFile('fakefile', 'fakefile', null, null, 'fakefile', true);
107
    }
108
}
109