Completed
Push — master ( 71ec50...18d3f1 )
by Albert
02:10
created

MessageParser::fakeFileData()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 3
Bugs 2 Features 0
Metric Value
c 3
b 2
f 0
dl 0
loc 10
ccs 7
cts 7
cp 1
rs 9.4286
cc 2
eloc 6
nc 2
nop 2
crap 2
1
<?php
2
3
namespace Proengsoft\JsValidation\Javascript;
4
5
use Proengsoft\JsValidation\Support\DelegatedValidator;
6
use Proengsoft\JsValidation\Support\UseDelegatedValidatorTrait;
7
8
class MessageParser
9
{
10
    use UseDelegatedValidatorTrait;
11
12
    /**
13
     * Create a new JsValidation instance.
14
     *
15
     * @param \Proengsoft\JsValidation\Support\DelegatedValidator $validator
16
     */
17 9
    public function __construct(DelegatedValidator $validator)
18
    {
19 9
        $this->validator = $validator;
20 9
    }
21
22
    /**
23
     *  Replace javascript error message place-holders with actual values.
24
     *
25
     * @param string $attribute
26
     * @param string $rule
27
     * @param array  $parameters
28
     *
29
     * @return mixed
30
     */
31 3
    public function getMessage($attribute, $rule, $parameters)
32
    {
33 3
        $data = $this->fakeValidationData($attribute, $rule, $parameters);
34
35 3
        $message = $this->validator->getMessage($attribute, $rule);
36 3
        $message = $this->validator->doReplacements($message, $attribute, $rule, $parameters);
37
38 3
        $this->setValidationData($data);
39
40 3
        return $message;
41
    }
42
43
    /**
44
     * Creates fake data needed to parse messages
45
     * Returns original data.
46
     *
47
     * @param string $attribute
48
     * @param string $rule
49
     * @param $parameters
50
     *
51
     * @return array
52
     */
53 3
    protected function fakeValidationData($attribute, $rule, $parameters)
54
    {
55 3
        $files = $this->validator->getFiles();
56 3
        $data = $this->validator->getData();
57
58 3
        $this->fakeFileData($files, $attribute);
59 3
        $this->fakeRequiredIfData($data, $rule, $parameters);
60
61 3
        return compact('data', 'files');
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 $files
86
     * @param $attribute
87
     */
88 3
    private function fakeFileData($files, $attribute)
89
    {
90 3
        if (! $this->validator->hasRule($attribute, array('Mimes', 'Image'))) {
91 2
            return;
92
        }
93
94 1
        $newFiles = $files;
95 1
        $newFiles[$attribute] = false;
96 1
        $this->validator->setFiles($newFiles);
97 1
    }
98
99
    /**
100
     * Sets validation data.
101
     *
102
     * @param array $data
103
     */
104 3
    protected function setValidationData($data)
105
    {
106 3
        $this->validator->setFiles($data['files']);
107 3
        $this->validator->setData($data['data']);
108 3
    }
109
}
110