Passed
Push — master ( 48f229...0598c4 )
by Tõnis
02:29
created

AddressForm::setDefaults()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.2
c 0
b 0
f 0
cc 4
eloc 8
nc 8
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 $name = "address-form";
38
39
    /** @var array $placeHolders Form placeholders */
40
    public $placeHolders;
41
42
    public $fieldIdPrefix = "address-form-";
43
44
    /** @var Module */
45
    public $module;
46
47
    /** @var ActiveForm */
48
    public $form;
49
50
    /** @var array Fields to disable */
51
    public $disabledFields = [];
52
53
    /** @var Address */
54
    public $address;
55
56
    /**
57
     * @var array config yii\bootstrap\ActiveForm
58
     */
59
    public $formOptions = [];
60
61
    /** @var string */
62
    public $submitText;
63
64
    /** @var array $submitOptions  the HTML options for submit button */
65
    public $submitOptions = [
66
        'class' => 'btn btn-primary',
67
    ];
68
69
    public function init()
70
    {
71
        parent::init();
72
        $this->registerTranslations();
73
        $this->module = Yii::$app->getModule('addressform');
74
75
        $this->setDefaults();
76
77
        $this->placeHolders = [
78
            'name' => Yii::t("addressform", "John Doe"),
79
            'country' => Yii::t("addressform", "Select country"),
80
            'state' => Yii::t("addressform", "Select state"),
81
            'city' => Yii::t("addressform", "New York City"),
82
            'postCode' => Yii::t("addressform", "10001"),
83
            'addressLine1' => Yii::t("addressform", "350 Fifth Avenue"),
84
            'addressLine2' => Yii::t("addressform", "2nd floor, room 203"),
85
        ];
86
    }
87
88
89
90
91
    public function run()
92
    {
93
        return $this->render('form', [
94
            'widget' => $this
95
        ]);
96
    }
97
98
    /**
99
     * @return string[]
100
     */
101
    public function getCountryList()
102
    {
103
104
        $countryList = [];
105
        foreach ($this->countries as $country) {
106
            $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

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