Passed
Push[email protected] ( 12d207...133761 )
by Bruno
03:42
created

Masking::changeComponentFields()   A

Complexity

Conditions 6
Paths 5

Size

Total Lines 20
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 15
c 1
b 0
f 0
nc 5
nop 1
dl 0
loc 20
rs 9.2222
1
<?php
2
/**
3
 * Copyright © O2TI. All rights reserved.
4
 *
5
 * @author    Bruno Elisei <[email protected]>
6
 * See COPYING.txt for license details.
7
 */
8
9
namespace O2TI\InputMasking\Model;
10
11
use O2TI\InputMasking\Helper\Config;
12
13
/**
14
 *  Masking - Add Masking Compoments for Inputs.
15
 */
16
class Masking
17
{
18
    /**
19
     * @var Config
20
     */
21
    protected $config;
22
23
    /**
24
     * @param Config $config
25
     */
26
    public function __construct(
27
        Config $config
28
    ) {
29
        $this->config = $config;
30
    }
31
32
    /**
33
     * Change Components Fields.
34
     *
35
     * @param array $fields
36
     *
37
     * @return array
38
     */
39
    public function changeComponentFields(array $fields): array
40
    {
41
        foreach ($fields as $key => $data) {
42
            if ($key === 'postcode') {
43
                $defaultPosition = (int) $fields[$key]['sortOrder'];
44
                $fields[$key]['sortOrder'] = $defaultPosition;
45
                $fields[$key]['component'] = 'O2TI_InputMasking/js/view/form/element/postcode';
46
            } elseif ($key === 'vat_id' || $key === 'taxvat') {
47
                $defaultPosition = (int) $fields[$key]['sortOrder'];
48
                $fields[$key]['sortOrder'] = $defaultPosition;
49
                $fields[$key]['component'] = 'O2TI_InputMasking/js/view/form/element/fiscal_document';
50
            } elseif ($key === 'telephone') {
51
                $defaultPosition = (int) $fields[$key]['sortOrder'];
52
                $fields[$key]['sortOrder'] = $defaultPosition;
53
                $fields[$key]['component'] = 'O2TI_InputMasking/js/view/form/element/telephone';
54
            }
55
            continue;
56
        }
57
58
        return $fields;
59
    }
60
61
    /**
62
     * Change Components at Create Mask to Address Fields.
63
     *
64
     * @param array $fields
65
     *
66
     * @return array
67
     */
68
    public function createMaskToAddressFields(array $fields): array
69
    {
70
        foreach ($fields as $key => $data) {
71
            if ((!isset($data['mask']) || !$data['mask'])) {
72
                $useMask = $this->config->getConfigAddressInput($key, 'enable_mask');
73
74
                if ($useMask) {
75
                    $mask = $this->config->getConfigAddressInput($key, 'mask');
76
                    $cleanIfNotMatch = $this->config->getConfigAddressInput($key, 'clean_if_not_match');
77
                    $fields = $this->insertElementsMask($fields, $data, $key, $mask, $cleanIfNotMatch);
0 ignored issues
show
Bug introduced by
It seems like $cleanIfNotMatch can also be of type null; however, parameter $cleanIfNotMatch of O2TI\InputMasking\Model\...g::insertElementsMask() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

77
                    $fields = $this->insertElementsMask($fields, $data, $key, $mask, /** @scrutinizer ignore-type */ $cleanIfNotMatch);
Loading history...
Bug introduced by
It seems like $mask can also be of type null; however, parameter $mask of O2TI\InputMasking\Model\...g::insertElementsMask() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

77
                    $fields = $this->insertElementsMask($fields, $data, $key, /** @scrutinizer ignore-type */ $mask, $cleanIfNotMatch);
Loading history...
78
                }
79
            }
80
        }
81
82
        return $fields;
83
    }
84
85
    /**
86
     * Change Components at Create Mask to Account Fields.
87
     *
88
     * @param array $fields
89
     *
90
     * @return array
91
     */
92
    public function createMaskToAccountFields(array $fields): array
93
    {
94
        foreach ($fields as $key => $data) {
95
            if ((!isset($data['mask']) || !$data['mask'])) {
96
                $useMask = $this->config->getConfigCustomerInput($key, 'enable_mask');
97
98
                if ($useMask) {
99
                    $mask = $this->config->getConfigCustomerInput($key, 'mask');
100
                    $cleanIfNotMatch = $this->config->getConfigCustomerInput($key, 'clean_if_not_match');
101
                    $fields = $this->insertElementsMask($fields, $data, $key, $mask, $cleanIfNotMatch);
0 ignored issues
show
Bug introduced by
It seems like $mask can also be of type null; however, parameter $mask of O2TI\InputMasking\Model\...g::insertElementsMask() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

101
                    $fields = $this->insertElementsMask($fields, $data, $key, /** @scrutinizer ignore-type */ $mask, $cleanIfNotMatch);
Loading history...
Bug introduced by
It seems like $cleanIfNotMatch can also be of type null; however, parameter $cleanIfNotMatch of O2TI\InputMasking\Model\...g::insertElementsMask() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

101
                    $fields = $this->insertElementsMask($fields, $data, $key, $mask, /** @scrutinizer ignore-type */ $cleanIfNotMatch);
Loading history...
102
                }
103
            }
104
        }
105
106
        return $fields;
107
    }
108
109
    /**
110
     * Insert Mask to Field.
111
     *
112
     * @param array  $fields
113
     * @param array  $data
114
     * @param string $field
115
     * @param string $mask
116
     * @param string $cleanIfNotMatch
117
     *
118
     * @return array
119
     */
120
    public function insertElementsMask(
121
        array $fields,
122
        array $data,
123
        string $field,
124
        string $mask,
125
        string $cleanIfNotMatch
126
    ): array {
127
        if (isset($data['type']) && $data['type'] === 'group'
128
            && isset($data['children']) && !empty($data['children'])
129
        ) {
130
            foreach ($data['children'] as $childrenKey => $childrenData) {
131
                if (!isset($data['mask']) || !$data['mask']) {
132
                    $fields[$field]['children'][$childrenKey]['mask'] = $mask;
133
                    $fields[$field]['children'][$childrenKey]['maskEnable'] = 1;
134
                    $fields[$field]['children'][$childrenKey]['maskClearIfNotMatch'] = $cleanIfNotMatch;
135
                }
136
                $childrenData = $childrenData;
0 ignored issues
show
Unused Code introduced by
The assignment to $childrenData is dead and can be removed.
Loading history...
137
            }
138
        } else {
139
            $fields[$field]['mask'] = $mask;
140
            $fields[$field]['maskEnable'] = 1;
141
            $fields[$field]['maskClearIfNotMatch'] = $cleanIfNotMatch;
142
        }
143
144
        return $fields;
145
    }
146
}
147