Last active
April 25, 2016 12:58
-
-
Save NemanyaM/ff5de140c757f98ccf57247edd1bd7fb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class ExpressionTest extends PHPUnit_Framework_TestCase | |
{ | |
/** @test */ | |
public function it_generates_Number() | |
{ | |
$number = Luhn::make(); | |
$this->generate('Luhn', $number); | |
$number = Luhn::make()->find($number); | |
$this->assertTrue($number); | |
$number_length = Luhn::make(); | |
$this->generate('Luhn', $number_length); | |
$number_length = Luhn::make()->compare(); | |
$this->assertEquals($number_length, $number); | |
$eachSecond = Luhn::make(); | |
$this->generate('Luhn', $eachSecond); | |
$number = Luhn::make()->find(); | |
$this->assertTrue($eachSecond); | |
$total = Luhn::make(); | |
$this->generate('Luhn', $total) | |
->find($chislo) | |
->then($total); | |
$this->assertTrue($total); | |
$this->assertTrue($total); | |
$this->assertFalse($total); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Luhn | |
{ | |
public static function make() | |
{ | |
return new static; | |
} | |
public function luhn_check($number) | |
{ | |
$number = preg_replace('/\D/', '', $number); | |
$number_length = strlen($number); | |
$eachSecond = $number_length % 2; | |
$total = 0; | |
for ($i = 0; $i < $number_length; $i++) { | |
$chislo = $number[$i]; | |
if ($i % 2 == $eachSecond) { | |
$chislo *= 2; | |
if ($chislo > 9) { | |
$chislo -= 9; | |
} | |
} | |
$total += $chislo; | |
} | |
return ($total % 10 == 0) ? true : false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment