Completed
Push — master ( a07461...abf834 )
by Rasmus
03:12
created

TextField::setPattern()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 2
crap 1
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 View Code Duplication
    public function renderInput(InputRenderer $renderer, InputModel $model, array $attr)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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 7
    public function createValidators()
72
    {
73 7
        $validators = parent::createValidators();
74
75 7
        if ($this->min_length !== null) {
76 6
            if ($this->max_length !== null) {
77 6
                $validators[] = new CheckLength($this->min_length, $this->max_length);
78
            } else {
79 6
                $validators[] = new CheckMinLength($this->min_length);
80
            }
81 7
        } else if ($this->max_length !== null) {
82 6
            $validators[] = new CheckMaxLength($this->max_length);
83
        }
84
85 7
        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 7
        return $validators;
90
    }
91
}
92