Passed
Push[email protected] ( be84d5...130fec )
by Bruno
03:41
created

Placeholder::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 4
rs 10
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\AdvancedFieldsCheckout\Model;
10
11
use O2TI\AdvancedFieldsCheckout\Helper\Config;
12
13
/**
14
 *  Placeholder - Implements Placeholder for Inputs.
15
 */
16
class Placeholder
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
     * Set Components Placeholder in Fields.
34
     *
35
     * @param array $fields
36
     *
37
     * @return array
38
     */
39
    public function setPlaceholder(array $fields): array
40
    {
41
        foreach ($fields as $key => $data) {
42
            if (in_array('label', $fields[$key])) {
43
                if ($fields[$key]['label']) {
44
                    $placeholder = $this->config->getPlaceholderForField($key);
45
                    $label = $fields[$key]['label'];
46
                    $fields[$key]['config']['placeholder'] = __($label);
47
                    if ($placeholder) {
48
                        $fields[$key]['config']['placeholder'] = __($placeholder);
49
                    }
50
                    if ($key === 'street') {
51
                        $fields = $this->setPlaceholderOfStreetLine($fields, $key);
52
                    }
53
                }
54
            }
55
        }
56
57
        return $fields;
58
    }
59
60
    /**
61
     * Set Components Placeholder of Street Lines.
62
     *
63
     * @param array  $fields
64
     * @param string $field
65
     *
66
     * @return array
67
     */
68
    public function setPlaceholderOfStreetLine(array $fields, string $field): array
69
    {
70
        foreach ($fields[$field]['children'] as $arrayPosition => $streetLine) {
71
            $streetKey = 'street_'.$arrayPosition;
72
            $placeholder = $this->config->getPlaceholderForField($streetKey);
73
            $label = $fields[$field]['children'][$arrayPosition]['label'];
74
            $fields[$field]['children'][$arrayPosition]['config']['placeholder'] = __($label);
75
            if ($placeholder) {
76
                $fields[$field]['children'][$arrayPosition]['config']['placeholder'] = __($placeholder);
77
            }
78
        }
79
80
        return $fields;
81
    }
82
}
83