Passed
Push — master ( 655b2f...381d64 )
by Tõnis
02:02
created

AddressForm::setDefaults()   B

Complexity

Conditions 6
Paths 24

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 18
rs 8.8571
c 0
b 0
f 0
cc 6
eloc 11
nc 24
nop 0
1
<?php
2
3
namespace tonisormisson\addressform;
4
5
use tonisormisson\addressform\models\Address;
6
use tonisormisson\addressform\traits\TranslationsTrait;
7
use yii\base\Widget;
8
use Yii;
9
use Rinvex\Country\Country;
10
use yii\widgets\ActiveForm;
11
12
13
/**
14
 * Class AddressForm
15
 * @property array $countryList
16
 * @property Country[] $countries
17
 * @property boolean $isFormInjected
18
 *
19
 *
20
 * @package tonisormisson\addressform
21
 * @author Tõnis Ormisson <[email protected]>
22
 */
23
class AddressForm extends Widget
24
{
25
    use TranslationsTrait;
0 ignored issues
show
Bug introduced by
The trait tonisormisson\addressform\traits\TranslationsTrait requires the property $i18n which is not provided by tonisormisson\addressform\AddressForm.
Loading history...
26
27
    /**
28
     * @var array $allowedCountries list of allowed countries. If list is empty, all countries are used.
29
     */
30
    public $allowedCountries = [];
31
32
    /**
33
     * @var Country
34
     */
35
    private $country;
36
37
    public $id = "address-form";
38
39
    /** @var array $placeHolders Form placeholders */
40
    public $placeHolders;
41
42
    /** @var Module */
43
    public $module;
44
45
    /** @var ActiveForm */
46
    public $form;
47
48
    /** @var array Fields to disable */
49
    public $disabledFields = [];
50
51
    /** @var Address */
52
    public $address;
53
54
    /**
55
     * @var array config yii\bootstrap\ActiveForm
56
     */
57
    public $formOptions = [];
58
59
    /** @var string */
60
    public $submitText;
61
62
63
    /** @var array $submitOptions  the HTML options for submit button */
64
    public $submitOptions = [
65
        'class' => 'btn btn-primary',
66
    ];
67
68
    public $htmlOptions = [];
69
70
    public function init()
71
    {
72
        parent::init();
73
        $this->registerTranslations();
74
        $this->module = Yii::$app->getModule('addressform');
75
76
        $this->setDefaults();
77
78
        $this->placeHolders = [
79
            'name' => Yii::t("addressform", "John Doe"),
80
            'country' => Yii::t("addressform", "Select country"),
81
            'state' => Yii::t("addressform", "Select state"),
82
            'city' => Yii::t("addressform", "New York City"),
83
            'postCode' => Yii::t("addressform", "10001"),
84
            'addressLine1' => Yii::t("addressform", "350 Fifth Avenue"),
85
            'addressLine2' => Yii::t("addressform", "2nd floor, room 203"),
86
        ];
87
    }
88
89
90
91
92
    public function run()
93
    {
94
        return $this->render('form', [
95
            'widget' => $this
96
        ]);
97
    }
98
99
    /**
100
     * @return string[]
101
     */
102
    public function getCountryList()
103
    {
104
105
        $countryList = [];
106
        foreach ($this->countries as $country) {
107
            $countryList[$country->getIsoAlpha2()] = $country->getEmoji(). " " . $country->getName();
0 ignored issues
show
Bug introduced by
Are you sure $country->getEmoji() of type null|array can be used in concatenation? ( Ignorable by Annotation )

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

107
            $countryList[$country->getIsoAlpha2()] = /** @scrutinizer ignore-type */ $country->getEmoji(). " " . $country->getName();
Loading history...
108
        }
109
        return $countryList;
110
    }
111
112
    /**
113
     * @return Country
114
     */
115
    public function getCountry()
116
    {
117
        return $this->country;
118
    }
119
120
    /**
121
     * @return Country[]
122
     */
123
    public function getCountries()
124
    {
125
        $countries = [];
126
        if (empty($this->allowedCountries)) {
127
            foreach (\countries() as $countryCode => $countryAttributes) {
128
                $country = country($countryCode);
129
                $countries[$country->getIsoAlpha2()] = $country;
130
            }
131
            return $countries;
132
        }
133
134
        foreach ($this->allowedCountries as $countryCode) {
135
            $countries[$countryCode] = country($countryCode);
136
        }
137
        return $countries;
138
139
    }
140
141
    private function setDefaults()
142
    {
143
        if (empty($this->address)) {
144
            $this->address = new Address();
145
        }
146
147
        if (count($this->allowedCountries) === 1) {
148
            $this->country = country($this->allowedCountries[0]);
149
            $this->address->country = $this->country->getIsoAlpha2();
150
        }
151
152
        if (empty($this->submitText)) {
153
            $this->submitText = Yii::t("addressform", "Send");
154
        }
155
        $this->htmlOptions['id'] = $this->id;
156
        if (!empty($this->htmlOptions)) {
157
            foreach ($this->htmlOptions as $key => $value) {
158
                $this->htmlOptions[$key] = $value;
159
            }
160
        }
161
162
    }
163
164
165
}