Skip to content

Commit

Permalink
Merge pull request #132 from sandervankasteel/feature/GbVatNumber
Browse files Browse the repository at this point in the history
Added basic GB VAT number validation
  • Loading branch information
sandervankasteel authored Dec 12, 2022
2 parents 031d62e + 9861c4a commit 5d09933
Show file tree
Hide file tree
Showing 14 changed files with 98 additions and 19 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/codestyle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ jobs:
uses: actions/checkout@v3

- name: Run PHP CS Fixer
uses: docker://oskarstark/php-cs-fixer-ga:3.4.0
uses: docker://oskarstark/php-cs-fixer-ga:3.13.0
with:
args: --config=.php-cs-fixer.php --allow-risky=yes --dry-run
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,6 @@
"minimum-stability": "dev",
"prefer-stable": true,
"scripts": {
"codestyle-fix": "./vendor/bin/php-cs-fixer fix"
"codestyle-fix": "./vendor/bin/php-cs-fixer --config=.php-cs-fixer.php fix"
}
}
17 changes: 9 additions & 8 deletions docker/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
FROM php:8.0-alpine
FROM php:8.1-alpine

RUN apk update && \
apk add composer \
bash \
php8-dom \
php8-tokenizer \
php8-fileinfo \
php8-xml \
php8-xmlwriter \
php8-pecl-xdebug
php81-bcmath \
php81-dom \
php81-tokenizer \
php81-fileinfo \
php81-xml \
php81-xmlwriter \
php81-pecl-xdebug

RUN docker-php-ext-install bcmath
RUN docker-php-ext-install bcmath && docker-php-ext-enable bcmath

ENTRYPOINT ["tail"]
CMD ["-f","/dev/null"]
12 changes: 12 additions & 0 deletions docs/locale/GB/Company/VatNumber.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# VatNumber
### `LaravelExtendedValidation\Rules\Locale\GB\Company\VatNumber`

Determines if the supplied VAT number is a valid British VAT number.

A valid examples are `GB123456789` or `GB123456789012`. They have to start with GB and be either 9 or 12 characters in length.

## Constructor argument(s)

```php
none
```
1 change: 1 addition & 0 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ nav:
- 'Denmark (DK)':
- locale/DK/Address/PostalCode.md
- 'Great Britain (GB)':
- locale/GB/Company/VatNumber.md
- locale/GB/Address/PostalCode.md
- 'Germany (DE)':
- locale/DE/Company/VatNumber.md
Expand Down
3 changes: 2 additions & 1 deletion sonar-project.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ sonar.organization=sandervankasteel
sonar.projectKey=sandervankasteel_laravel-extended-validation
sonar.projectName=Laravel Extended Validation

sonar.sources=.
sonar.sources=src/
sonar.tests=tests/
2 changes: 1 addition & 1 deletion src/Rules/Color/HexColor.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public function passes($attribute, $value)

$colours = str_split(
($startsWithHashSign) ? $colourCodes[1] : $value,
2);
2);

$validColourRange = false;

Expand Down
2 changes: 1 addition & 1 deletion src/Rules/Locale/BE/Company/VatNumber.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public function passes($attribute, $value): bool

return abs(
bcmod(Str::of($numbers)->limit(8, ''), '97') - 97
) == substr($numbers, -2);
) == substr($numbers, -2);
}

/**
Expand Down
41 changes: 41 additions & 0 deletions src/Rules/Locale/GB/Company/VatNumber.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace LaravelExtendedValidation\Rules\Locale\GB\Company;

use Illuminate\Contracts\Validation\Rule;
use Illuminate\Support\Str;

class VatNumber implements Rule
{
/**
* @inheritDoc
*/
public function passes($attribute, $value): bool
{
/** @var string $numbers */
$numbers = Str::of($value)
->replace(' ', '')
->upper()
->split('/GB/')
->filter()
->first();

if (! Str::of($value)->upper()->startsWith('GB')) {
return false;
}

if (Str::of($numbers)->length() !== 9 && Str::of($numbers)->length() !== 12) {
return false;
}

return true;
}

/**
* @inheritDoc
*/
public function message(): string
{
return ':attribute does not contain a valid British VAT number';
}
}
2 changes: 1 addition & 1 deletion src/Rules/Locale/NL/Company/VatNumber.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function passes($attribute, $value): bool
->filter()
->values()
->first()
));
));

$total = 0;
$numbers->each(static function ($item, $index) use (&$total) {
Expand Down
4 changes: 2 additions & 2 deletions tests/Barcode/ISBN10Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@
test('that the attribute is being returned in the error message', function () {
$sut = new ISBN10();
expect(
$sut->message()
)->toContain(':attribute');
$sut->message()
)->toContain(':attribute');
});
4 changes: 2 additions & 2 deletions tests/DateAndTime/TimeZoneAbbrTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,6 @@
$sut->acceptedTimezones = [];

expect(
$sut->passes('some-attribute', 'cest')
)->toBe(true);
$sut->passes('some-attribute', 'cest')
)->toBe(true);
});
24 changes: 24 additions & 0 deletions tests/Locale/GB/VatNumberTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

use LaravelExtendedValidation\Rules\Locale\GB\Company\VatNumber;

test('that we can successfully validate VAT numbers', function ($vatNumber, $expectedOutput) {
$sut = new VatNumber();

expect(
$sut->passes('some-attribute', $vatNumber)
)->toBe($expectedOutput);
})->with([
['GB123456789', true],
['GB123456789012', true],
['GB1234567890', false],
['GB12345678', false],
['FR3285520050', false],
]);

test('that we get the attribute back from the error message', function () {
$sut = new VatNumber();
expect(
$sut->message()
)->toContain(':attribute');
});
1 change: 0 additions & 1 deletion tests/Pest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

uses(TestCase::class)
->beforeEach(function () {

/** @var $app Application */
$app = $this->app;

Expand Down

0 comments on commit 5d09933

Please sign in to comment.