Passed
Push — master ( 6f914d...48f229 )
by Tõnis
03:00
created

AddressForm::run()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
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
    /**
58
     * @var array config yii\bootstrap\ActiveForm
59
     */
60
    public $formOptions = [];
61
62
63
    public function init()
64
    {
65
        parent::init();
66
        $this->registerTranslations();
67
        $this->module = Yii::$app->getModule('addressform');
68
69
        if (empty($this->address)) {
70
            $this->address = new Address();
71
        } else {
72
            $this->address->validate();
73
        }
74
75
        if (count($this->allowedCountries) === 1) {
76
            $this->country = country($this->allowedCountries[0]);
77
        }
78
79
80
        $this->placeHolders = [
81
            'name' => Yii::t("addressform", "John Doe"),
82
            'country' => Yii::t("addressform", "Select country"),
83
            'state' => Yii::t("addressform", "Select state"),
84
            'city' => Yii::t("addressform", "New York City"),
85
            'postCode' => Yii::t("addressform", "Post Code"),
86
            'addressLine1' => Yii::t("addressform", "350 Fifth Avenue"),
87
            'addressLine2' => Yii::t("addressform", "Section, floor, etc."),
88
        ];
89
    }
90
91
92
93
94
    public function run()
95
    {
96
        return $this->render('form', [
97
            'widget' => $this
98
        ]);
99
    }
100
101
    /**
102
     * @return string[]
103
     */
104
    public function getCountryList()
105
    {
106
107
        $countryList = [];
108
        foreach ($this->countries as $country) {
109
            $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

109
            $countryList[$country->getIsoAlpha2()] = /** @scrutinizer ignore-type */ $country->getEmoji(). " " . $country->getName();
Loading history...
110
        }
111
        return $countryList;
112
    }
113
114
    /**
115
     * @return Country
116
     */
117
    public function getCountry()
118
    {
119
        return $this->country;
120
    }
121
122
    /**
123
     * @return Country[]
124
     */
125
    public function getCountries()
126
    {
127
        $countries = [];
128
        if (empty($this->allowedCountries)) {
129
            foreach (\countries() as $countryCode => $countryAttributes) {
130
                $country = country($countryCode);
131
                $countries[$country->getIsoAlpha2()] = $country;
132
            }
133
            return $countries;
134
        }
135
136
        foreach ($this->allowedCountries as $countryCode) {
137
            $countries[$countryCode] = country($countryCode);
138
        }
139
        return $countries;
140
141
    }
142
143
144
}