maggomann/addressable

This Laravel package provides a minimal trait Addressable to add eloquent models using these addresses.

v0.2.2 2023-05-20 20:26 UTC

README

GitHub Tests Action Status Total Downloads

addressable

This package provides a minimal trait Addressable to add eloquent models using these addresses for laraval. The package will be extended over time. It was directly outsourced as a package before I start using this class modified in different projects.

Installation

You can install the package via composer:

composer require maggomann/addressable

You can install anything with the command

php artisan addressable:install
php artisan migrate

Or

You can publish and run the migrations with:

php artisan vendor:publish --tag="addressable-migrations"
php artisan migrate

Optionally, you can publish the configuration file with:

php artisan vendor:publish --tag="addressable-config"

Optionally, you can publish the translation files with:

php artisan vendor:publish --tag="addressable-translations"

How is it used?

use Illuminate\Database\Eloquent\Model;
use Maggomann\Addressable\Traits\Addressable;

class Player extends Models
{
    use Addressable;
}

//...
//...

$exampleAttributes = [
    'first_name' => 'first name',
    'last_name' => 'last name',
    'name' => 'name',
    'street_address' => 'street address',
    'street_addition' => 'street addition',
    'postal_code' => 'postal code',
    'city' => 'city',
    'country_code' => 'de',
    'state' => null,
    'company' => null,
    'job_title' => null,
    'is_preferred' => true,
    'is_main' => true,
];

$address = new Address();
$address->fill($exampleAttributes);
$address->withCategory($categoryIdOrCategoryClass);
$address->withGender($genderIdOrGenderClass);

Player::query()
    ->findOrFail(1)
    ->address()
    ->save($address);
use Illuminate\Database\Eloquent\Model;
use Maggomann\Addressable\Traits\Addressable;

class Player extends Models
{
    use Addressable;
}

//...
//...

$exampleAttributes = [
    'first_name' => 'first name',
    'last_name' => 'last name',
    'name' => 'name',
    'street_address' => 'street address',
    'street_addition' => 'street addition',
    'postal_code' => 'postal code',
    'city' => 'city',
    'country_code' => 'de',
    'state' => null,
    'company' => null,
    'job_title' => null,
    'is_preferred' => true,
    'is_main' => true,
];

$addressOne = new Address();
$addressOne->fill($exampleAttributes);
$addressOne->withCategory($categoryIdOrCategoryClass);
$addressOne->withGender($genderIdOrGenderClass);

$addressTwo = new Address();
$addressTwo->fill($exampleAttributes);
$addressTwo->withCategory($categoryIdOrCategoryClass);
$addressTwo->withGender($genderIdOrGenderClass);

$player = Player::query()->findOrFail(1);

$player->addresses()->save($addressOne);
$player->addresses()->save($addressTwo);

// or

$player->addresses()->saveMany(
    collect([
        $addressOne,
        $addressTwo,
    ])
);
use Illuminate\Database\Eloquent\Model;
use Maggomann\Addressable\Domain\Actions\UpdateOrCreateAddressAction;
use Maggomann\Addressable\Traits\Addressable;

class Player extends Models
{
    use Addressable;
}

//...
//...

$exampleAttributes = [
    'first_name' => 'first name',
    'last_name' => 'last name',
    'name' => 'name',
    'street_address' => 'street address',
    'street_addition' => 'street addition',
    'postal_code' => 'postal code',
    'city' => 'city',
    'country_code' => 'de',
    'state' => null,
    'company' => null,
    'job_title' => null,
    'is_preferred' => true,
    'is_main' => true,
];

$newAddress = app(UpdateOrCreateAddressAction::class)->execute(
    $player,
    AddressData::from($exampleAttributes)
);

// or

$updatedAddress = app(UpdateOrCreateAddressAction::class)->execute(
    $player,
    AddressData::from($exampleAttributes)
    $player->addresses()->first()
);

The address table currently comes with the following attributes:

    'attributes' => [
        'addresses' => [
            'category_id' => 'address type',
            'gender_id' => 'salutation',
            'first_name' => 'first name',
            'last_name' => 'last name',
            'name' => 'first_name', 'last_name' => 'last_name',
            'street_address' => 'streets and no.',
            'street_addition' => 'street addition',
            'postal_code' => 'postal code',
            'city' => 'city',
            'country_code' => 'country',
            'state' => 'federal state',
            'company' => 'company',
            'job_title' => 'job title',
            'is_preferred' => 'prefer',
            'is_main' => 'is main address',
            'latitude' => 'latitude',
            'longitude' => 'longitude',
        ],
    ],

Preinstalled salutations

By default, these are located in the table: address_genders

    'address_genders' => [
        'title' => [
            'male' => 'Mr.',
            'female' => 'Ms.',
            'diverse' => 'Various',
        ],
    ],

Preinstalled categories

By default, these are located in the table: address_categories

    'address_categories' => [
        'title' => [
            'standard' => 'Default address',
            'billing' => 'Billing addres',
            'shipping' => 'Delivery address',
        ],
    ],

Testing

composer test
composer test:pest-coverage

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

The MIT License (MIT). Please see License File for more information.