PHP code example of germania-kg / addresses

1. Go to this page and download the library: Download germania-kg/addresses 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/ */

    

germania-kg / addresses example snippets



use Germania\Address;

$address = new Address;

// All these are string or null
echo $address->getStreet1();
echo $address->getStreet2();
echo $address->getZip();
echo $address->getLocation();
echo $address->getCountry();
echo $address->getType();

// Depending on street1/street2, zip, and location
echo $address->isEmpty() ? "empty" : "has address data";

// Setters accept string or null,
// returning fluent interface.
$address->setStreet1( $new_street_1 )
  ->setStreet2( $new_street_2 )
  ->setZip( $new_zip ) 
  ->setLocation( $new_location )
  ->setsetCountry( $new_country )
  ->setType( $new_type );

$adr2 = $address->getAddress();
$adr2 === $address; // true


use Germania\Address;

$factory = new AddressFactory;
$address = $factory([
  'type'     => 'office',
  'street1'  => 'Street name 1',
  'street2'  => null,
  'zip'      => 'DG2JQ',
  'location' => 'Dumfries',
  'country'  => 'Scotland'
]);

// Given a custom implementation
// and $address_data from database
class CustomAddress extends Address {}

$address_data = array( ... );

$factory = new AddressFactory( CustomAddress::class );
$address = $factory( $address_data );

echo get_class( $address ); // CustomAddress


use Germania\Address\AddressFactory;
use Germania\Address\AddressProviderInterface;
use Germania\Address\Address;

// Custom AddressProviderInterface implementation
class MyAddressProvider implements AddressProviderInterface
{ };

$address_provider = new MyAddressProvider;
$factory = new AddressFactory;

$address_provider = $factory->apply($provider_address, [
	'street1' => 'Updated information'
]);

$old_address = new Address;
$new_address = factory->apply(old_address, new_address_data);
echo get_class( $new_address ); // Address


use Germania\AddressProviderInterface;
use Germania\AddressProviderTrait;

class MyClass implements AddressProviderInterface
{
  use AddressProviderTrait;
}

$obj = new MyClass;
$address = $obj->getAddress();


use Germania\Address;
use Germania\AddressAwareInterface;
use Germania\AddressAwareTrait;  

class MyClass implements AddressProviderInterface
{
  use AddressAwareTrait;
}

$obj = new MyClass;
$address = $obj->getAddress();
// null

$obj->setAddress( new Address );
print_r( $obj->getAddress() );
// Germania\Address