Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
29 / 29 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
1 / 1 |
| CreditCard | |
100.00% |
29 / 29 |
|
100.00% |
2 / 2 |
11 | |
100.00% |
1 / 1 |
| evaluate | |
100.00% |
26 / 26 |
|
100.00% |
1 / 1 |
9 | |||
| generateDefaultMessage | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
| 1 | <?php |
| 2 | declare(strict_types=1); |
| 3 | /** |
| 4 | * Pop PHP Framework (https://www.popphp.org/) |
| 5 | * |
| 6 | * @link https://github.com/popphp/popphp-framework |
| 7 | * @author Nick Sagona, III <nick@popphp.org> |
| 8 | * @copyright Copyright (c) 2009-2026 Nick Sagona, III |
| 9 | * @license https://www.popphp.org/license New BSD License |
| 10 | */ |
| 11 | |
| 12 | /** |
| 13 | * @namespace |
| 14 | */ |
| 15 | namespace Pop\Validator; |
| 16 | |
| 17 | /** |
| 18 | * Credit card validator class |
| 19 | * |
| 20 | * @category Pop |
| 21 | * @package Pop\Validator |
| 22 | * @author Nick Sagona, III <nick@popphp.org> |
| 23 | * @copyright Copyright (c) 2009-2026 Nick Sagona, III |
| 24 | * @license https://www.popphp.org/license New BSD License |
| 25 | * @version 5.0.0 |
| 26 | */ |
| 27 | class CreditCard extends AbstractValidator |
| 28 | { |
| 29 | |
| 30 | /** |
| 31 | * Method to evaluate the validator |
| 32 | * |
| 33 | * @param mixed $input |
| 34 | * @return bool |
| 35 | */ |
| 36 | public function evaluate(mixed $input = null): bool |
| 37 | { |
| 38 | // Set the input, if passed |
| 39 | if ($input !== null) { |
| 40 | $this->input = $input; |
| 41 | } |
| 42 | |
| 43 | // Set the default message |
| 44 | if (!$this->hasMessage()) { |
| 45 | $this->generateDefaultMessage(); |
| 46 | } |
| 47 | |
| 48 | $inputValue = ($this->hasKeyField()) ? $this->getKeyFieldValue() : $this->input; |
| 49 | |
| 50 | if (is_string($inputValue)) { |
| 51 | $inputValue = str_replace([' ', '-'], '', $inputValue); |
| 52 | } |
| 53 | |
| 54 | if (!ctype_digit((string)$inputValue)) { |
| 55 | return false; |
| 56 | } |
| 57 | |
| 58 | // Evaluate the input against the validator |
| 59 | $nums = str_split((string)$inputValue); |
| 60 | $check = $nums[count($nums) - 1]; |
| 61 | $start = count($nums) - 2; |
| 62 | $sum = 0; |
| 63 | $double = true; |
| 64 | |
| 65 | for ($i = $start; $i >= 0; $i--) { |
| 66 | if ($double) { |
| 67 | $num = (int)$nums[$i] * 2; |
| 68 | if ($num > 9) { |
| 69 | $num = (int)substr((string)$num, 0, 1) + (int)substr((string)$num, 1, 1); |
| 70 | } |
| 71 | $sum += $num; |
| 72 | $double = false; |
| 73 | } else { |
| 74 | $sum += $nums[$i]; |
| 75 | $double = true; |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | $sum += $check; |
| 80 | $rem = $sum % 10; |
| 81 | |
| 82 | return ($rem == 0); |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Generate default message |
| 87 | |
| 88 | * @param mixed $name |
| 89 | * @param mixed $value |
| 90 | * @return string |
| 91 | */ |
| 92 | public function generateDefaultMessage(mixed $name = null, mixed $value = null): string |
| 93 | { |
| 94 | $this->message = "The " . (($name !== null) ? "'" . $name . "'" : "value") . |
| 95 | " must be a valid credit card number."; |
| 96 | |
| 97 | return $this->message; |
| 98 | } |
| 99 | |
| 100 | } |