Forms   A
last analyzed

Complexity

Total Complexity 20

Size/Duplication

Total Lines 151
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 151
ccs 0
cts 73
cp 0
rs 10
c 0
b 0
f 0
wmc 20
lcom 2
cbo 2

11 Methods

Rating   Name   Duplication   Size   Complexity  
A setConfig() 0 4 1
A setFormValidator() 0 12 3
A getFormValidator() 0 8 2
A addFormValidatorAdapter() 0 8 2
A getFieldTypes() 0 4 1
A __callStatic() 0 10 3
A field() 0 13 2
A getValue() 0 8 2
A open() 0 9 2
A close() 0 6 1
A submit() 0 4 1
1
<?php namespace Rocket\UI\Forms;
2
3
use Collective\Html\FormFacade as Form;
4
use Illuminate\Support\Facades\Log;
5
use Rocket\UI\Forms\ValidatorAdapters\ValidatorInterface;
6
7
/**
8
 * Class Forms
9
 * @method static Fields\Date date($id, $title = "")
10
 * @method static Fields\Time time($id, $title = "")
11
 * @method static Fields\Datetime datetime($id, $title = "")
12
 * @method static Fields\Textarea textarea($id, $title = "")
13
 * @method static Fields\Htmlarea htmlarea($id, $title = "")
14
 * @method static Fields\Field text($id, $title = "")
15
 * @method static Fields\Password password($id, $title = "")
16
 * @method static Fields\Radio radio($id, $title = "")
17
 * @method static Fields\Email email($id, $title = "")
18
 * @method static Fields\Autocomplete autocomplete($id, $title = "")
19
 * @method static Fields\Select select($id, $title = "")
20
 * @method static Fields\Checkbox checkbox($id, $title = "")
21
 * @method static Fields\Hidden hidden($id, $title = "")
22
 * @method static Fields\Honeypot honeypot($id, $title = "")
23
 * @method static Fields\File file($id, $title = "")
24
 * @method static Fields\Kaptcha kaptcha($id, $title = "")
25
 */
26
class Forms
27
{
28
    /**
29
     * Hold the configuration array
30
     *
31
     * @var array
32
     */
33
    public static $config;
34
35
    public static $currentValidator;
36
    public static $currentData;
37
38
    public static $adapters = [
39
        \Rocket\UI\Forms\ValidatorAdapters\LaravelValidator::class,
40
        \Rocket\UI\Forms\ValidatorAdapters\CodeIgniterFormValidator::class,
41
    ];
42
43
    /**
44
     * Set the current configuration
45
     *
46
     * @param $config
47
     */
48
    public static function setConfig($config)
49
    {
50
        self::$config = $config;
51
    }
52
53
    /**
54
     * @param object $validator
55
     * @param mixed $data
56
     * @param mixed $defaults
57
     * @return bool
58
     */
59
    public static function setFormValidator($validator, $data = [], $defaults = [])
60
    {
61
        foreach (self::$adapters as $class) {
62
            if ($class::supports($validator)) {
63
                self::$currentValidator = new $class($validator, $data, $defaults);
64
65
                return true;
66
            }
67
        }
68
69
        throw new \RuntimeException('impossible to find a form adapter for ' . get_class($validator));
70
    }
71
72
    /**
73
     * @return ValidatorInterface
74
     */
75
    public static function getFormValidator()
76
    {
77
        if (!self::$currentValidator) {
78
            Log::debug('You have no form validator defined');
79
        }
80
81
        return self::$currentValidator;
82
    }
83
84
    /**
85
     * @param string $adapter
86
     */
87
    public static function addFormValidatorAdapter($adapter)
88
    {
89
        if (!is_subclass_of($adapter, ValidatorInterface::class)) {
0 ignored issues
show
Bug introduced by
Due to PHP Bug #53727, is_subclass_of might return inconsistent results on some PHP versions if \Rocket\UI\Forms\Validat...lidatorInterface::class can be an interface. If so, you could instead use ReflectionClass::implementsInterface.
Loading history...
90
            throw new \RuntimeException("$adapter doesn't implement ValidatorInterface");
91
        }
92
93
        self::$adapters[] = $adapter;
94
    }
95
96
    /**
97
     * Get the list of field types
98
     *
99
     * @return mixed
100
     */
101
    public static function getFieldTypes()
102
    {
103
        return self::$config['field_types'];
104
    }
105
106
    /**
107
     * Shortcut to create a field
108
     *
109
     * @param $type
110
     * @param $arguments
111
     * @return Fields\Field
112
     */
113
    public static function __callStatic($type, $arguments)
114
    {
115
        $types = self::getFieldTypes();
116
117
        if (!array_key_exists($type, $types)) {
118
            throw new \RuntimeException("Cannot create field of type $type");
119
        }
120
121
        return self::field($arguments[0], array_key_exists(1, $arguments) ? $arguments[1] : '', $type);
122
    }
123
124
    /**
125
     * Create a field
126
     *
127
     * @param string $id
128
     * @param string $title
129
     * @param string $type
130
     * @return \Rocket\UI\Forms\Fields\Field
131
     */
132
    public static function field($id, $title = '', $type = 'text')
133
    {
134
        $types = self::getFieldTypes();
135
136
        $data = ['title' => $title];
137
138
        //Generates the class if we find the right renderer
139
        if (!array_key_exists($type, $types)) {
140
            $type = 'text';
141
        }
142
143
        return new $types[$type]($id, $data);
144
    }
145
146
    public static function getValue($field, $default = '')
147
    {
148
        $validator = static::getFormValidator();
149
150
        if ($validator) {
151
            return $validator->getValue($field, $default);
152
        }
153
    }
154
155
    public static function open(array $options = [])
156
    {
157
        if (array_key_exists('validator', $options)) {
158
            static::setFormValidator($options['validator']);
159
            unset($options['validator']);
160
        }
161
162
        return Form::open($options);
163
    }
164
165
    public static function close()
166
    {
167
        static::$currentValidator = null;
168
169
        return Form::close();
170
    }
171
172
    public static function submit($value = null, $options = [])
173
    {
174
        return Form::submit($value, $options);
175
    }
176
}
177