Skip to content

Instantly share code, notes, and snippets.

@NemanyaM
Last active April 25, 2016 12:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NemanyaM/ff5de140c757f98ccf57247edd1bd7fb to your computer and use it in GitHub Desktop.
Save NemanyaM/ff5de140c757f98ccf57247edd1bd7fb to your computer and use it in GitHub Desktop.
<?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);
}
}
<?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