PHP code example of olssonm / swedish-entity

1. Go to this page and download the library: Download olssonm/swedish-entity library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

olssonm / swedish-entity example snippets



use Olssonm\SwedishEntity\Person;

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


use Olssonm\SwedishEntity\Organization;

(new Organization('556016-0680'))->valid()
// true


use Olssonm\SwedishEntity\Entity;

$entity = Entity::detect('600411-8177');

var_dump(get_class($entity))
// Olssonm\SwedishEntity\Person



use Olssonm\SwedishEntity\Person;

(new Person('071012-9735'))->format($characters = 12, $separator = true)
// 20071012-9735

(new Person('071012+9735'))->format($characters = 12, $separator = true)
// 19071012+9735

(new Person('200710129735'))->format()
// 071012-9735


use Olssonm\SwedishEntity\Organization;

(new Organization('5560160680'))->format($separator = true)
// 556016-0680

(new Organization('556016-0680'))->format($separator = false)
// 5560160680


$this->validate($request, [
    'number' => '


$this->validate($request, [
    'number' => '


use Illuminate\Support\Facades\Validator;

$validator = Validator::make($request->all(), [
    'number' => '

'organization_number' => [
    '

'organization_number' => [
    '


use Olssonm\SwedishEntity\Person;

$person = new Person('600411-8177');
$person->gender;
// Male


use Olssonm\SwedishEntity\Organization;

$organization = new Organization('212000-1355');
$organization->type;
// Stat, landsting och kommuner


use Olssonm\SwedishEntity\Entity;

$number = Entity::clean(' 212000-1355a');
// '212000-1355'


use Olssonm\SwedishEntity\Entity;
use Olssonm\SwedishEntity\Person;
use Olssonm\SwedishEntity\Organization;
use Olssonm\SwedishEntity\Exceptions\DetectException

try {
    $entity = Entity::detect('600411-8177');
} catch (DetectException $e) {
    // Handle exception
}

// PHP < 8
if(get_class($entity) == Person::class) {
    // Do stuff for person
} elseif(get_class($entity) == Organization::class) {
    // Do stuff for organization
}

// PHP 8
if($entity::class == Person::class) {
    // Do stuff for person
} elseif($entity::class == Organization::class) {
    // Do stuff for organization
}