Skip to content

Commit

Permalink
no message
Browse files Browse the repository at this point in the history
  • Loading branch information
Your Name committed Jun 4, 2020
1 parent 1667fe0 commit 3bf9dff
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 34 deletions.
39 changes: 16 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,29 +1,22 @@
# README #
# Valid and Dedect bank cards #

This README would normally document whatever steps are necessary to get your application up and running.
Utility to valid and determine bank card type by stgit90

### What is this repository for? ###
### Installation ###
```
composer require
```

* Quick summary
* Version
* [Learn Markdown](https://bitbucket.org/tutorials/markdowndemo)

### How do I get set up? ###
### Usage ###

* Summary of set up
* Configuration
* Dependencies
* Database configuration
* How to run tests
* Deployment instructions
```
require "/vendor/autoload.php";
use CardValidDetect\ValidDetect;
### Contribution guidelines ###

* Writing tests
* Code review
* Other guidelines

### Who do I talk to? ###

* Repo owner or admin
* Other community or team contact
$detector = new ValidDetect();
$card = '4916847576752405';
if($detector->detect('4916847576752405')->isValid()){
echo $detector->getType() //Visa
}
```
44 changes: 34 additions & 10 deletions src/ValidDetect.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,24 @@

namespace CardValidDetect;
use \Exception;
use phpDocumentor\Reflection\Types\This;

/**
* Class ValidDetect
* @package CardValidDetect
*/
class ValidDetect
{
/**
* @var bool $is_valid
*/
private bool $is_valid;

/**
* @var string $types
*/
private string $type;

/**
* @var array
*/
Expand Down Expand Up @@ -69,23 +81,35 @@ private function valid(string $card_number) : bool
}
$total+=$digit;
}
return ($total % 10 == 0) ? TRUE : FALSE;
return $this->is_valid = ($total % 10 == 0) ? TRUE : FALSE;
}

public function isValid(): bool
{
return $this->is_valid;
}

/**
* @param string $card_number
* @return string
* @throws Exception
*/
public function detect(string $card_number) : string
public function getType(): string
{
if(!$this->valid($card_number)){
throw new Exception('Card number is invalid');
}
foreach ($this->card_types as $type=>$pattern) {
if ($this->$type($card_number)) {
return $type;
return $this->type;
}
/**
* @param string $card_number
* @return $this|string
*/
public function detect(string $card_number) : ValidDetect
{
if($this->valid($card_number)){
foreach ($this->card_types as $type=>$pattern) {
if ($this->$type($card_number)) {
$this->type = $type;
break;
}
}
}
return $this;
}
}
2 changes: 1 addition & 1 deletion tests/unit/ValidDetectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,6 @@ public function additionProvider()
*/
public function testPushAndPop($expected, $card)
{
$this->assertSame($expected, $this->ValidDetect->detect($card));
$this->assertSame($expected, $this->ValidDetect->detect($card)->getType());
}
}

0 comments on commit 3bf9dff

Please sign in to comment.