Skip to content

Commit 987bda4

Browse files
committed
[WIP] beta
1 parent 02b3083 commit 987bda4

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+3108
-16
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ vendor
33
.php_cs.cache
44
phpunit.xml
55
coverage.xml
6+
test-reports

.php_cs

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<?php
2+
3+
return PhpCsFixer\Config::create()
4+
->setRiskyAllowed(true)
5+
->setUsingCache(false)
6+
->setRules(array(
7+
'blank_line_after_opening_tag' => true,
8+
'braces' => true,
9+
'concat_space' => ['spacing' => 'none'],
10+
'no_multiline_whitespace_around_double_arrow' => true,
11+
'no_empty_statement' => true,
12+
'elseif' => true,
13+
'simplified_null_return' => true,
14+
'encoding' => true,
15+
'single_blank_line_at_eof' => true,
16+
'no_extra_consecutive_blank_lines' => true,
17+
'no_spaces_after_function_name' => true,
18+
'function_declaration' => true,
19+
'include' => true,
20+
'indentation_type' => true,
21+
'no_alias_functions' => true,
22+
'blank_line_after_namespace' => true,
23+
'line_ending' => true,
24+
'no_trailing_comma_in_list_call' => true,
25+
'not_operator_with_successor_space' => true,
26+
'lowercase_constants' => true,
27+
'lowercase_keywords' => true,
28+
'method_argument_space' => true,
29+
'trailing_comma_in_multiline_array' => true,
30+
'no_multiline_whitespace_before_semicolons' => true,
31+
'single_import_per_statement' => true,
32+
'no_leading_namespace_whitespace' => true,
33+
'no_blank_lines_after_class_opening' => true,
34+
'no_blank_lines_after_phpdoc' => true,
35+
'object_operator_without_whitespace' => true,
36+
'binary_operator_spaces' => true,
37+
'ordered_class_elements' => true,
38+
'ordered_imports' => ['sortAlgorithm' => 'length'],
39+
'no_spaces_inside_parenthesis' => true,
40+
'phpdoc_indent' => true,
41+
'phpdoc_inline_tag' => true,
42+
'phpdoc_no_access' => true,
43+
'phpdoc_no_package' => true,
44+
'phpdoc_scalar' => true,
45+
'phpdoc_summary' => true,
46+
'phpdoc_to_comment' => true,
47+
'phpdoc_trim' => true,
48+
'phpdoc_no_alias_tag' => ['type' => 'var'],
49+
'phpdoc_var_without_name' => true,
50+
'no_leading_import_slash' => true,
51+
'no_extra_consecutive_blank_lines' => ['break', 'continue', 'extra', 'return', 'throw', 'use', 'parenthesis_brace_block', 'square_brace_block', 'curly_brace_block'],
52+
'blank_line_before_return' => true,
53+
'self_accessor' => true,
54+
'array_syntax' => ['syntax' => 'short'],
55+
'no_short_echo_tag' => true,
56+
'full_opening_tag' => true,
57+
'no_trailing_comma_in_singleline_array' => true,
58+
'single_blank_line_before_namespace' => true,
59+
'single_line_after_imports' => true,
60+
'single_quote' => true,
61+
'no_singleline_whitespace_before_semicolons' => true,
62+
'cast_spaces' => true,
63+
'standardize_not_equals' => true,
64+
'ternary_operator_spaces' => true,
65+
'no_trailing_whitespace' => true,
66+
'trim_array_spaces' => true,
67+
'binary_operator_spaces' => ['align_equals' => false],
68+
'unary_operator_spaces' => true,
69+
'no_unused_imports' => true,
70+
'visibility_required' => true,
71+
'no_whitespace_in_blank_line' => true,
72+
))
73+
->setFinder(
74+
PhpCsFixer\Finder::create()
75+
->in('src')
76+
->in('tests')
77+
)
78+
;

.styleci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
preset: psr2
1+
preset: laravel

.travis.yml

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
language: php
22

3-
php:
4-
- 7.0
5-
- 7.1
6-
3+
git:
4+
depth: 1
75
env:
8-
matrix:
9-
- COMPOSER_FLAGS="--prefer-lowest"
10-
- COMPOSER_FLAGS=""
6+
global:
7+
- DEFAULT_COMPOSER_FLAGS="--no-interaction --no-progress --optimize-autoloader"
8+
- REPORT_TESTS_COVERAGE=0
9+
matrix:
10+
fast_finish: true
11+
include:
12+
- php: 7.0
13+
- php: 7.1
14+
env: REPORT_TESTS_COVERAGE=1
1115

1216
cache:
1317
directories:
@@ -23,5 +27,5 @@ script:
2327
- vendor/bin/phpunit --coverage-text --coverage-clover=coverage.clover
2428

2529
after_script:
26-
- wget https://scrutinizer-ci.com/ocular.phar
27-
- php ocular.phar code-coverage:upload --format=php-clover coverage.clover
30+
- if [ $REPORT_TESTS_COVERAGE == 1 ]; then wget https://scrutinizer-ci.com/ocular.phar; fi
31+
- if [ $REPORT_TESTS_COVERAGE == 1 ]; then php ocular.phar code-coverage:upload --format=php-clover coverage.clover; fi

README.md

Lines changed: 101 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Laravel Validator Rules - Credit Card
1+
# [WIP] Laravel Validator Rules - Credit Card
22

33
This rule will validate that a given credit card **number**, **expiration date** or **cvc** is valid.
44

@@ -20,6 +20,106 @@ This rule will validate that a given credit card **number**, **expiration date**
2020
</a>
2121
</p>
2222

23+
## Installation
24+
25+
```bash
26+
composer require laravel-validation-rules/credit-card
27+
```
28+
29+
## Usage
30+
### As FormRequest
31+
```php
32+
<?php
33+
34+
namespace App\Http\Requests;
35+
36+
use LVR\CreditCard\CardCvc;
37+
use LVR\CreditCard\CardNumber;
38+
use LVR\CreditCard\CardExpirationYear;
39+
use LVR\CreditCard\CardExpirationMonth;
40+
use Illuminate\Foundation\Http\FormRequest;
41+
42+
class CreditCardRequest extends FormRequest
43+
{
44+
/**
45+
* Get the validation rules that apply to the request.
46+
*
47+
* @return array
48+
*/
49+
public function rules()
50+
{
51+
return [
52+
'card_number' => ['required', new CardNumber],
53+
'expiration_year' => ['required', new CardExpirationYear($this->get('expiration_month'))],
54+
'expiration_month' => ['required', new CardExpirationMonth($this->get('expiration_year'))],
55+
'cvc' => ['required', new CardCvc($this->get('card_number'))]
56+
];
57+
}
58+
}
59+
```
60+
61+
### Card number
62+
#### From request
63+
```php
64+
$request->validate(
65+
['card_number' => '37873449367100'],
66+
['card_number' => new LVR\CreditCard\CardNumber]
67+
);
68+
```
69+
#### Directly
70+
```php
71+
(new LVR\CreditCard\Cards\Visa)
72+
->setCardNumber('4012888888881881')
73+
->isValidCardNumber()
74+
```
75+
76+
77+
### Card expiration
78+
#### From request
79+
```php
80+
// CardExpirationYear requires card expiration month
81+
$request->validate(
82+
['expiration_year' => '2017'],
83+
['expiration_year' => ['required', new LVR\CreditCard\CardExpirationYear($request->get('expiration_month'))]]
84+
);
85+
86+
// CardExpirationMonth requires card expiration year
87+
$request->validate(
88+
['expiration_month' => '11'],
89+
['expiration_month' => ['required', new LVR\CreditCard\CardExpirationMonth($request->get('expiration_year'))]]
90+
);
91+
92+
// CardExpirationDate requires date format
93+
$request->validate(
94+
['expiration_date' => '02-18'],
95+
['expiration_mont' => ['required', new LVR\CreditCard\CardExpirationDate('m-y')]]
96+
);
97+
```
98+
#### Directly
99+
```php
100+
LVR\CreditCard\Cards\Card::isValidExpirationDate(
101+
$expiration_year,
102+
$expiration_month
103+
);
104+
```
105+
106+
107+
### Card CVC
108+
#### From request
109+
```php
110+
// CardCvc requires card number to determine allowed cvc length
111+
$request->validate(
112+
['cvc' => '123'],
113+
['cvc' => new LVR\CreditCard\CardCvc($request->get('card_number'))]
114+
);
115+
116+
```
117+
#### Directly
118+
```php
119+
LVR\CreditCard\Cards\Card::isValidCvcLength($cvc);
120+
```
121+
122+
23123
### License
24124
This project is licensed under an Apache 2.0 license which you can find
25125
[in this LICENSE](https://github.com/laravel-validation-rules/credit-card/blob/master/LICENSE).

composer.json

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
"illuminate/translation": "^5.5"
88
},
99
"require-dev": {
10-
"friendsofphp/php-cs-fixer": "~2.0",
11-
"phpunit/phpunit": "~6.0",
10+
"friendsofphp/php-cs-fixer": "~2.6",
11+
"phpunit/phpunit": "6.2.*",
1212
"orchestra/testbench": "^3.5"
1313
},
1414
"autoload": {
@@ -22,7 +22,7 @@
2222
}
2323
},
2424
"scripts": {
25-
"cs-fix": "php-cs-fixer fix . --rules=@PSR2",
25+
"cs-fix": "php-cs-fixer fix .",
2626
"test": "phpunit --colors=always"
2727
},
2828
"license": "Apache-2.0",
@@ -31,5 +31,9 @@
3131
"name": "Darius Matulionis",
3232
"email": "[email protected]"
3333
}
34-
]
34+
],
35+
"config": {
36+
"preferred-install": "dist",
37+
"sort-packages": true
38+
}
3539
}

composer.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

phpunit.xml.dist

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,8 @@
2424
</filter>
2525
<logging>
2626
<log type="coverage-clover" target="coverage.xml" showUncoveredFiles="true"/>
27+
<log type="coverage-html" target="./test-reports" charset="UTF-8"
28+
yui="true" highlight="true"
29+
lowUpperBound="50" highLowerBound="80" />
2730
</logging>
2831
</phpunit>

src/CardCvc.php

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
3+
namespace LVR\CreditCard;
4+
5+
use LVR\CreditCard\Cards\Card;
6+
use Illuminate\Contracts\Validation\Rule;
7+
8+
class CardCvc implements Rule
9+
{
10+
const MSG_CARD_CVC_INVALID = 'validation.credit_card.card_cvc_invalid';
11+
12+
protected $message;
13+
14+
/**
15+
* Credit card number.
16+
*
17+
* @var string
18+
*/
19+
protected $card_number;
20+
21+
public function __construct(string $card_number)
22+
{
23+
$this->message = static::MSG_CARD_CVC_INVALID;
24+
$this->card_number = $card_number;
25+
}
26+
27+
/**
28+
* Determine if the validation rule passes.
29+
*
30+
* @param string $attribute
31+
* @param mixed $value
32+
*
33+
* @return bool
34+
*/
35+
public function passes($attribute, $value)
36+
{
37+
try {
38+
return Factory::makeFromNumber($this->card_number)->isValidCvc($value);
39+
} catch (\Exception $ex) {
40+
return false;
41+
}
42+
}
43+
44+
/**
45+
* Get the validation error message.
46+
*
47+
* @return string
48+
*/
49+
public function message()
50+
{
51+
return $this->message;
52+
}
53+
}

0 commit comments

Comments
 (0)