Skip to content

Commit 5d40673

Browse files
authored
Merge pull request #60 from fastbill/#59/master/add-iban-validation
#59/master/add iban validation
2 parents ac3133b + b8b2c7d commit 5d40673

File tree

3 files changed

+46
-1
lines changed

3 files changed

+46
-1
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
"require": {
66
"guzzlehttp/guzzle": "^7.0",
77
"php": "^8.0",
8-
"ext-simplexml": "*"
8+
"ext-simplexml": "*",
9+
"jschaedl/iban-validation": "^2.5"
910
},
1011
"require-dev": {
1112
"phpunit/phpunit": "^9.5.25"

src/Customer/CustomerValidator.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
namespace FastBillSdk\Customer;
55

66
use FastBillSdk\Common\MissingPropertyException;
7+
use Iban\Validation\Iban;
8+
use Iban\Validation\Validator;
79

810
class CustomerValidator
911
{
@@ -35,6 +37,12 @@ public function validateRequiredCreationProperties(CustomerEntity $entity): arra
3537
$errorMessages[] = $exception->getMessage();
3638
}
3739

40+
try {
41+
$this->checkIban($entity);
42+
} catch (MissingPropertyException $exception) {
43+
$errorMessages[] = $exception->getMessage();
44+
}
45+
3846
return $errorMessages;
3947
}
4048

@@ -108,4 +116,15 @@ private function isBusiness(CustomerEntity $entity): bool
108116
{
109117
return $entity->customerType === CustomerEntity::CUSTOMER_TYPE_BUSINESS;
110118
}
119+
120+
private function checkIban(CustomerEntity $entity)
121+
{
122+
if ($entity->bankIban) {
123+
$iban = new Iban($entity->bankIban);
124+
$validator = new Validator();
125+
if (!$validator->validate($iban)) {
126+
throw new MissingPropertyException('The property bankIban is not valid!');
127+
}
128+
}
129+
}
111130
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace FastBillSdkTest\Customer;
5+
6+
use FastBillSdk\Customer\CustomerEntity;
7+
use FastBillSdk\Customer\CustomerValidator;
8+
use FastBillSdkTest\Common\BaseTestTrait;
9+
use PHPUnit\Framework\TestCase;
10+
11+
class CustomerValidationTest extends TestCase
12+
{
13+
use BaseTestTrait;
14+
15+
public function testIbanValidation()
16+
{
17+
$customer = new CustomerEntity();
18+
$customer->bankIban = 'DE99370400440532013000'; // Example wrong IBAN
19+
20+
$validator = new CustomerValidator();
21+
$errors = $validator->validateRequiredCreationProperties($customer);
22+
23+
self::assertContains('The property bankIban is not valid!', $errors);
24+
}
25+
}

0 commit comments

Comments
 (0)