PHP code example of tiny-blocks / country

1. Go to this page and download the library: Download tiny-blocks/country 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/ */

    

tiny-blocks / country example snippets




declare(strict_types=1);

use TinyBlocks\Country\Alpha2Code;

$alpha2 = Alpha2Code::BRAZIL;

$alpha2->value;              # BR
$alpha2->toAlpha3()->value;  # BRA
$alpha2->toNumeric()->value; # 076
$alpha2->toString();         # BR



declare(strict_types=1);

use TinyBlocks\Country\NumericCode;

$numeric = NumericCode::BRAZIL;

$numeric->value;             # 076
$numeric->toInteger();       # 76
$numeric->toAlpha2()->value; # BR
$numeric->toAlpha3()->value; # BRA



declare(strict_types=1);

use TinyBlocks\Country\Alpha2Code;
use TinyBlocks\Country\Country;

$country = Country::from(code: Alpha2Code::BRAZIL);

$country->name();           # Brazil
$country->alpha2()->value;  # BR
$country->alpha3()->value;  # BRA
$country->numeric()->value; # 076



declare(strict_types=1);

use TinyBlocks\Country\Country;
use TinyBlocks\Country\NumericCode;

$country = Country::from(code: NumericCode::UNITED_STATES_OF_AMERICA);

$country->name();           # United States
$country->alpha2()->value;  # US
$country->alpha3()->value;  # USA



declare(strict_types=1);

use TinyBlocks\Country\Country;

Country::fromString(code: 'BR')->name();   # Brazil
Country::fromString(code: 'USA')->name();  # United States
Country::fromString(code: '076')->name();  # Brazil



declare(strict_types=1);

use TinyBlocks\Country\Country;

Country::tryFromString(code: 'BR')?->alpha2()->value; # BR
Country::tryFromString(code: 'XYZ');                  # null



declare(strict_types=1);

use TinyBlocks\Country\Country;
use TinyBlocks\Country\Subdivision;

$subdivision = Subdivision::fromString(code: 'BR-SP');

$subdivision->code();                                              # BR-SP
$subdivision->name();                                              # São Paulo
$subdivision->category()->value;                                   # State
$subdivision->category()->isState();                               # true
$subdivision->country()->name();                                   # Brazil
$subdivision->belongsTo(country: Country::fromString(code: 'BR')); # true



declare(strict_types=1);

use TinyBlocks\Country\Alpha2Code;
use TinyBlocks\Country\Country;

$subdivisions = Country::from(code: Alpha2Code::BRAZIL)->subdivisions();

$subdivisions->count();      # 27
$subdivisions->isEmpty();    # false



declare(strict_types=1);

use TinyBlocks\Country\Countries;

$countries = Countries::all();

$countries->count(); # 249



declare(strict_types=1);

use TinyBlocks\Country\Alpha2Code;
use TinyBlocks\Country\Country;

$timezones = Country::from(code: Alpha2Code::JAPAN)->timezones();

$timezones->count();                                 # 1
$timezones->default()->value;                        # Asia/Tokyo
$timezones->contains(iana: 'Asia/Tokyo');            # true
$timezones->contains(iana: 'America/New_York');      # false
$timezones->findByIdentifierOrUtc(iana: 'X')->value; # UTC