|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Allows input of credit card numbers via four separate form fields, |
|
4
|
|
|
* including generic validation of its numeric values. |
|
5
|
|
|
* |
|
6
|
|
|
* @todo Validate |
|
7
|
|
|
* |
|
8
|
|
|
* @package forms |
|
9
|
|
|
* @subpackage fields-formattedinput |
|
10
|
|
|
*/ |
|
11
|
|
|
class CreditCardField extends TextField { |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Add default attributes for use on all inputs. |
|
15
|
|
|
* |
|
16
|
|
|
* @return array List of attributes |
|
17
|
|
|
*/ |
|
18
|
|
|
public function getAttributes() { |
|
19
|
|
|
return array_merge( |
|
20
|
|
|
parent::getAttributes(), |
|
21
|
|
|
array( |
|
22
|
|
|
'autocomplete' => 'off', |
|
23
|
|
|
'maxlength' => 4, |
|
24
|
|
|
'size' => 4 |
|
25
|
|
|
) |
|
26
|
|
|
); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
public function Field($properties = array()) { |
|
30
|
|
|
$parts = $this->arrayValue(); |
|
31
|
|
|
|
|
32
|
|
|
$properties['ValueOne'] = $parts[0]; |
|
33
|
|
|
$properties['ValueTwo'] = $parts[1]; |
|
34
|
|
|
$properties['ValueThree'] = $parts[2]; |
|
35
|
|
|
$properties['ValueFour'] = $parts[3]; |
|
36
|
|
|
|
|
37
|
|
|
return parent::Field($properties); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Get tabindex HTML string |
|
42
|
|
|
* |
|
43
|
|
|
* @param int $increment Increase current tabindex by this value |
|
44
|
|
|
* @return string |
|
45
|
|
|
*/ |
|
46
|
|
|
public function getTabIndexHTML($increment = 0) { |
|
47
|
|
|
// we can't add a tabindex if there hasn't been one set yet. |
|
48
|
|
|
if($this->getAttribute('tabindex') === null) return false; |
|
49
|
|
|
|
|
50
|
|
|
$tabIndex = (int)$this->getAttribute('tabindex') + (int)$increment; |
|
51
|
|
|
return (is_numeric($tabIndex)) ? ' tabindex = "' . $tabIndex . '"' : ''; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
public function dataValue() { |
|
55
|
|
|
if(is_array($this->value)) { |
|
56
|
|
|
return implode("", $this->value); |
|
57
|
|
|
} else { |
|
58
|
|
|
return $this->value; |
|
59
|
|
|
} |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Get either list of values, or null |
|
64
|
|
|
* |
|
65
|
|
|
* @return array |
|
66
|
|
|
*/ |
|
67
|
|
|
public function arrayValue() { |
|
68
|
|
|
if (is_array($this->value)) { |
|
69
|
|
|
return $this->value; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
$value = $this->dataValue(); |
|
73
|
|
|
return $this->parseCreditCard($value); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Parse credit card value into list of four four-digit values |
|
78
|
|
|
* |
|
79
|
|
|
* @param string $value |
|
80
|
|
|
* @return array|null |
|
81
|
|
|
*/ |
|
82
|
|
|
protected function parseCreditCard($value) { |
|
83
|
|
|
if(preg_match("/([0-9]{4})([0-9]{4})([0-9]{4})([0-9]{4})/", $value, $parts)) { |
|
84
|
|
|
return [ $parts[1], $parts[2], $parts[3], $parts[4] ]; |
|
85
|
|
|
} |
|
86
|
|
|
return null; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
public function validate($validator){ |
|
90
|
|
|
$value = $this->dataValue(); |
|
91
|
|
|
if(empty($value)) { |
|
92
|
|
|
return true; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
// Check if format is valid |
|
96
|
|
|
if ($this->parseCreditCard($value)) { |
|
97
|
|
|
return true; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
// Format is invalid |
|
101
|
|
|
$validator->validationError( |
|
102
|
|
|
$this->name, |
|
103
|
|
|
_t( |
|
104
|
|
|
'Form.VALIDATIONCREDIT', |
|
105
|
|
|
"Please ensure you have entered the credit card number correctly" |
|
106
|
|
|
), |
|
107
|
|
|
"validation" |
|
108
|
|
|
); |
|
109
|
|
|
return false; |
|
110
|
|
|
} |
|
111
|
|
|
} |
|
112
|
|
|
|