Completed
Branch delegated-factory (c8a9c9)
by Albert
02:06
created

MessageParser::fakeValidationData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 0
loc 10
rs 9.4286
cc 1
eloc 6
nc 1
nop 3
1
<?php
2
3
namespace Proengsoft\JsValidation\Javascript;
4
use Proengsoft\JsValidation\Support\DelegatedValidator;
5
use Proengsoft\JsValidation\Support\UseDelegatedValidatorTrait;
6
7
class MessageParser
8
{
9
    use UseDelegatedValidatorTrait;
10
11
    /**
12
     * Create a new JsValidation instance.
13
     *
14
     * @param \Proengsoft\JsValidation\Support\DelegatedValidator $validator
15
     */
16
    public function __construct(DelegatedValidator $validator)
17
    {
18
        $this->validator = $validator;
19
    }
20
21
    /**
22
     *  Replace javascript error message place-holders with actual values.
23
     *
24
     * @param string $attribute
25
     * @param string $rule
26
     * @param array  $parameters
27
     *
28
     * @return mixed
29
     */
30
    public function getJsMessage($attribute, $rule, $parameters)
31
    {
32
33
        $data = $this->fakeValidationData($attribute, $rule, $parameters);
34
35
        $message = $this->validator->getMessage($attribute, $rule);
36
        $message = $this->validator->doReplacements($message, $attribute, $rule, $parameters);
37
38
        $this->setValidationData($data);
39
40
        return $message;
41
    }
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
    protected function fakeValidationData($attribute, $rule, $parameters) {
55
        $files = $this->validator->getFiles();
56
        $data = $this->validator->getData();
57
58
        $this->fakeFileData($files, $attribute);
59
        $this->fakeRequiredIfData($data, $rule, $parameters);
60
61
        return compact('data', 'files');
62
63
    }
64
65
    /**
66
     * Generate fake data to get RequiredIf message
67
     *
68
     * @param $data
69
     * @param $rule
70
     * @param $parameters
71
     */
72
    private function fakeRequiredIfData($data, $rule, $parameters) {
73
        if ($rule == 'RequiredIf') {
74
75
            $newData = $data;
76
            $newData[$parameters[0]] = $parameters[1];
77
            $this->validator->setData($newData);
78
        }
79
    }
80
81
    /**
82
     * Generate fake data to get file type messages
83
     *
84
     * @param $files
85
     * @param $attribute
86
     */
87
    private function fakeFileData($files, $attribute) {
88
89
        if ($this->validator->hasRule($attribute, array('Mimes', 'Image'))) {
90
            $newFiles = $files;
91
            $newFiles[$attribute] = false;
92
            $this->validator->setFiles($newFiles);
93
        }
94
    }
95
96
    /**
97
     * Sets validation data
98
     *
99
     * @param array $data
100
     */
101
    protected function setValidationData($data) {
102
        $this->validator->setFiles($data['files']);
103
        $this->validator->setData($data['data']);
104
    }
105
106
107
}