Completed
Push — master ( 9394c7...856cc5 )
by Mihail
06:05
created

Constructor::globalProperties()   B

Complexity

Conditions 5
Paths 6

Size

Total Lines 20
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 20
rs 8.8571
cc 5
eloc 12
nc 6
nop 3
1
<?php
2
3
namespace Ffcms\Core\Helper\HTML\Form;
4
5
use Ffcms\Core\App;
6
use Ffcms\Core\Helper\Type\Obj;
7
use Ffcms\Core\Exception\SyntaxException;
8
use Ffcms\Core\Helper\Type\Str;
9
10
class Constructor
11
{
12
    const TYPE_TEXT = 'text';
13
    const TYPE_PASSWORD = 'password';
14
    const TYPE_EMAIL = 'email';
15
    const TYPE_CHECKBOX = 'checkbox';
16
    const TYPE_SELECT = 'select';
17
    const TYPE_TEXTAREA = 'textarea';
18
    const TYPE_MULTI_CHECKBOXES = 'checkboxes';
19
    const TYPE_CAPTCHA = 'captcha';
20
    const TYPE_FILE = 'file';
21
    const TYPE_HIDDEN = 'hidden';
22
    const TYPE_DIV_FAKE = 'div';
23
    
24
    /** @var Ffcms\Core\Arch\Model $model */
25
    private $model;
26
    private $formName;
27
    private $type;
28
    
29
    /**
30
     * Initialize Constructor. Pass model and type inside of current field inside.
31
     * @param Ffcms\Core\Arch\Model $model
32
     * @param string $type
33
     */
34
    public function __construct($model, $formName = false, $type = 'text')
35
    {
36
        $this->model = $model;
37
        $this->formName = $formName;
38
        $this->type = $type;
39
    }
40
    
41
    
42
    public function makeTag($name, $value = null, $properties = null)
43
    {
44
        // check if properties is passed well
45
        if (!Obj::isArray($properties) && $properties !== null) {
46
            throw new SyntaxException('Property must be passed as array or null! Field: ' . $name);
47
        }
48
        
49
        // add properties to autovalidation by js (properties passed by ref)
50
        $this->validatorProperties($name, $properties);
51
        // prepare properties name and id to autobuild
52
        $this->globalProperties($name, $value, $properties);
53
        
54
        // initialize build model depend of current type
55
        switch ($this->type) {
56
            case static::TYPE_TEXT: // for <input type="text">
57
                $builder = new TextField($properties, $name);
58
                return $builder->make();
59
            case static::TYPE_CHECKBOX:
60
                $builder = new CheckboxField($properties, $name, $value);
61
                return $builder->make();
62
            case static::TYPE_PASSWORD:
63
                $builder = new PasswordField($properties, $name);
64
                return $builder->make();
65
            case static::TYPE_EMAIL:
66
                $builder = new EmailField($properties, $name);
67
                return $builder->make();
68
            case static::TYPE_SELECT:
69
                $builder = new SelectField($properties, $name, $value);
70
                return $builder->make();
71
            case static::TYPE_TEXTAREA:
72
                $builder = new TextareaField($properties, $name, $value);
73
                return $builder->make();
74
            case static::TYPE_MULTI_CHECKBOXES:
75
                $builder = new MultiCheckboxField($properties, $name, $value);
76
                return $builder->make();
77
            case static::TYPE_CAPTCHA:
78
                $builder = new CaptchaField($properties, $name);
79
                return $builder->make();
80
            case static::TYPE_FILE:
81
                $builder = new FileField($properties, $name);
82
                return $builder->make();
83
            case static::TYPE_HIDDEN:
84
                $builder = new HiddenField($properties, $name, $value);
85
                return $builder->make();
86
            case static::TYPE_DIV_FAKE:
87
                $builder = new DivFakeField($properties, $name, $value);
88
                return $builder->make();
89
        }
90
        
91
        // if field is unknown type add notification in debugbar
92
        if (App::$Debug !== null) {
93
            App::$Debug->addMessage('Field with name [' . App::$Security->strip_tags($name) . '] have unknown type [' . $this->type . ']', 'error');
94
        }
95
        return 'No data: ' . App::$Security->strip_tags($name);
96
    }
97
    
98
    /**
99
     * Set validator options to current properties
100
     * @param string $name
101
     * @param array $properties
102
     */
103
    private function validatorProperties($name, &$properties)
104
    {
105
        // jquery validation quick-build some rules
106
        $rules = $this->model->getValidationRule($name);
107
        if (count($rules) > 0) {
108
            foreach ($rules as $rule_name => $rule_value) {
109
                switch ($rule_name) {
110
                    case 'required':
111
                        $properties['required'] = null;
112
                        break;
113
                    case 'length_min':
114
                        $properties['minlength'] = $rule_value;
115
                        break;
116
                    case 'length_max':
117
                        $properties['maxlength'] = $rule_value;
118
                        break;
119
                }
120
            }
121
        }
122
    }
123
    
124
    /**
125
     * Prepare field global properties - name, id and value
126
     * @param string $name
127
     * @param string|null $value
128
     * @param array $properties
129
     */
130
    private function globalProperties($name, $value = null, &$properties)
131
    {
132
        // standard property data definition
133
        $properties['name'] = $properties['id'] = $this->formName; // form global name
134
        if ($value !== null && !Str::likeEmpty($value)) {
135
            $properties['value'] = $value;
136
        }
137
        
138
        // sounds like a array-path based obj name
139
        if (Str::contains('.', $name)) {
140
            $splitedName = explode('.', $name);
141
            foreach ($splitedName as $nameKey) {
142
                $properties['name'] .= '[' . $nameKey . ']';
143
                $properties['id'] .= '-' . $nameKey;
144
            }
145
        } else { // standard property definition - add field name
146
            $properties['name'] .= '[' . $name . ']';
147
            $properties['id'] .= '-' . $name;
148
        }
149
    }
150
}