Completed
Push — master ( 3a5568...961737 )
by Andrey
03:56
created

src/Fields.php (1 issue)

possible assignment of superclass instead of class.

Bug Documentation Minor

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Itstructure\FieldWidgets;
4
5
use Itstructure\FieldWidgets\interfaces\LanguageListInterface;
6
use yii\base\{Model, Widget};
7
use yii\db\ActiveRecord;
8
use yii\widgets\ActiveForm;
9
10
/**
11
 * Class Fields
12
 * Widget class for displaying a list of form fields with given types.
13
 *
14
 * @property array $fields
15
 * @property LanguageListInterface|null $languageModel
16
 * @property ActiveRecord $model
17
 * @property ActiveForm $form
18
 *
19
 * @package Itstructure\FieldWidgets
20
 */
21
class Fields extends Widget
22
{
23
    /**
24
     * List of form fields.
25
     * You must set the following parameters for each field:: name, type.
26
     * name (string) - field name, example: title, description.
27
     * type (string) - field type, may be: text, textarea, ckeditor, 'ckeditorAdmin', file, checkbox, dropdown,
28
     * password.
29
     * Optional:
30
     * label (string) - field label.
31
     * data (array) - data to be inserted in to the field.
32
     * options (array) - field options.
33
     * hide (bool) - to switch off the field. Default false.
34
     * Example:
35
     * $fields = [
36
     *      [
37
     *          'name' => 'title',
38
     *          'type' => 'text'
39
     *      ],
40
     *      [
41
     *          'name' => 'description',
42
     *          'type' => 'ckeditor',
43
     *          'label' => 'Description',
44
     *          'preset' => 'custom',
45
     *          'options' => [
46
     *              'toolbarGroups' => [
47
     *                  [
48
     *                      'name' => 'undo'
49
     *                  ],
50
     *                  [
51
     *                      'name' => 'basicstyles',
52
     *                      'groups' => ['basicstyles', 'cleanup']
53
     *                  ],
54
     *                  [
55
     *                      'name' => 'colors'
56
     *                  ],
57
     *                  [
58
     *                      'name' => 'links',
59
     *                      'groups' => ['links', 'insert']
60
     *                  ],
61
     *                  [
62
     *                      'name' => 'others',
63
     *                      'groups' => ['others', 'about']
64
     *                  ],
65
     *                  ],
66
     *              'filebrowserBrowseUrl' => '/ckfinder/ckfinder.html',
67
     *              'filebrowserImageBrowseUrl' => '/ckfinder/ckfinder.html?type=Images',
68
     *              'filebrowserUploadUrl' => '/ckfinder/core/connector/php/connector.php?command=QuickUpload&type=Files',
69
     *              'filebrowserImageUploadUrl' => '/ckfinder/core/connector/php/connector.php?command=QuickUpload&type=Images',
70
     *              'filebrowserWindowWidth' => '1000',
71
     *              'filebrowserWindowHeight' => '700',
72
     *          ]
73
     *      ],
74
     *      [
75
     *          'name' => 'roles',
76
     *          'type' => 'checkbox',
77
     *          'label' => 'Roles',
78
     *          'data' => ArrayHelper::map($roles, 'name', 'name'),
79
     *          'options' => ['style' => 'width: 50%;'],
80
     *      ],
81
     * ];
82
     *
83
     * @var array
84
     */
85
    public $fields = [];
86
87
    /**
88
     * Language model.
89
     * For multilanguage mode.
90
     *
91
     * @var LanguageListInterface
92
     */
93
    private $languageModel = null;
94
95
    /**
96
     * Model with fields.
97
     *
98
     * @var ActiveRecord
99
     */
100
    private $model;
101
102
    /**
103
     * Form object.
104
     *
105
     * @var ActiveForm
106
     */
107
    private $form;
108
109
    /**
110
     * Set the language model.
111
     * For multilanguage mode.
112
     *
113
     * @param mixed $languageModel
114
     *
115
     * @return void
116
     */
117
    public function setLanguageModel(LanguageListInterface $languageModel): void
118
    {
119
        $this->languageModel = $languageModel;
120
    }
121
122
    /**
123
     * Set model.
124
     *
125
     * @param Model $model
126
     *
127
     * @return void
128
     */
129
    public function setModel(Model $model): void
130
    {
131
        $this->model = $model;
0 ignored issues
show
Documentation Bug introduced by Andrey Girnik
$model is of type object<yii\base\Model>, but the property $model was declared to be of type object<yii\db\ActiveRecord>. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
132
    }
133
134
    /**
135
     * Set form.
136
     *
137
     * @param mixed $form
138
     *
139
     * @return void
140
     */
141
    public function setForm(ActiveForm $form): void
142
    {
143
        $this->form = $form;
144
    }
145
146
    /**
147
     * Starts the output widget of the form fields list with the specified types.
148
     *
149
     * @return string
150
     */
151
    public function run(): string
152
    {
153
        $params = [
154
            'fields' => $this->fields,
155
            'model'  => $this->model,
156
            'form'   => $this->form,
157
        ];
158
159
        if ($this->languageModel === null){
160
            $template = 'fieldsSimple';
161
        } else {
162
            $template = 'fieldsMultilanguage';
163
            $params['languageModel'] = $this->languageModel;
164
        }
165
166
        return $this->render($template, $params);
167
    }
168
}
169