Passed
Push — 1.0 ( c519e2...8893aa )
by Morven
09:42 queued 03:00
created

QuantityField::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 1
c 1
b 0
f 1
nc 1
nop 5
dl 0
loc 3
rs 10
1
<?php
2
3
namespace SilverCommerce\QuantityField\Forms;
4
5
use SilverStripe\Forms\NumericField;
6
7
/**
8
 * Text input field with validation for numeric values.
9
 * 
10
 * @package quantityfield
11
 */
12
class QuantityField extends NumericField
13
{
14
    /**
15
     * Construct this field (we override the default to set a default value)
16
     *
17
     * @param [type] $name
18
     * @param [type] $title
19
     * @param string $value
20
     * @param [type] $maxLength
21
     * @param [type] $form
22
     */
0 ignored issues
show
Documentation Bug introduced by
The doc comment [type] at position 0 could not be parsed: Unknown type name '[' at position 0 in [type].
Loading history...
23
    public function __construct($name, $title = null, $value = 1, $maxLength = null, $form = null)
24
    {
25
        parent::__construct($name, $title, $value);
26
    }
27
28
    public function Type()
29
    {
30
        return 'quantity numeric text';
31
    }
32
33
    /**
34
     * PHP Validation
35
     * 
36
     * @return boolean
37
     * @throws Exception
38
    **/
39
    public function validate($validator)
40
    {
41
        $value = $this->Value() + 0;
42
        
43
        if(is_int($value)) {
0 ignored issues
show
introduced by
The condition is_int($value) can never be false.
Loading history...
44
            return true;
45
        }
46
        
47
        $validator->validationError(
48
            $this->name,
49
            _t(
50
                'Checkout.VALIDATION', '{value} is not a valid number, only whole numbers can be accepted for this field',
51
                array('value' => $value)
52
            ),
53
            "validation"
54
        );
55
56
        return false;
57
    }
58
    
59
    public function dataValue()
60
    {
61
        $value = $this->Value();
62
        $value =  (is_numeric($value)) ? $value : 0;
63
64
        return $this->cast($value);
65
    }
66
}
67