├── CHANGELOG.md ├── CODE_OF_CONDUCT.md ├── CONTRIBUTING.md ├── LICENSE ├── README.md ├── composer.json ├── config └── config.php ├── database └── migrations │ ├── 2020_01_01_000001_create_addresses_table.php │ └── 2020_01_01_000002_alter_addresses_table_make_family_name_nullable.php ├── phpstan.neon.dist └── src ├── Console └── Commands │ ├── MigrateCommand.php │ ├── PublishCommand.php │ └── RollbackCommand.php ├── Models └── Address.php ├── Providers └── AddressesServiceProvider.php └── Traits └── Addressable.php /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Rinvex Addresses Change Log 2 | 3 | All notable changes to this project will be documented in this file. 4 | 5 | This project adheres to [Semantic Versioning](CONTRIBUTING.md). 6 | 7 | 8 | ## [v6.1.0] - 2022-02-14 9 | - Update composer dependencies to Laravel v9 10 | - Fix `findByDistance` method compatibility 11 | - Override latitude & longitude column names (fix #22) 12 | 13 | ## [v6.0.0] - 2021-08-22 14 | - Drop PHP v7 support, and upgrade rinvex package dependencies to next major version 15 | - Update composer dependencies 16 | - Merge rules instead of resetting, to allow adequate model override 17 | - Fix constructor initialization order (fill attributes should come next after merging fillables & rules) 18 | - Set validation rules in constructor for consistency & flexibility 19 | - Upgrade to GitHub-native Dependabot 20 | - Enabled use of Google api key 21 | - Utilize SoftDeletes 22 | - Define morphMany parameters explicitly 23 | - Simplify service provider model registration into IoC 24 | - Enable StyleCI risky mode 25 | 26 | ## [v5.0.1] - 2020-12-25 27 | - Add support for PHP v8 28 | 29 | ## [v5.0.0] - 2020-12-22 30 | - Upgrade to Laravel v8 31 | - Move custom eloquent model events to module layer from core package layer 32 | - Refactor and tweak Eloquent Events 33 | 34 | ## [v4.1.0] - 2020-06-15 35 | - Fix for Events namespace 36 | - Drop using rinvex/laravel-cacheable from core packages for more flexibility 37 | - Caching should be handled on the application layer, not enforced from the core packages 38 | - Drop PHP 7.2 & 7.3 support from travis 39 | 40 | ## [v4.0.6] - 2020-05-30 41 | - Remove default indent size config 42 | - Add strip_tags validation rule to string fields 43 | - Specify events queue 44 | - Explicitly specify relationship attributes 45 | - Add strip_tags validation rule 46 | - Explicitly define relationship name 47 | 48 | ## [v4.0.5] - 2020-04-12 49 | - Fix ServiceProvider registerCommands method compatibility 50 | 51 | ## [v4.0.4] - 2020-04-09 52 | - Tweak artisan command registration 53 | - Reverse commit "Convert database int fields into bigInteger" 54 | - Refactor publish command and allow multiple resource values 55 | 56 | ## [v4.0.3] - 2020-04-04 57 | - Fix namespace issue 58 | 59 | ## [v4.0.2] - 2020-04-04 60 | - Enforce consistent artisan command tag namespacing 61 | - Enforce consistent package namespace 62 | - Drop laravel/helpers usage as it's no longer used 63 | 64 | ## [v4.0.1] - 2020-03-20 65 | - Convert into bigInteger database fields 66 | - Add shortcut -f (force) for artisan publish commands 67 | - Fix migrations path 68 | 69 | ## [v4.0.0] - 2020-03-15 70 | - Upgrade to Laravel v7.1.x & PHP v7.4.x 71 | 72 | ## [v3.0.2] - 2020-03-13 73 | - Tweak TravisCI config 74 | - Add migrations autoload option to the package 75 | - Tweak service provider `publishesResources` 76 | - Update StyleCI config 77 | 78 | ## [v3.0.1] - 2019-12-18 79 | - Fix `migrate:reset` args as it doesn't accept --step 80 | - Create event classes and map them in the model 81 | 82 | ## [v3.0.0] - 2019-09-23 83 | - Upgrade to Laravel v6 and update dependencies 84 | 85 | ## [v2.1.1] - 2019-06-03 86 | - Enforce latest composer package versions 87 | 88 | ## [v2.1.0] - 2019-06-02 89 | - Update composer deps 90 | - Drop PHP 7.1 travis test 91 | - Refactor migrations and artisan commands, and tweak service provider publishes functionality 92 | 93 | ## [v2.0.0] - 2019-03-03 94 | - Rename environment variable QUEUE_DRIVER to QUEUE_CONNECTION 95 | - Require PHP 7.2 & Laravel 5.8 96 | - Apply PHPUnit 8 updates 97 | 98 | ## [v1.0.2] - 2018-12-22 99 | - Update composer dependencies 100 | - Add PHP 7.3 support to travis 101 | 102 | ## [v1.0.1] - 2018-10-05 103 | - Fix wrong composer package version constraints 104 | 105 | ## [v1.0.0] - 2018-10-01 106 | - Enforce Consistency 107 | - Support Laravel 5.7+ 108 | - Rename package to rinvex/laravel-addresses 109 | 110 | ## [v0.0.4] - 2018-09-22 111 | - Update travis php versions 112 | - Define polymorphic relationship parameters explicitly 113 | - Rename lat/lng to latitude/longitude and change database column type to decimal 114 | - Require composer package rinvex/countries and tweak country validation rule 115 | - Simplify address fields 116 | - Require full name address field 117 | - Enforce consistency 118 | - Add soft deletes 119 | - Drop StyleCI multi-language support (paid feature now!) 120 | - Update composer dependencies 121 | - Split full_name into given_name and family_name fields 122 | - Prepare and tweak testing configuration 123 | - Update StyleCI options 124 | - Update PHPUnit options 125 | - Add address model factory 126 | - Update PHPUnit options 127 | 128 | ## [v0.0.3] - 2018-02-18 129 | - Update supplementary files 130 | - Update composer dependencies 131 | - Add PublishCommand to artisan 132 | - Add Rollback Console Command 133 | - Add PHPUnitPrettyResultPrinter 134 | - Require PHP v7.1.3 135 | - Typehint method returns 136 | - Drop useless model contracts (models already swappable through IoC) 137 | - Add Laravel v5.6 support 138 | - Simplify IoC binding 139 | - Add force option to artisan commands 140 | - Drop Laravel 5.5 support 141 | 142 | ## [v0.0.2] - 2017-09-08 143 | - Fix many issues and apply many enhancements 144 | - Rename package rinvex/laravel-addresses from rinvex/addressable 145 | 146 | ## v0.0.1 - 2017-04-07 147 | - Tag first release 148 | 149 | [v6.1.0]: https://github.com/rinvex/laravel-addresses/compare/v6.0.0...v6.1.0 150 | [v6.0.0]: https://github.com/rinvex/laravel-addresses/compare/v5.0.1...v6.0.0 151 | [v5.0.1]: https://github.com/rinvex/laravel-addresses/compare/v5.0.0...v5.0.1 152 | [v5.0.0]: https://github.com/rinvex/laravel-addresses/compare/v4.1.0...v5.0.0 153 | [v4.1.0]: https://github.com/rinvex/laravel-addresses/compare/v4.0.6...v4.1.0 154 | [v4.0.5]: https://github.com/rinvex/laravel-addresses/compare/v4.0.4...v4.0.5 155 | [v4.0.4]: https://github.com/rinvex/laravel-addresses/compare/v4.0.3...v4.0.4 156 | [v4.0.3]: https://github.com/rinvex/laravel-addresses/compare/v4.0.2...v4.0.3 157 | [v4.0.2]: https://github.com/rinvex/laravel-addresses/compare/v4.0.1...v4.0.2 158 | [v4.0.1]: https://github.com/rinvex/laravel-addresses/compare/v4.0.0...v4.0.1 159 | [v4.0.0]: https://github.com/rinvex/laravel-addresses/compare/v3.0.2...v4.0.0 160 | [v3.0.2]: https://github.com/rinvex/laravel-addresses/compare/v3.0.1...v3.0.2 161 | [v3.0.1]: https://github.com/rinvex/laravel-addresses/compare/v3.0.0...v3.0.1 162 | [v3.0.0]: https://github.com/rinvex/laravel-addresses/compare/v2.1.1...v3.0.0 163 | [v2.1.1]: https://github.com/rinvex/laravel-addresses/compare/v2.1.0...v2.1.1 164 | [v2.1.0]: https://github.com/rinvex/laravel-addresses/compare/v2.0.0...v2.1.0 165 | [v2.0.0]: https://github.com/rinvex/laravel-addresses/compare/v1.0.2...v2.0.0 166 | [v1.0.2]: https://github.com/rinvex/laravel-addresses/compare/v1.0.1...v1.0.2 167 | [v1.0.1]: https://github.com/rinvex/laravel-addresses/compare/v1.0.0...v1.0.1 168 | [v1.0.0]: https://github.com/rinvex/laravel-addresses/compare/v0.0.4...v1.0.0 169 | [v0.0.4]: https://github.com/rinvex/laravel-addresses/compare/v0.0.3...v0.0.4 170 | [v0.0.3]: https://github.com/rinvex/laravel-addresses/compare/v0.0.2...v0.0.3 171 | [v0.0.2]: https://github.com/rinvex/laravel-addresses/compare/v0.0.1...v0.0.2 172 | -------------------------------------------------------------------------------- /CODE_OF_CONDUCT.md: -------------------------------------------------------------------------------- 1 | # Contributor Covenant Code of Conduct 2 | 3 | ## Our Pledge 4 | 5 | In the interest of fostering an open and welcoming environment, we as 6 | contributors and maintainers pledge to making participation in our project and 7 | our community a harassment-free experience for everyone, regardless of age, body 8 | size, disability, ethnicity, gender identity and expression, level of experience, 9 | nationality, personal appearance, race, religion, or sexual identity and 10 | orientation. 11 | 12 | ## Our Standards 13 | 14 | Examples of behavior that contributes to creating a positive environment 15 | include: 16 | 17 | * Using welcoming and inclusive language 18 | * Being respectful of differing viewpoints and experiences 19 | * Gracefully accepting constructive criticism 20 | * Focusing on what is best for the community 21 | * Showing empathy towards other community members 22 | 23 | Examples of unacceptable behavior by participants include: 24 | 25 | * The use of sexualized language or imagery and unwelcome sexual attention or 26 | advances 27 | * Trolling, insulting/derogatory comments, and personal or political attacks 28 | * Public or private harassment 29 | * Publishing others' private information, such as a physical or electronic 30 | address, without explicit permission 31 | * Other conduct which could reasonably be considered inappropriate in a 32 | professional setting 33 | 34 | ## Our Responsibilities 35 | 36 | Project maintainers are responsible for clarifying the standards of acceptable 37 | behavior and are expected to take appropriate and fair corrective action in 38 | response to any instances of unacceptable behavior. 39 | 40 | Project maintainers have the right and responsibility to remove, edit, or 41 | reject comments, commits, code, wiki edits, issues, and other contributions 42 | that are not aligned to this Code of Conduct, or to ban temporarily or 43 | permanently any contributor for other behaviors that they deem inappropriate, 44 | threatening, offensive, or harmful. 45 | 46 | ## Scope 47 | 48 | This Code of Conduct applies both within project spaces and in public spaces 49 | when an individual is representing the project or its community. Examples of 50 | representing a project or community include using an official project e-mail 51 | address, posting via an official social media account, or acting as an appointed 52 | representative at an online or offline event. Representation of a project may be 53 | further defined and clarified by project maintainers. 54 | 55 | ## Enforcement 56 | 57 | Instances of abusive, harassing, or otherwise unacceptable behavior may be 58 | reported by contacting the project team at [help@rinvex.com](mailto:help@rinvex.com). All 59 | complaints will be reviewed and investigated and will result in a response that 60 | is deemed necessary and appropriate to the circumstances. The project team is 61 | obligated to maintain confidentiality with regard to the reporter of an incident. 62 | Further details of specific enforcement policies may be posted separately. 63 | 64 | Project maintainers who do not follow or enforce the Code of Conduct in good 65 | faith may face temporary or permanent repercussions as determined by other 66 | members of the project's leadership. 67 | 68 | ## Attribution 69 | 70 | This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4, 71 | available at [http://contributor-covenant.org/version/1/4][version] 72 | 73 | [homepage]: http://contributor-covenant.org 74 | [version]: http://contributor-covenant.org/version/1/4/ 75 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contribution Guide 2 | 3 | This project adheres to the following standards and practices. 4 | 5 | 6 | ## Versioning 7 | 8 | This project is versioned under the [Semantic Versioning](http://semver.org/) guidelines as much as possible. 9 | 10 | Releases will be numbered with the following format: 11 | 12 | - `..` 13 | - `..` 14 | 15 | And constructed with the following guidelines: 16 | 17 | - Breaking backward compatibility bumps the major and resets the minor and patch. 18 | - New additions without breaking backward compatibility bump the minor and reset the patch. 19 | - Bug fixes and misc changes bump the patch. 20 | 21 | 22 | ## Pull Requests 23 | 24 | The pull request process differs for new features and bugs. 25 | 26 | Pull requests for bugs may be sent without creating any proposal issue. If you believe that you know of a solution for a bug that has been filed, please leave a comment detailing your proposed fix or create a pull request with the fix mentioning that issue id. 27 | 28 | 29 | ## Coding Standards 30 | 31 | This project follows the FIG PHP Standards Recommendations compliant with the [PSR-1: Basic Coding Standard](http://www.php-fig.org/psr/psr-1/), [PSR-2: Coding Style Guide](http://www.php-fig.org/psr/psr-2/) and [PSR-4: Autoloader](http://www.php-fig.org/psr/psr-4/) to ensure a high level of interoperability between shared PHP code. If you notice any compliance oversights, please send a patch via pull request. 32 | 33 | 34 | ## Feature Requests 35 | 36 | If you have a proposal or a feature request, you may create an issue with `[Proposal]` in the title. 37 | 38 | The proposal should also describe the new feature, as well as implementation ideas. The proposal will then be reviewed and either approved or denied. Once a proposal is approved, a pull request may be created implementing the new feature. 39 | 40 | 41 | ## Git Flow 42 | 43 | This project follows [Git-Flow](http://nvie.com/posts/a-successful-git-branching-model/), and as such has `master` (latest stable releases), `develop` (latest WIP development) and X.Y support branches (when there's multiple major versions). 44 | 45 | Accordingly all pull requests MUST be sent to the `develop` branch. 46 | 47 | > **Note:** Pull requests which do not follow these guidelines will be closed without any further notice. 48 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016-2021, Rinvex LLC, 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Rinvex Addresses 2 | 3 | ⚠️ This package is abandoned and no longer maintained. No replacement package was suggested. ⚠️ 4 | 5 | 👉 If you are interested to step on as the main maintainer of this package, please [reach out to me](https://twitter.com/omranic)! 6 | 7 | --- 8 | 9 | **Rinvex Addresses** is a polymorphic Laravel package, for addressbook management. You can add addresses to any eloquent model with ease. 10 | 11 | [![Packagist](https://img.shields.io/packagist/v/rinvex/laravel-addresses.svg?label=Packagist&style=flat-square)](https://packagist.org/packages/rinvex/laravel-addresses) 12 | [![Scrutinizer Code Quality](https://img.shields.io/scrutinizer/g/rinvex/laravel-addresses.svg?label=Scrutinizer&style=flat-square)](https://scrutinizer-ci.com/g/rinvex/laravel-addresses/) 13 | [![Travis](https://img.shields.io/travis/rinvex/laravel-addresses.svg?label=TravisCI&style=flat-square)](https://travis-ci.org/rinvex/laravel-addresses) 14 | [![StyleCI](https://styleci.io/repos/87485079/shield)](https://styleci.io/repos/87485079) 15 | [![License](https://img.shields.io/packagist/l/rinvex/laravel-addresses.svg?label=License&style=flat-square)](https://github.com/rinvex/laravel-addresses/blob/develop/LICENSE) 16 | 17 | 18 | ## Installation 19 | 20 | 1. Install the package via composer: 21 | ```shell 22 | composer require rinvex/laravel-addresses 23 | ``` 24 | 25 | 2. Publish resources (migrations and config files): 26 | ```shell 27 | php artisan rinvex:publish:addresses 28 | ``` 29 | 30 | 3. Execute migrations via the following command: 31 | ```shell 32 | php artisan rinvex:migrate:addresses 33 | ``` 34 | 35 | 4. Done! 36 | 37 | 38 | ## Usage 39 | 40 | To add addresses support to your eloquent models simply use `\Rinvex\Addresses\Traits\Addressable` trait. 41 | 42 | ### Manage your addresses 43 | 44 | ```php 45 | // Get instance of your model 46 | $user = new \App\Models\User::find(1); 47 | 48 | // Create a new address 49 | $user->addresses()->create([ 50 | 'label' => 'Default Address', 51 | 'given_name' => 'Abdelrahman', 52 | 'family_name' => 'Omran', 53 | 'organization' => 'Rinvex', 54 | 'country_code' => 'eg', 55 | 'street' => '56 john doe st.', 56 | 'state' => 'Alexandria', 57 | 'city' => 'Alexandria', 58 | 'postal_code' => '21614', 59 | 'latitude' => '31.2467601', 60 | 'longitude' => '29.9020376', 61 | 'is_primary' => true, 62 | 'is_billing' => true, 63 | 'is_shipping' => true, 64 | ]); 65 | 66 | // Create multiple new addresses 67 | $user->addresses()->createMany([ 68 | [...], 69 | [...], 70 | [...], 71 | ]); 72 | 73 | // Find an existing address 74 | $address = app('rinvex.addresses.address')->find(1); 75 | 76 | // Update an existing address 77 | $address->update([ 78 | 'label' => 'Default Work Address', 79 | ]); 80 | 81 | // Delete address 82 | $address->delete(); 83 | 84 | // Alternative way of address deletion 85 | $user->addresses()->where('id', 123)->first()->delete(); 86 | ``` 87 | 88 | ### Manage your addressable model 89 | 90 | The API is intuitive and very straight forward, so let's give it a quick look: 91 | 92 | ```php 93 | // Get instance of your model 94 | $user = new \App\Models\User::find(1); 95 | 96 | // Get attached addresses collection 97 | $user->addresses; 98 | 99 | // Get attached addresses query builder 100 | $user->addresses(); 101 | 102 | // Scope Primary Addresses 103 | $primaryAddresses = app('rinvex.addresses.address')->isPrimary()->get(); 104 | 105 | // Scope Billing Addresses 106 | $billingAddresses = app('rinvex.addresses.address')->isBilling()->get(); 107 | 108 | // Scope Shipping Addresses 109 | $shippingAddresses = app('rinvex.addresses.address')->isShipping()->get(); 110 | 111 | // Scope Addresses in the given country 112 | $egyptianAddresses = app('rinvex.addresses.address')->InCountry('eg')->get(); 113 | 114 | // Find all users within 5 kilometers radius from the latitude/longitude 31.2467601/29.9020376 115 | $fiveKmAddresses = \App\Models\User::findByDistance(5, 'kilometers', '31.2467601', '29.9020376')->get(); 116 | 117 | // Alternative method to find users within certain radius 118 | $user = new \App\Models\User(); 119 | $users = $user->lat('31.2467601')->lng('29.9020376')->within(5, 'kilometers')->get(); 120 | ``` 121 | 122 | 123 | ## Changelog 124 | 125 | Refer to the [Changelog](CHANGELOG.md) for a full history of the project. 126 | 127 | 128 | ## Support 129 | 130 | The following support channels are available at your fingertips: 131 | 132 | - [Chat on Slack](https://bit.ly/rinvex-slack) 133 | - [Help on Email](mailto:help@rinvex.com) 134 | - [Follow on Twitter](https://twitter.com/rinvex) 135 | 136 | 137 | ## Contributing & Protocols 138 | 139 | Thank you for considering contributing to this project! The contribution guide can be found in [CONTRIBUTING.md](CONTRIBUTING.md). 140 | 141 | Bug reports, feature requests, and pull requests are very welcome. 142 | 143 | - [Versioning](CONTRIBUTING.md#versioning) 144 | - [Pull Requests](CONTRIBUTING.md#pull-requests) 145 | - [Coding Standards](CONTRIBUTING.md#coding-standards) 146 | - [Feature Requests](CONTRIBUTING.md#feature-requests) 147 | - [Git Flow](CONTRIBUTING.md#git-flow) 148 | 149 | 150 | ## Security Vulnerabilities 151 | 152 | If you discover a security vulnerability within this project, please send an e-mail to [help@rinvex.com](help@rinvex.com). All security vulnerabilities will be promptly addressed. 153 | 154 | 155 | ## About Rinvex 156 | 157 | Rinvex is a software solutions startup, specialized in integrated enterprise solutions for SMEs established in Alexandria, Egypt since June 2016. We believe that our drive The Value, The Reach, and The Impact is what differentiates us and unleash the endless possibilities of our philosophy through the power of software. We like to call it Innovation At The Speed Of Life. That’s how we do our share of advancing humanity. 158 | 159 | 160 | ## License 161 | 162 | This software is released under [The MIT License (MIT)](LICENSE). 163 | 164 | (c) 2016-2022 Rinvex LLC, Some rights reserved. 165 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "rinvex/laravel-addresses", 3 | "description": "Rinvex Addresses is a polymorphic Laravel package, for addressbook management. You can add addresses to any eloquent model with ease.", 4 | "type": "library", 5 | "keywords": [ 6 | "city", 7 | "state", 8 | "model", 9 | "postal", 10 | "rinvex", 11 | "street", 12 | "address", 13 | "laravel", 14 | "country", 15 | "primary", 16 | "billing", 17 | "shipping", 18 | "eloquent", 19 | "latitude", 20 | "longitude", 21 | "addressable", 22 | "polymorphic" 23 | ], 24 | "license": "MIT", 25 | "homepage": "https://rinvex.com", 26 | "support": { 27 | "email": "help@rinvex.com", 28 | "issues": "https://github.com/rinvex/laravel-addresses/issues", 29 | "source": "https://github.com/rinvex/laravel-addresses", 30 | "docs": "https://github.com/rinvex/laravel-addresses/blob/master/README.md" 31 | }, 32 | "authors": [ 33 | { 34 | "name": "Rinvex LLC", 35 | "homepage": "https://rinvex.com", 36 | "email": "help@rinvex.com" 37 | }, 38 | { 39 | "name": "Abdelrahman Omran", 40 | "homepage": "https://omranic.com", 41 | "email": "me@omranic.com", 42 | "role": "Project Lead" 43 | }, 44 | { 45 | "name": "The Generous Laravel Community", 46 | "homepage": "https://github.com/rinvex/laravel-addresses/contributors" 47 | } 48 | ], 49 | "require": { 50 | "php": "^8.0.0", 51 | "illuminate/console": "^9.0.0 || ^10.0.0", 52 | "illuminate/database": "^9.0.0 || ^10.0.0", 53 | "illuminate/support": "^9.0.0 || ^10.0.0", 54 | "jackpopp/geodistance": "^1.2.0", 55 | "rinvex/countries": "^8.0.0", 56 | "rinvex/laravel-support": "^6.0.0" 57 | }, 58 | "require-dev": { 59 | "codedungeon/phpunit-result-printer": "^0.31.0", 60 | "illuminate/container": "^9.0.0 || ^10.0.0", 61 | "phpunit/phpunit": "^9.5.0" 62 | }, 63 | "autoload": { 64 | "psr-4": { 65 | "Rinvex\\Addresses\\": "src/" 66 | } 67 | }, 68 | "autoload-dev": { 69 | "psr-4": { 70 | "Rinvex\\Addresses\\Tests\\": "tests" 71 | } 72 | }, 73 | "scripts": { 74 | "test": "vendor/bin/phpunit" 75 | }, 76 | "config": { 77 | "sort-packages": true, 78 | "preferred-install": "dist", 79 | "optimize-autoloader": true 80 | }, 81 | "extra": { 82 | "laravel": { 83 | "providers": [ 84 | "Rinvex\\Addresses\\Providers\\AddressesServiceProvider" 85 | ] 86 | } 87 | }, 88 | "minimum-stability": "dev", 89 | "prefer-stable": true 90 | } 91 | -------------------------------------------------------------------------------- /config/config.php: -------------------------------------------------------------------------------- 1 | true, 9 | 10 | // Addresses Database Tables 11 | 'tables' => [ 12 | 'addresses' => 'addresses', 13 | ], 14 | 15 | // Addresses Models 16 | 'models' => [ 17 | 'address' => \Rinvex\Addresses\Models\Address::class, 18 | ], 19 | 20 | // Addresses Geocoding Options 21 | 'geocoding' => [ 22 | 'enabled' => false, 23 | 'api_key' => env('GOOGLE_APP_KEY'), 24 | ], 25 | 26 | ]; 27 | -------------------------------------------------------------------------------- /database/migrations/2020_01_01_000001_create_addresses_table.php: -------------------------------------------------------------------------------- 1 | increments('id'); 16 | $table->morphs('addressable'); 17 | $table->string('given_name'); 18 | $table->string('family_name'); 19 | $table->string('label')->nullable(); 20 | $table->string('organization')->nullable(); 21 | $table->string('country_code', 2)->nullable(); 22 | $table->string('street')->nullable(); 23 | $table->string('state')->nullable(); 24 | $table->string('city')->nullable(); 25 | $table->string('postal_code')->nullable(); 26 | $table->decimal('latitude', 10, 7)->nullable(); 27 | $table->decimal('longitude', 10, 7)->nullable(); 28 | $table->boolean('is_primary')->default(false); 29 | $table->boolean('is_billing')->default(false); 30 | $table->boolean('is_shipping')->default(false); 31 | $table->timestamps(); 32 | $table->softDeletes(); 33 | }); 34 | } 35 | 36 | public function down() 37 | { 38 | Schema::dropIfExists(config('rinvex.addresses.tables.addresses')); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /database/migrations/2020_01_01_000002_alter_addresses_table_make_family_name_nullable.php: -------------------------------------------------------------------------------- 1 | string('family_name')->nullable()->change(); 15 | }); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /phpstan.neon.dist: -------------------------------------------------------------------------------- 1 | includes: 2 | - ./vendor/nunomaduro/larastan/extension.neon 3 | parameters: 4 | level: 5 5 | paths: 6 | - src 7 | -------------------------------------------------------------------------------- /src/Console/Commands/MigrateCommand.php: -------------------------------------------------------------------------------- 1 | alert($this->description); 33 | 34 | $path = config('rinvex.addresses.autoload_migrations') ? 35 | 'vendor/rinvex/laravel-addresses/database/migrations' : 36 | 'database/migrations/rinvex/laravel-addresses'; 37 | 38 | if (file_exists($path)) { 39 | $this->call('migrate', [ 40 | '--step' => true, 41 | '--path' => $path, 42 | '--force' => $this->option('force'), 43 | ]); 44 | } else { 45 | $this->warn('No migrations found! Consider publish them first: php artisan rinvex:publish:addresses'); 46 | } 47 | 48 | $this->line(''); 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /src/Console/Commands/PublishCommand.php: -------------------------------------------------------------------------------- 1 | alert($this->description); 33 | 34 | collect($this->option('resource') ?: ['config', 'migrations'])->each(function ($resource) { 35 | $this->call('vendor:publish', ['--tag' => "rinvex/addresses::{$resource}", '--force' => $this->option('force')]); 36 | }); 37 | 38 | $this->line(''); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /src/Console/Commands/RollbackCommand.php: -------------------------------------------------------------------------------- 1 | alert($this->description); 33 | 34 | $path = config('rinvex.addresses.autoload_migrations') ? 35 | 'vendor/rinvex/laravel-addresses/database/migrations' : 36 | 'database/migrations/rinvex/laravel-addresses'; 37 | 38 | if (file_exists($path)) { 39 | $this->call('migrate:reset', [ 40 | '--path' => $path, 41 | '--force' => $this->option('force'), 42 | ]); 43 | } else { 44 | $this->warn('No migrations found! Consider publish them first: php artisan rinvex:publish:addresses'); 45 | } 46 | 47 | $this->line(''); 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /src/Models/Address.php: -------------------------------------------------------------------------------- 1 | 'integer', 106 | 'addressable_type' => 'string', 107 | 'label' => 'string', 108 | 'given_name' => 'string', 109 | 'family_name' => 'string', 110 | 'organization' => 'string', 111 | 'country_code' => 'string', 112 | 'street' => 'string', 113 | 'state' => 'string', 114 | 'city' => 'string', 115 | 'postal_code' => 'string', 116 | 'latitude' => 'float', 117 | 'longitude' => 'float', 118 | 'is_primary' => 'boolean', 119 | 'is_billing' => 'boolean', 120 | 'is_shipping' => 'boolean', 121 | 'deleted_at' => 'datetime', 122 | ]; 123 | 124 | /** 125 | * {@inheritdoc} 126 | */ 127 | protected $observables = [ 128 | 'validating', 129 | 'validated', 130 | ]; 131 | 132 | /** 133 | * The default rules that the model will validate against. 134 | * 135 | * @var array 136 | */ 137 | protected $rules = []; 138 | 139 | /** 140 | * Whether the model should throw a 141 | * ValidationException if it fails validation. 142 | * 143 | * @var bool 144 | */ 145 | protected $throwValidationExceptions = true; 146 | 147 | /** 148 | * Create a new Eloquent model instance. 149 | * 150 | * @param array $attributes 151 | */ 152 | public function __construct(array $attributes = []) 153 | { 154 | $this->setTable(config('rinvex.addresses.tables.addresses')); 155 | $this->mergeRules([ 156 | 'addressable_id' => 'required|integer', 157 | 'addressable_type' => 'required|string|strip_tags|max:150', 158 | 'label' => 'nullable|string|strip_tags|max:150', 159 | 'given_name' => 'required|string|strip_tags|max:150', 160 | 'family_name' => 'nullable|string|strip_tags|max:150', 161 | 'organization' => 'nullable|string|strip_tags|max:150', 162 | 'country_code' => 'nullable|alpha|size:2|country', 163 | 'street' => 'nullable|string|strip_tags|max:150', 164 | 'state' => 'nullable|string|strip_tags|max:150', 165 | 'city' => 'nullable|string|strip_tags|max:150', 166 | 'postal_code' => 'nullable|string|strip_tags|max:150', 167 | 'latitude' => 'nullable|numeric', 168 | 'longitude' => 'nullable|numeric', 169 | 'is_primary' => 'sometimes|boolean', 170 | 'is_billing' => 'sometimes|boolean', 171 | 'is_shipping' => 'sometimes|boolean', 172 | ]); 173 | 174 | parent::__construct($attributes); 175 | } 176 | 177 | /** 178 | * Get the owner model of the address. 179 | * 180 | * @return \Illuminate\Database\Eloquent\Relations\MorphTo 181 | */ 182 | public function addressable(): MorphTo 183 | { 184 | return $this->morphTo('addressable', 'addressable_type', 'addressable_id', 'id'); 185 | } 186 | 187 | /** 188 | * Scope primary addresses. 189 | * 190 | * @param \Illuminate\Database\Eloquent\Builder $builder 191 | * 192 | * @return \Illuminate\Database\Eloquent\Builder 193 | */ 194 | public function scopeIsPrimary(Builder $builder): Builder 195 | { 196 | return $builder->where('is_primary', true); 197 | } 198 | 199 | /** 200 | * Scope billing addresses. 201 | * 202 | * @param \Illuminate\Database\Eloquent\Builder $builder 203 | * 204 | * @return \Illuminate\Database\Eloquent\Builder 205 | */ 206 | public function scopeIsBilling(Builder $builder): Builder 207 | { 208 | return $builder->where('is_billing', true); 209 | } 210 | 211 | /** 212 | * Scope shipping addresses. 213 | * 214 | * @param \Illuminate\Database\Eloquent\Builder $builder 215 | * 216 | * @return \Illuminate\Database\Eloquent\Builder 217 | */ 218 | public function scopeIsShipping(Builder $builder): Builder 219 | { 220 | return $builder->where('is_shipping', true); 221 | } 222 | 223 | /** 224 | * Scope addresses by the given country. 225 | * 226 | * @param \Illuminate\Database\Eloquent\Builder $builder 227 | * @param string $countryCode 228 | * 229 | * @return \Illuminate\Database\Eloquent\Builder 230 | */ 231 | public function scopeInCountry(Builder $builder, string $countryCode): Builder 232 | { 233 | return $builder->where('country_code', $countryCode); 234 | } 235 | 236 | /** 237 | * Scope addresses by the given language. 238 | * 239 | * @param \Illuminate\Database\Eloquent\Builder $builder 240 | * @param string $languageCode 241 | * 242 | * @return \Illuminate\Database\Eloquent\Builder 243 | */ 244 | public function scopeInLanguage(Builder $builder, string $languageCode): Builder 245 | { 246 | return $builder->where('language_code', $languageCode); 247 | } 248 | 249 | /** 250 | * Get full name attribute. 251 | * 252 | * @return string 253 | */ 254 | public function getFullNameAttribute(): string 255 | { 256 | return implode(' ', [$this->given_name, $this->family_name]); 257 | } 258 | 259 | /** 260 | * {@inheritdoc} 261 | */ 262 | protected static function boot() 263 | { 264 | parent::boot(); 265 | 266 | static::saving(function (self $address) { 267 | $geocoding = config('rinvex.addresses.geocoding.enabled'); 268 | $geocoding_api_key = config('rinvex.addresses.geocoding.api_key'); 269 | if ($geocoding && $geocoding_api_key) { 270 | $segments[] = $address->street; 271 | $segments[] = sprintf('%s, %s %s', $address->city, $address->state, $address->postal_code); 272 | $segments[] = country($address->country_code)->getName(); 273 | 274 | $query = str_replace(' ', '+', implode(', ', $segments)); 275 | $geocode = json_decode(file_get_contents( 276 | "https://maps.google.com/maps/api/geocode/json?address={$query}&sensor=false&key={$geocoding_api_key}" 277 | )); 278 | 279 | if (count($geocode->results)) { 280 | $address->latitude = $geocode->results[0]->geometry->location->lat; 281 | $address->longitude = $geocode->results[0]->geometry->location->lng; 282 | } 283 | } 284 | }); 285 | } 286 | } 287 | -------------------------------------------------------------------------------- /src/Providers/AddressesServiceProvider.php: -------------------------------------------------------------------------------- 1 | 'command.rinvex.addresses.migrate', 25 | PublishCommand::class => 'command.rinvex.addresses.publish', 26 | RollbackCommand::class => 'command.rinvex.addresses.rollback', 27 | ]; 28 | 29 | /** 30 | * {@inheritdoc} 31 | */ 32 | public function register() 33 | { 34 | // Merge config 35 | $this->mergeConfigFrom(realpath(__DIR__.'/../../config/config.php'), 'rinvex.addresses'); 36 | 37 | // Bind eloquent models to IoC container 38 | $this->registerModels([ 39 | 'rinvex.addresses.address' => Address::class, 40 | ]); 41 | 42 | // Register console commands 43 | $this->registerCommands($this->commands); 44 | } 45 | 46 | /** 47 | * {@inheritdoc} 48 | */ 49 | public function boot() 50 | { 51 | // Publish Resources 52 | $this->publishesConfig('rinvex/laravel-addresses'); 53 | $this->publishesMigrations('rinvex/laravel-addresses'); 54 | ! $this->autoloadMigrations('rinvex/laravel-addresses') || $this->loadMigrationsFrom(__DIR__.'/../../database/migrations'); 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /src/Traits/Addressable.php: -------------------------------------------------------------------------------- 1 | addresses()->delete(); 43 | }); 44 | } 45 | 46 | /** 47 | * Get all attached addresses to the model. 48 | * 49 | * @return \Illuminate\Database\Eloquent\Relations\MorphMany 50 | */ 51 | public function addresses(): MorphMany 52 | { 53 | return $this->morphMany(config('rinvex.addresses.models.address'), 'addressable', 'addressable_type', 'addressable_id'); 54 | } 55 | 56 | /** 57 | * Find addressables by distance. 58 | * 59 | * @param string $distance 60 | * @param string $unit 61 | * @param string $latitude 62 | * @param string $longitude 63 | * 64 | * @return \Illuminate\Support\Collection 65 | */ 66 | public function findByDistance($distance, $unit, $latitude, $longitude): Collection 67 | { 68 | // @TODO: this method needs to be refactored! 69 | return $this->addresses()->within($distance, $unit, $latitude, $longitude)->get(); 70 | 71 | $results = []; 72 | foreach ($records as $record) { 73 | $results[] = $record->addressable; 74 | } 75 | 76 | return collect($results); 77 | } 78 | } 79 | --------------------------------------------------------------------------------