Passed
Branch master (cbf361)
by Mihail
04:19
created

Constructor   A

Complexity

Total Complexity 29

Size/Duplication

Total Lines 153
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 153
rs 10
c 0
b 0
f 0
wmc 29

4 Methods

Rating   Name   Duplication   Size   Complexity  
B addValidationProperties() 0 16 6
C makeTag() 0 60 17
A __construct() 0 5 1
B setDefaultProperties() 0 18 5
1
<?php
2
3
namespace Ffcms\Core\Helper\HTML\Form;
4
5
use Ffcms\Core\App;
6
use Ffcms\Core\Arch\Model;
7
use Ffcms\Core\Exception\SyntaxException;
8
use Ffcms\Core\Helper\Type\Any;
9
use Ffcms\Core\Helper\Type\Obj;
10
use Ffcms\Core\Helper\Type\Str;
11
12
/**
13
 * Class Constructor. Form field construction manager.
14
 * @package Ffcms\Core\Helper\HTML\Form
15
 */
16
class Constructor
17
{
18
    const TYPE_TEXT = 'text';
19
    const TYPE_PASSWORD = 'password';
20
    const TYPE_EMAIL = 'email';
21
    const TYPE_CHECKBOX = 'checkbox';
22
    const TYPE_SELECT = 'select';
23
    const TYPE_MULTISELECT = 'multiselect';
24
    const TYPE_TEXTAREA = 'textarea';
25
    const TYPE_MULTI_CHECKBOXES = 'checkboxes';
26
    const TYPE_CAPTCHA = 'captcha';
27
    const TYPE_FILE = 'file';
28
    const TYPE_HIDDEN = 'hidden';
29
    const TYPE_DIV_FAKE = 'div';
30
    const TYPE_RADIO = 'radio';
31
    
32
    /** @var \Ffcms\Core\Arch\Model $model */
33
    private $model;
34
    private $formName;
35
    private $type;
36
    
37
    /**
38
     * Initialize Constructor. Pass model and type inside of current field inside.
39
     * @param \Ffcms\Core\Arch\Model $model
40
     * @param string|null $formName
41
     * @param string $type
42
     */
43
    public function __construct(Model $model, ?string $formName = null, ?string $type = 'text')
44
    {
45
        $this->model = $model;
46
        $this->formName = $formName;
47
        $this->type = $type;
48
    }
49
50
    /**
51
     * @param string|array $name
52
     * @param string|array|null $value
53
     * @param array|null $properties
54
     * @return null|string
55
     */
56
    public function makeTag($name, $value = null, ?array $properties = null): ?string
57
    {
58
        // check if properties is passed well
59
        if ($properties !== null && !Any::isArray($properties)) {
60
            return null;
61
        }
62
        
63
        // add properties to autovalidation by js (properties passed by ref)
64
        $this->addValidationProperties($name, $properties);
0 ignored issues
show
Bug introduced by
It seems like $name can also be of type array; however, parameter $name of Ffcms\Core\Helper\HTML\F...dValidationProperties() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

64
        $this->addValidationProperties(/** @scrutinizer ignore-type */ $name, $properties);
Loading history...
65
        // set default form data as properties: name="", value="", id="" based tag info
66
        $this->setDefaultProperties($name, $value, $properties);
0 ignored issues
show
Bug introduced by
It seems like $value can also be of type array; however, parameter $value of Ffcms\Core\Helper\HTML\F...:setDefaultProperties() does only seem to accept null|string, maybe add an additional type check? ( Ignorable by Annotation )

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

66
        $this->setDefaultProperties($name, /** @scrutinizer ignore-type */ $value, $properties);
Loading history...
Bug introduced by
It seems like $name can also be of type array; however, parameter $name of Ffcms\Core\Helper\HTML\F...:setDefaultProperties() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

66
        $this->setDefaultProperties(/** @scrutinizer ignore-type */ $name, $value, $properties);
Loading history...
67
        
68
        // initialize build model depend of current type
69
        switch ($this->type) {
70
            case static::TYPE_TEXT: // for <input type="text">
71
                $builder = new TextField($properties, $name);
0 ignored issues
show
Bug introduced by
It seems like $name can also be of type array; however, parameter $name of Ffcms\Core\Helper\HTML\F...extField::__construct() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

71
                $builder = new TextField($properties, /** @scrutinizer ignore-type */ $name);
Loading history...
72
                break;
73
            case static::TYPE_CHECKBOX:
74
                $builder = new CheckboxField($properties, $name, $value);
0 ignored issues
show
Bug introduced by
It seems like $name can also be of type array; however, parameter $name of Ffcms\Core\Helper\HTML\F...boxField::__construct() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

74
                $builder = new CheckboxField($properties, /** @scrutinizer ignore-type */ $name, $value);
Loading history...
Bug introduced by
It seems like $value can also be of type array; however, parameter $value of Ffcms\Core\Helper\HTML\F...boxField::__construct() does only seem to accept null|string, maybe add an additional type check? ( Ignorable by Annotation )

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

74
                $builder = new CheckboxField($properties, $name, /** @scrutinizer ignore-type */ $value);
Loading history...
75
                break;
76
            case static::TYPE_PASSWORD:
77
                $builder = new PasswordField($properties, $name);
0 ignored issues
show
Bug introduced by
It seems like $name can also be of type array; however, parameter $name of Ffcms\Core\Helper\HTML\F...ordField::__construct() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

77
                $builder = new PasswordField($properties, /** @scrutinizer ignore-type */ $name);
Loading history...
78
                break;
79
            case static::TYPE_EMAIL:
80
                $builder = new EmailField($properties, $name);
0 ignored issues
show
Bug introduced by
It seems like $name can also be of type array; however, parameter $name of Ffcms\Core\Helper\HTML\F...ailField::__construct() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

80
                $builder = new EmailField($properties, /** @scrutinizer ignore-type */ $name);
Loading history...
81
                break;
82
            case static::TYPE_SELECT:
83
                $builder = new SelectField($properties, $name, $value);
0 ignored issues
show
Bug introduced by
It seems like $value can also be of type array; however, parameter $value of Ffcms\Core\Helper\HTML\F...ectField::__construct() does only seem to accept null|string, maybe add an additional type check? ( Ignorable by Annotation )

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

83
                $builder = new SelectField($properties, $name, /** @scrutinizer ignore-type */ $value);
Loading history...
Bug introduced by
It seems like $name can also be of type array; however, parameter $name of Ffcms\Core\Helper\HTML\F...ectField::__construct() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

83
                $builder = new SelectField($properties, /** @scrutinizer ignore-type */ $name, $value);
Loading history...
84
                break;
85
            case static::TYPE_TEXTAREA:
86
                $builder = new TextareaField($properties, $name, $value);
0 ignored issues
show
Bug introduced by
It seems like $name can also be of type array; however, parameter $name of Ffcms\Core\Helper\HTML\F...reaField::__construct() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

86
                $builder = new TextareaField($properties, /** @scrutinizer ignore-type */ $name, $value);
Loading history...
Bug introduced by
It seems like $value can also be of type array; however, parameter $value of Ffcms\Core\Helper\HTML\F...reaField::__construct() does only seem to accept null|string, maybe add an additional type check? ( Ignorable by Annotation )

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

86
                $builder = new TextareaField($properties, $name, /** @scrutinizer ignore-type */ $value);
Loading history...
87
                break;
88
            case static::TYPE_MULTI_CHECKBOXES:
89
                $builder = new MultiCheckboxField($properties, $name, $value);
0 ignored issues
show
Bug introduced by
It seems like $name can also be of type array; however, parameter $name of Ffcms\Core\Helper\HTML\F...boxField::__construct() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

89
                $builder = new MultiCheckboxField($properties, /** @scrutinizer ignore-type */ $name, $value);
Loading history...
Bug introduced by
It seems like $value can also be of type array; however, parameter $value of Ffcms\Core\Helper\HTML\F...boxField::__construct() does only seem to accept null|string, maybe add an additional type check? ( Ignorable by Annotation )

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

89
                $builder = new MultiCheckboxField($properties, $name, /** @scrutinizer ignore-type */ $value);
Loading history...
90
                break;
91
            case static::TYPE_CAPTCHA:
92
                $builder = new CaptchaField($properties, $name);
0 ignored issues
show
Bug introduced by
It seems like $name can also be of type array; however, parameter $name of Ffcms\Core\Helper\HTML\F...chaField::__construct() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

92
                $builder = new CaptchaField($properties, /** @scrutinizer ignore-type */ $name);
Loading history...
93
                break;
94
            case static::TYPE_FILE:
95
                $builder = new FileField($properties, $name);
0 ignored issues
show
Bug introduced by
It seems like $name can also be of type array; however, parameter $name of Ffcms\Core\Helper\HTML\F...ileField::__construct() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

95
                $builder = new FileField($properties, /** @scrutinizer ignore-type */ $name);
Loading history...
96
                break;
97
            case static::TYPE_HIDDEN:
98
                $builder = new HiddenField($properties, $name, $value);
0 ignored issues
show
Bug introduced by
It seems like $value can also be of type array; however, parameter $value of Ffcms\Core\Helper\HTML\F...denField::__construct() does only seem to accept null|string, maybe add an additional type check? ( Ignorable by Annotation )

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

98
                $builder = new HiddenField($properties, $name, /** @scrutinizer ignore-type */ $value);
Loading history...
Bug introduced by
It seems like $name can also be of type array; however, parameter $name of Ffcms\Core\Helper\HTML\F...denField::__construct() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

98
                $builder = new HiddenField($properties, /** @scrutinizer ignore-type */ $name, $value);
Loading history...
99
                break;
100
            case static::TYPE_DIV_FAKE:
101
                $builder = new DivFakeField($properties, $name, $value);
0 ignored issues
show
Bug introduced by
It seems like $value can also be of type array; however, parameter $value of Ffcms\Core\Helper\HTML\F...akeField::__construct() does only seem to accept null|string, maybe add an additional type check? ( Ignorable by Annotation )

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

101
                $builder = new DivFakeField($properties, $name, /** @scrutinizer ignore-type */ $value);
Loading history...
Bug introduced by
It seems like $name can also be of type array; however, parameter $name of Ffcms\Core\Helper\HTML\F...akeField::__construct() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

101
                $builder = new DivFakeField($properties, /** @scrutinizer ignore-type */ $name, $value);
Loading history...
102
                break;
103
            case static::TYPE_MULTISELECT:
104
                $builder = new MultiSelectField($properties, $name, $value);
0 ignored issues
show
Bug introduced by
It seems like $name can also be of type array; however, parameter $name of Ffcms\Core\Helper\HTML\F...ectField::__construct() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

104
                $builder = new MultiSelectField($properties, /** @scrutinizer ignore-type */ $name, $value);
Loading history...
Bug introduced by
It seems like $value can also be of type array; however, parameter $value of Ffcms\Core\Helper\HTML\F...ectField::__construct() does only seem to accept null|string, maybe add an additional type check? ( Ignorable by Annotation )

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

104
                $builder = new MultiSelectField($properties, $name, /** @scrutinizer ignore-type */ $value);
Loading history...
105
                break;
106
            case static::TYPE_RADIO:
107
                $builder = new RadioField($properties, $name, $value);
0 ignored issues
show
Bug introduced by
It seems like $name can also be of type array; however, parameter $name of Ffcms\Core\Helper\HTML\F...dioField::__construct() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

107
                $builder = new RadioField($properties, /** @scrutinizer ignore-type */ $name, $value);
Loading history...
Bug introduced by
It seems like $value can also be of type array; however, parameter $value of Ffcms\Core\Helper\HTML\F...dioField::__construct() does only seem to accept null|string, maybe add an additional type check? ( Ignorable by Annotation )

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

107
                $builder = new RadioField($properties, $name, /** @scrutinizer ignore-type */ $value);
Loading history...
108
                break;
109
            default:
110
                if (App::$Debug) {
111
                    App::$Debug->addMessage('Field has unknown type: ' . App::$Security->strip_tags($name));
0 ignored issues
show
Bug introduced by
Are you sure Ffcms\Core\App::Security->strip_tags($name) of type string|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

111
                    App::$Debug->addMessage('Field has unknown type: ' . /** @scrutinizer ignore-type */ App::$Security->strip_tags($name));
Loading history...
112
                }
113
        }
114
115
        return $builder->make();
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $builder does not seem to be defined for all execution paths leading up to this point.
Loading history...
116
    }
117
    
118
    /**
119
     * Set validator options to current properties
120
     * @param string $name
121
     * @param array|null $properties
122
     * @return void
123
     */
124
    private function addValidationProperties(string $name, ?array &$properties = null): void
125
    {
126
        // jquery validation quick-build some rules
127
        $rules = $this->model->getValidationRule($name);
128
        if (count($rules) > 0) {
129
            foreach ($rules as $type => $value) {
130
                switch ($type) {
131
                    case 'required':
132
                        $properties['required'] = null;
133
                        break;
134
                    case 'length_min':
135
                        $properties['minlength'] = $value;
136
                        break;
137
                    case 'length_max':
138
                        $properties['maxlength'] = $value;
139
                        break;
140
                }
141
            }
142
        }
143
    }
144
    
145
    /**
146
     * Prepare field global properties - name, id and value
147
     * @param string $name
148
     * @param string|null $value
149
     * @param array|null $properties
150
     */
151
    private function setDefaultProperties(string $name, $value = null, array &$properties = null): void
152
    {
153
        // standard property data definition
154
        $properties['name'] = $properties['id'] = $this->formName; // form global name
155
        if ($value !== null && !Any::isEmpty($value)) {
156
            $properties['value'] = $value;
157
        }
158
        
159
        // sounds like a array-path based obj name
160
        if (Str::contains('.', $name)) {
161
            $splitedName = explode('.', $name);
162
            foreach ($splitedName as $nameKey) {
163
                $properties['name'] .= '[' . $nameKey . ']';
164
                $properties['id'] .= '-' . $nameKey;
165
            }
166
        } else { // standard property definition - add field name
167
            $properties['name'] .= '[' . $name . ']';
168
            $properties['id'] .= '-' . $name;
169
        }
170
    }
171
}
172