Issues (9)

Model/ChangesStreetLine.php (2 issues)

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\AdvancedStreetAddress\Model;
10
11
use O2TI\AdvancedStreetAddress\Helper\Config;
12
13
/**
14
 *  ChangesStreetLine - Change Compoments for Inputs.
15
 */
16
class ChangesStreetLine
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 to 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 == 'street') {
43
                $defaultPosition = (int) $fields[$key]['sortOrder'];
44
45
                $fields[$key]['config']['template'] = 'O2TI_AdvancedStreetAddress/form/element/addressline';
46
                $fields[$key]['config']['fieldTemplate'] = 'O2TI_AdvancedStreetAddress/form/field';
47
48
                foreach ($fields[$key]['children'] as $arrayPosition => $streetLine) {
49
                    $fields[$key]['children'][$arrayPosition]['sortOrder'] = $defaultPosition + $arrayPosition;
50
51
                    if ($this->config->getConfigForLabel($arrayPosition, 'use_label')) {
52
                        $labelStreet = $this->config->getConfigForLabel($arrayPosition, 'label');
53
                        $fields[$key]['children'][$arrayPosition]['label'] = __($labelStreet);
54
                    }
55
56
                    if ($isRequired = $this->config->getConfigForValidation($arrayPosition, 'is_number')) {
0 ignored issues
show
The assignment to $isRequired is dead and can be removed.
Loading history...
57
                        // phpcs:ignore
58
                        if ($fields[$key]['children'][$arrayPosition]['config']['elementTmpl'] === 'ui/form/element/input') {
59
                            // phpcs:ignore
60
                            $fields[$key]['children'][$arrayPosition]['config']['elementTmpl'] = 'O2TI_AdvancedStreetAddress/form/element/number';
61
                        // phpcs:ignore
62
                        } elseif ($fields[$key]['children'][$arrayPosition]['config']['elementTmpl'] === 'O2TI_AdvancedFieldsCheckout/form/element/input') {
63
                            $fields[$key]['children'][$arrayPosition]['config']['elementTmpl']
64
                                = 'O2TI_AdvancedStreetAddress/form/element/O2TI/AdvancedStreetAddress/number';
65
                        }
66
                    }
67
68
                    $isRequired = $this->config->getConfigForValidation($arrayPosition, 'is_required');
69
                    $maxLength = $this->config->getConfigForValidation($arrayPosition, 'max_length');
70
                    $fields[$key]['children'][$arrayPosition]['validation'] = [
71
                        'min_text_length' => 1,
72
                        'max_text_length' => $maxLength,
73
                    ];
74
                    if ($isRequired) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $isRequired of type integer|null is loosely compared to true; this is ambiguous if the integer can be 0. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
75
                        $fields[$key]['children'][$arrayPosition]['validation'] = [
76
                            'required-entry'  => $isRequired,
77
                            'min_text_length' => 1,
78
                            'max_text_length' => $maxLength,
79
                        ];
80
                    }
81
                }
82
            }
83
        }
84
85
        return $fields;
86
    }
87
}
88