I have a class to calculate the moon checksum for a number. It takes an integer as input and returns true or false to indicate reality or otherwise, or throws an exception if an invalid data type is specified as input.
The code is as follows (Full source is on GitHub ):
class Luhn extends abstr\Prop implements iface\Prop
{
public function isValid ()
{
$data = $this -> getData ();
$valid = false;
switch (gettype ($data))
{
case 'NULL' :
$valid = true;
break;
case 'integer' :
$digits = array_reverse (array_map ('intval', str_split ((string) $data)));
for ($i = 0, $count = count ($digits); $i < $count; $i++)
{
if ($i % 2)
{
if (($digits [$i] *= 2) > 9)
{
$digits [$i] -= 10;
$digits [] = 1;
}
}
}
$valid = ((array_sum ($digits) % 10) === 0);
break;
default :
throw new \InvalidArgumentException (__CLASS__ . ': This property cannot be applied to data of type ' . gettype ($data));
break;
}
return ($valid);
}
}
I also created a complete unit test to implement the class.
- 64- PHP 5.3 Apache OSX Lion. 64- Apache PHP 5.4 Apache. , Ubuntu Linux 64- Apache PHP 5.3. unit test , .
, - (Windows 7, XAMPP, 32- PHP 5.3) , , , , unit test.
, 32- PHP float, 32- . - float. float, , int (PHP_INT_MIN.. PHP_INT_MAX), number_format(), . , .
. , 0 , ( ). 0 , , ? ( , , , , 1000 , int . 1000, , 1001, , 1001.9, 1002, , ).
, ?
. , , , , - , Luhn -checkable data - , . PHP, , , , , , . , , , PHP int, float. , .