Fighting large ints in 32-bit PHP

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
{
    /**
     * Test that the given data passes a Luhn check. 
     * 
     * @return bool True if the data passes the Luhn check
     * @throws \InvalidArgumentException 
     * @see http://en.wikipedia.org/wiki/Luhn_algorithm
     */
    public function isValid ()
    {
        $data   = $this -> getData ();
        $valid  = false;

        switch (gettype ($data))
        {
            case 'NULL'     :
                $valid  = true;
            break;
            case 'integer'  :
                // Get the sequence of digits that make up the number under test
                $digits = array_reverse (array_map ('intval', str_split ((string) $data)));
                // Walk the array, doubling the value of every second digit
                for ($i = 0, $count = count ($digits); $i < $count; $i++)
                {
                    if ($i % 2)
                    {
                        // Double the digit
                        if (($digits [$i] *= 2) > 9)
                        {
                            // Handle the case where the doubled digit is over 9
                            $digits [$i]    -= 10;
                            $digits []      = 1;
                        }
                    }
                }
                // The Luhn is valid if the sum of the digits ends in a 0
                $valid  = ((array_sum ($digits) % 10) === 0);
            break;
            default         :
                // An attempt was made to apply the check to an invalid data type
                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. , .

+3
2
+3

, float.

, ( ), bc*: BCMath

+7

All Articles