TextField::renderInput()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 15
c 0
b 0
f 0
ccs 8
cts 8
cp 1
rs 9.4285
cc 3
eloc 8
nc 4
nop 3
crap 3
1
<?php
2
3
namespace mindplay\kissform\Fields;
4
5
use mindplay\kissform\Field;
6
use mindplay\kissform\InputModel;
7
use mindplay\kissform\InputRenderer;
8
use mindplay\kissform\Validators\CheckLength;
9
use mindplay\kissform\Validators\CheckMaxLength;
10
use mindplay\kissform\Validators\CheckMinLength;
11
use mindplay\kissform\Validators\CheckPattern;
12
13
/**
14
 * This class provides information about a text field, e.g. a plain
15
 * input type=text element.
16
 */
17
class TextField extends Field
18
{
19
    /**
20
     * @var int|null
21
     */
22
    public $min_length;
23
24
    /**
25
     * @var int|null
26
     */
27
    public $max_length;
28
29
    /**
30
     * @var string|null
31
     */
32
    protected $pattern;
33
34
    /**
35
     * @var string|null
36
     */
37
    protected $pattern_error;
38
39
    /**
40
     * @param string $pattern regular expression pattern (optional; no delimiters, modifiers or anchors)
41
     * @param string $error   error message to apply on pattern mismatch
42
     */
43 5
    public function setPattern($pattern, $error)
44
    {
45 5
        $this->pattern = $pattern;
46 5
        $this->pattern_error = $error;
47 5
    }
48
49
    /**
50
     * {@inheritdoc}
51
     */
52 6
    public function renderInput(InputRenderer $renderer, InputModel $model, array $attr)
53
    {
54 6
        $defaults = [];
55
56 6
        if ($this->max_length) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->max_length of type integer|null is loosely compared to true; this is ambiguous if the integer can be zero. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
57 1
            $defaults['maxlength'] = $this->max_length;
58
        }
59
60 6
        if ($this->pattern) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->pattern of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
61 1
            $defaults['pattern'] = $this->pattern;
62 1
            $defaults['data-pattern-error'] = $this->pattern_error;
63
        }
64
        
65 6
        return $renderer->inputFor($this, 'text', $attr + $defaults);
66
    }
67
68
    /**
69
     * @inheritdoc
70
     */
71 8
    public function createValidators()
72
    {
73 8
        $validators = parent::createValidators();
74
75 8
        if ($this->min_length !== null) {
76 7
            if ($this->max_length !== null) {
77 7
                $validators[] = new CheckLength($this->min_length, $this->max_length);
78
            } else {
79 7
                $validators[] = new CheckMinLength($this->min_length);
80
            }
81 8
        } else if ($this->max_length !== null) {
82 7
            $validators[] = new CheckMaxLength($this->max_length);
83
        }
84
85 8
        if ($this->pattern) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->pattern of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
86 4
            $validators[] = new CheckPattern("/^{$this->pattern}$/", $this->pattern_error);
87
        }
88
89 8
        return $validators;
90
    }
91
}
92