Skip to content

Commit

Permalink
Documentation fixes (#3)
Browse files Browse the repository at this point in the history
* Fix for Laravel-messages

* Improved documentation

* Tweaks and fixes
  • Loading branch information
olssonm authored Dec 8, 2020
1 parent 175317a commit 46a0d0a
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 25 deletions.
33 changes: 23 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,19 @@
[![Scrutinizer Score][scrutinizer-ico]][scrutinizer-link]
[![Software License][license-ico]](LICENSE.md)

Validate, format and extract data for Swedish personnummer (social security numbers) and organizationsnummer (organizational numbers).
**Validate, format and extract data for Swedish personnummer (social security numbers) and organisationsnummer (organizational numbers).**

This package also handles the temporary personal identity number known as "Samordningsnummer" (a.k.a. coordination number).

Includes [validators for Laravel](#laravel-validators).
Also includes [validators for Laravel](#laravel-validators).


The benefits of this package – while not always strictly according to the standard – is the ability to format using both short/long (10 or 12 characters) without or with a seperator (i.e. 11/13 characters).

Note that companies always consists of 10/11 characters (with or without an optional seperator).

This package use the excellent [personnummer/php](https://github.com/personnummer/php)-package as it's basis for the social security-handling, but with some additional attributes.
This package use the excellent [personnummer/php](https://github.com/personnummer/php)-package as it's basis for the social security-handling, but with some additional attributes and methods.

## Installation

Expand All @@ -31,13 +31,15 @@ composer require olssonm/swedish-entity
### Validation

```php
<?php
use Olssonm\SwedishEntity\Person;

(new Person('600411-8177'))->valid()
// true
```

```php
<?php
use Olssonm\SwedishEntity\Organization;

(new Organization('556016-0680'))->valid()
Expand All @@ -49,6 +51,7 @@ use Olssonm\SwedishEntity\Organization;
*⚠️ If the `detect`-method fails, an `Olssonm\SwedishEntity\Exceptions\DetectException` will be thrown.*

```php
<?php
use Olssonm\SwedishEntity\Entity;

$entity = Entity::detect('600411-8177');
Expand All @@ -65,21 +68,23 @@ var_dump(get_class($entity))
#### Person

```php
<?php
use Olssonm\SwedishEntity\Person;

(new Person('600411-8177'))->format($characters = 12, $seperator = true)
// 19600411-8177
(new Person('071012-9735'))->format($characters = 12, $seperator = true)
// 20071012-9735

(new Person('100411+8177'))->format($characters = 12, $seperator = true)
// 19100411+8177
(new Person('071012+9735'))->format($characters = 12, $seperator = true)
// 19071012+9735

(new Person('19100411+8177'))->format($characters = 10, $seperator = false)
// 1004118177
(new Person('200710129735'))->format()
// 071012-9735
```

#### Organization

```php
<?php
use Olssonm\SwedishEntity\Organization;

(new Organization('5560160680'))->format($seperator = true)
Expand All @@ -94,6 +99,7 @@ use Olssonm\SwedishEntity\Organization;
The package registrers the "entity" rule, which accepts the parameters `any`, `organization` or `person`.

```php
<?php
$this->validate($request, [
'number' => 'required|entity:organization'
]);
Expand All @@ -102,6 +108,7 @@ $this->validate($request, [
You may also omit the parameter and the validator will fallback to `any`

```php
<?php
$this->validate($request, [
'number' => 'required|entity'
]);
Expand All @@ -110,6 +117,9 @@ $this->validate($request, [
Custom messages

```php
<?php
use Illuminate\Support\Facades\Validator;

$validator = Validator::make($request->all(), [
'number' => 'required|entity:person'
], [
Expand Down Expand Up @@ -140,6 +150,7 @@ $validator = Validator::make($request->all(), [
**Example**

```php
<?php
use Olssonm\SwedishEntity\Person;

$person = new Person('600411-8177');
Expand All @@ -160,6 +171,7 @@ $person->gender;
**Example**

```php
<?php
use Olssonm\SwedishEntity\Organization;

$organization = new Organization('212000-1355');
Expand All @@ -178,6 +190,7 @@ EF (Enskild firma) – while technically a company/organization, uses the propri
If you need to after the validation check type;

```php
<?php
use Olssonm\SwedishEntity\Entity;
use Olssonm\SwedishEntity\Person;
use Olssonm\SwedishEntity\Organization;
Expand Down Expand Up @@ -210,7 +223,7 @@ The MIT License (MIT). Please see the [License File](LICENSE.md) for more inform
© 2020 [Marcus Olsson](https://marcusolsson.me).

[version-ico]: https://img.shields.io/packagist/v/olssonm/swedish-enity.svg?style=flat-square
[build-ico]: https://img.shields.io/github/workflow/status/olssonm/swedish-entity/run-tests.svg?style=flat-square
[build-ico]: https://img.shields.io/github/workflow/status/olssonm/swedish-entity/run-tests.svg?style=flat-square&label=tests
[license-ico]: https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square
[php-ico]: https://img.shields.io/packagist/php-v/olssonm/swedish-entity.svg?style=flat-square
[scrutinizer-ico]: https://img.shields.io/scrutinizer/g/olssonm/swedish-entity.svg?style=flat-square
Expand Down
2 changes: 1 addition & 1 deletion src/SwedishEntityServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function boot()
}

return $object->valid();
});
}, 'The :attribute is not a valid entity.');
}

/**
Expand Down
53 changes: 39 additions & 14 deletions tests/SwedishEntityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,19 +159,30 @@ public function testUnsuccessfulDetection()
public function testLaravelValidator()
{
if (class_exists(Validator::class)) {
$this->assertTrue($this->validate('556016-0680', 'organization'));
$this->assertTrue($this->validate('5560160680', 'any'));
$this->assertTrue($this->validate('556016-0680'));
$this->assertFalse($this->validate('5560160680', 'person'));

$this->assertTrue($this->validate('600411-8177', 'person'));
$this->assertTrue($this->validate('6004118177', 'any'));
$this->assertTrue($this->validate('19600411-8177', 'any'));
$this->assertFalse($this->validate('600411-8177', 'organization'));

$this->assertFalse($this->validate('aabbcc-ddee', 'any'));
$this->assertFalse($this->validate('aabbccddee', 'organization'));
$this->assertFalse($this->validate('00aabbccddee', 'person'));
$this->assertTrue($this->validateLaravel('556016-0680', 'organization'));
$this->assertTrue($this->validateLaravel('5560160680', 'any'));
$this->assertTrue($this->validateLaravel('556016-0680'));
$this->assertFalse($this->validateLaravel('5560160680', 'person'));

$this->assertTrue($this->validateLaravel('600411-8177', 'person'));
$this->assertTrue($this->validateLaravel('6004118177', 'any'));
$this->assertTrue($this->validateLaravel('19600411-8177', 'any'));
$this->assertFalse($this->validateLaravel('600411-8177', 'organization'));

$this->assertFalse($this->validateLaravel('aabbcc-ddee', 'any'));
$this->assertFalse($this->validateLaravel('aabbccddee', 'organization'));
$this->assertFalse($this->validateLaravel('00aabbccddee', 'person'));
}
}

/** @test */
public function testLaravelValidatorWithMessage()
{
if (class_exists(Validator::class)) {
$this->assertEquals('The number is not a valid entity.', $this->validateLaravelMessage('aabbcc-ddee', 'any'));
$this->assertEquals('Ogiltigt organisationsnummer.', $this->validateLaravelMessage('aabbccddee', 'organization', 'Ogiltigt organisationsnummer.'));
$this->assertEquals('Number är ett ogiltigt personnummer.', $this->validateLaravelMessage('00aabbccddee', 'person', ':Attribute är ett ogiltigt personnummer.'));
$this->assertEquals('number är ett ogiltigt personnummer.', $this->validateLaravelMessage('00aabbccddee', 'person', ':attribute är ett ogiltigt personnummer.'));
}
}

Expand All @@ -187,7 +198,7 @@ protected function getPackageProviders($app)
];
}

private function validate($number, $type = null)
private function validateLaravel($number, $type = null)
{
$data = ['number' => $number];
$validator = Validator::make($data, [
Expand All @@ -197,6 +208,20 @@ private function validate($number, $type = null)
return $validator->passes();
}

private function validateLaravelMessage($number, $type = null, $message = null)
{
$data = ['number' => $number];
$validator = Validator::make($data, [
'number' => sprintf('entity:%s', $type)
], [
'number.entity' => $message
]);

$errors = $validator->errors();

return $errors->first('number');
}

public function tearDown(): void
{
parent::tearDown();
Expand Down

0 comments on commit 46a0d0a

Please sign in to comment.