Skip to content

Commit 531ad31

Browse files
committed
cleaner code
1 parent 7ae4d8e commit 531ad31

File tree

8 files changed

+42
-19
lines changed

8 files changed

+42
-19
lines changed

README.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# My Validation rules for Laravel
2+
In this repository I will add validation rules that I use in my SaaS app. I think especially Swedish developers will find them handy.
3+
4+
## Requirements
5+
- PHP 8.0|8.1+
6+
- Laravel v9.0+
7+
8+
## Installation
9+
```bash
10+
composer require tanthammar/laravel-rules
11+
```
12+
13+
## Rules
14+
See the src/Rules folder.
15+
16+
## Documentation
17+
There won't be much documentation written, this repository will grow as I add items.
18+
Hopefully the source code contains enough hints to use the components.
19+
20+
They are all used in the same way, following Laravel conventions.
21+
22+
Example:
23+
```php
24+
use Tanthammar\LaravelRules\Rules\PhoneNumber;
25+
use Tanthammar\LaravelRules\Rules\PersonNummer;
26+
27+
SomeField::make('phone')
28+
->rules([ new PhoneNumber ])
29+
SomeField::make('person_nummer')
30+
->rules([ new PersonNummer ])
31+
```
32+
33+
34+
35+

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,9 @@
2020
"php": "^8.1",
2121
"brick/phonenumber": "^0.4.0",
2222
"byrokrat/banking": "^2.2",
23-
"laravel/framework": "^9.0",
2423
"intervention/validation": "^3.2",
24+
"laravel/framework": "^9.0",
25+
"personnummer/personnummer": "^3.0",
2526
"spatie/laravel-package-tools": "^1.9.2"
2627
},
2728
"autoload": {

src/Rules/BankGiro.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,10 @@ class BankGiro implements Rule
1919
public function passes($attribute, $value): bool
2020
{
2121
try {
22-
$account = (new BankgiroFactory)->createAccount($value);
22+
return (new BankgiroFactory)->createAccount($value)->getBankName() === BankNames::BANK_BANKGIRO;
2323
} catch (Exception $e) {
2424
return false;
2525
}
26-
return $account->getBankName() === BankNames::BANK_BANKGIRO;
2726
}
2827

2928
/**

src/Rules/BankKonto.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,10 @@ class BankKonto implements Rule
1818
public function passes($attribute, $value): bool
1919
{
2020
try {
21-
$account = (new AccountFactory)->createAccount($value);
21+
return filled((new AccountFactory)->createAccount($value));
2222
} catch (Exception $e) {
2323
return false;
2424
}
25-
return filled($account);
2625
}
2726

2827
/**

src/Rules/OrgNummer.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,10 @@ class OrgNummer implements Rule
1818
public function passes($attribute, $value): bool
1919
{
2020
try {
21-
$boolean = (new Luhn)->passes(attribute: null, value: Helpers::clean_numbers($value));
21+
return (new Luhn)->passes(attribute: null, value: Helpers::clean_numbers($value));
2222
} catch (\Exception $e) {
2323
return false;
2424
}
25-
return $boolean;
2625
}
2726

2827
/**

src/Rules/PersonNummer.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,8 @@
22

33
namespace Tanthammar\LaravelRules\Rules;
44

5-
use App\Helpers\BookonsHelpers;
65
use Illuminate\Contracts\Validation\Rule;
76
use Personnummer\Personnummer as PersonNummerVerifier;
8-
use Personnummer\PersonnummerException;
97
use TantHammar\LaravelRules\Helpers;
108

119
class PersonNummer implements Rule
@@ -20,11 +18,10 @@ class PersonNummer implements Rule
2018
public function passes($attribute, $value): bool
2119
{
2220
try {
23-
$boolean = PersonNummerVerifier::valid(Helpers::clean_numbers($value));
21+
return PersonNummerVerifier::valid(Helpers::clean_numbers($value));
2422
} catch (\Exception $e) {
2523
return false;
2624
}
27-
return $boolean;
2825
}
2926

3027
/**

src/Rules/PersonOrOrgNummer.php

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,7 @@
22

33
namespace Tanthammar\LaravelRules\Rules;
44

5-
use App\Helpers\BookonsHelpers;
65
use Illuminate\Contracts\Validation\Rule;
7-
use Intervention\Validation\Validator;
8-
use Intervention\Validation\Exception\ValidationException;
9-
use Personnummer\Personnummer as PersonNummerVerifier;
10-
use Personnummer\PersonnummerException;
11-
use TantHammar\LaravelRules\Helpers;
126

137
class PersonOrOrgNummer implements Rule
148
{

src/Rules/PlusGiro.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,10 @@ class PlusGiro implements Rule
1919
public function passes($attribute, $value): bool
2020
{
2121
try {
22-
$boolean = (new PlusgiroFactory)->createAccount($value)->getBankName() === BankNames::BANK_PLUSGIRO;
22+
return (new PlusgiroFactory)->createAccount($value)->getBankName() === BankNames::BANK_PLUSGIRO;
2323
} catch (Exception $e) {
2424
return false;
2525
}
26-
return $boolean;
2726
}
2827

2928
/**

0 commit comments

Comments
 (0)