Skip to content

Commit

Permalink
Merge pull request #1 from stylers-llc/feature/parcel-number
Browse files Browse the repository at this point in the history
Add columns: country_code, floor ,door, latitude, longitude, parcel_number, description
  • Loading branch information
Stylers Developer authored Feb 21, 2020
2 parents 247ed75 + c337c79 commit b11f393
Show file tree
Hide file tree
Showing 13 changed files with 173 additions and 101 deletions.
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ php:

before_install:
- travis_retry composer self-update
- echo "memory_limit=2G" >> ~/.phpenv/versions/$(phpenv version-name)/etc/conf.d/travis.ini

install:
- travis_retry composer install --no-interaction --prefer-dist
- COMPOSER_MEMORY_LIMIT=-1 travis_retry composer install --no-interaction --prefer-dist

script:
- vendor/bin/phpunit --verbose --coverage-clover=coverage.xml
Expand Down
35 changes: 29 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ $attributes = [
"name_of_public_place" => "Kossuth Lajos",
"type_of_public_place" => "place",
"number_of_house" => "1-3",
"floor" => "42",
"door" => "69",
"latitude" => "47.5070738",
"longitude" => "19.045599",
"parcel_number" => "10086/0/A/3",
]; // array
$type = AddressTypeEnum::PRIMARY; // ?string
$address = $user->updateOrCreateAddress($attributes, $type); // AddressInterface
Expand All @@ -74,13 +79,31 @@ use Stylers\Address\Enums\AddressTypeEnum;
$user = User::first();
$arrayOfAttributes = [
AddressTypeEnum::MAILING => [
"country" => "Hungary",
"zip_code" => "1055",
"city" => "Budapest",
"name_of_public_place" => "Kossuth Lajos",
"type_of_public_place" => "place",
"number_of_house" => "1-3",
"country" => "Hungary",
"zip_code" => "1055",
"city" => "Budapest",
"name_of_public_place" => "Kossuth Lajos",
"type_of_public_place" => "place",
"number_of_house" => "1-3",
"floor" => "42",
"door" => "69",
"latitude" => "47.5070738",
"longitude" => "19.045599",
"parcel_number" => "10086/0/A/3",
]
];
$addresses = $user->syncAddresses($arrayOfAttributes); // Collection
```

## How to Test
```bash
$ docker run -it --rm -v $PWD:/app -w /app epcallan/php7-testing-phpunit:7.2-phpunit7 bash
$ composer install
$ ./vendor/bin/phpunit
```

### Troubleshooting
```bash
# Fatal error: Allowed memory size of...
$ COMPOSER_MEMORY_LIMIT=-1 composer install
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class AddColumnsToAddressesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('addresses', function (Blueprint $table) {
$table->string('country_code', 2)->nullable()->after('country');
$table->string('floor')->nullable()->after('number_of_house');
$table->string('door')->nullable()->after('floor');
$table->float('latitude')->nullable()->after('door');
$table->float('longitude')->nullable()->after('latitude');
$table->string('parcel_number')->nullable()->after('longitude');
$table->string('description')->nullable()->after('parcel_number');
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('addresses', function (Blueprint $table) {
$table->dropColumn('country_code');
$table->dropColumn('floor');
$table->dropColumn('door');
$table->dropColumn('latitude');
$table->dropColumn('longitude');
$table->dropColumn('parcel_number');
$table->dropColumn('description');
});
}
}
8 changes: 0 additions & 8 deletions src/Contracts/Models/AddressInterface.php
Original file line number Diff line number Diff line change
@@ -1,18 +1,10 @@
<?php


namespace Stylers\Address\Contracts\Models;

use Illuminate\Database\Eloquent\Relations\MorphTo;

/**
* Interface AddressInterface
* @package Stylers\Address\Contracts\Models
*/
interface AddressInterface
{
/**
* @return MorphTo
*/
public function addressable(): MorphTo;
}
25 changes: 0 additions & 25 deletions src/Contracts/Models/Traits/HasAddressesInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,40 +7,15 @@
use Illuminate\Database\Eloquent\Relations\MorphMany;
use Illuminate\Support\Collection;

/**
* Interface HasAddressesInterface
* @package Stylers\Address\Contracts\Models\Traits
*/
interface HasAddressesInterface
{
/**
* @return MorphMany
*/
public function addresses(): MorphMany;

/**
* @param null|string $type
* @return bool
*/
public function hasAddress(string $type = null): bool;

/**
* @param array $attributes
* @param null|string $type
* @return AddressInterface
*/
public function updateOrCreateAddress(array $attributes, string $type = null): AddressInterface;

/**
* @param null|string $type
* @return bool
* @throws \Exception|ModelNotFoundException
*/
public function deleteAddress(string $type = null): bool;

/**
* @param array $arrayOfAttributes
* @return Collection
*/
public function syncAddresses(array $arrayOfAttributes): Collection;
}
8 changes: 0 additions & 8 deletions src/Enums/AbstractEnum.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,8 @@
use Stylers\Address\Contracts\Enums\EnumInterface;
use \ReflectionClass;

/**
* Class AbstractEnum
* @package Stylers\Address\Enums
*/
abstract class AbstractEnum implements EnumInterface
{
/**
* @return array
* @throws \ReflectionException
*/
public static function getConstants(): array
{
$class = get_called_class();
Expand Down
4 changes: 0 additions & 4 deletions src/Enums/AddressTypeEnum.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@

namespace Stylers\Address\Enums;

/**
* Class AddressTypeEnum
* @package Stylers\Address\Enums
*/
abstract class AddressTypeEnum extends AbstractEnum
{
const PRIMARY = "primary";
Expand Down
19 changes: 7 additions & 12 deletions src/Models/Address.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,25 @@
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\MorphTo;

/**
* Class Address
* @package Stylers\Address\Models
*/
class Address extends Model implements AddressInterface
{
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'country',
'country_code',
'zip_code',
'city',
'name_of_public_place',
'type_of_public_place',
'number_of_house',
'floor',
'door',
'latitude',
'longitude',
'parcel_number',
'description',
'type',
];

/**
* @return MorphTo
*/
public function addressable(): MorphTo
{
return $this->morphTo();
Expand Down
24 changes: 1 addition & 23 deletions src/Models/Traits/HasAddresses.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,50 +10,28 @@

trait HasAddresses
{
/**
* @return MorphMany
*/
public function addresses(): MorphMany
{
return $this->morphMany(app(AddressInterface::class), 'addressable');
}

/**
* @param string|null $type
* @return bool
*/
public function hasAddress(string $type = null): bool
{
return (bool)$this->addresses()->where('type', $type)->first();
}


/**
* @param array $attributes
* @param string|null $type
* @return AddressInterface|Model
*/
public function updateOrCreateAddress(array $attributes, string $type = null): AddressInterface
{
$attributes['type'] = $type;

return $this->addresses()->updateOrCreate(['type' => $type], $attributes);
}


/**
* @param string|null $type
* @return bool
* @throws \Exception|ModelNotFoundException
*/
public function deleteAddress(string $type = null): bool
{
return $this->addresses()->where('type', $type)->firstOrFail()->delete();
}

/**
* @param array $arrayOfAttributes
* @return Collection
*/
public function syncAddresses(array $arrayOfAttributes): Collection
{
$collection = new Collection();
Expand Down
14 changes: 0 additions & 14 deletions src/Providers/AddressServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,15 @@
use Stylers\Address\Contracts\Models\AddressInterface;
use Stylers\Address\Models\Address;

/**
* Class AddressServiceProvider
* @package Stylers\Address\Providers
*/
class AddressServiceProvider extends ServiceProvider
{
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
$this->publishes([
__DIR__ . '/../../database/migrations' => database_path('migrations'),
], 'migrations');
}

/**
* Register any application services.
*
* @return void
*/
public function register()
{
$this->app->bind(AddressInterface::class, Address::class);
Expand Down
7 changes: 7 additions & 0 deletions tests/fixtures/database/factories/AddressFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,17 @@
return [
'type' => array_random(AddressTypeEnum::getConstants()),
'country' => $faker->country,
'country_code' => $faker->countryCode,
'zip_code' => $faker->postcode,
'city' => $faker->city,
'name_of_public_place' => $faker->streetName,
'type_of_public_place' => 'street',
'number_of_house' => $faker->randomNumber(),
'floor' => $faker->randomNumber(),
'door' => $faker->randomNumber(),
"latitude" => $faker->latitude,
"longitude" => $faker->longitude,
"parcel_number" => "10086/0/A/3",
"description" => $faker->text,
];
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateAddressesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('addresses', function (Blueprint $table) {
$table->increments('id');
$table->morphs('addressable');
$table->string('country')->nullable();
$table->string('zip_code')->nullable();
$table->string('city')->nullable();
$table->string('name_of_public_place')->nullable(); // example: "Kossuth Lajos"
$table->string('type_of_public_place')->nullable(); // example: "street" <or> "place"
$table->string('number_of_house')->nullable(); // example: "1-3"
$table->string('type')->nullable(); // AddressTypeEnum
$table->timestamps();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('addresses');
}
}
Loading

0 comments on commit b11f393

Please sign in to comment.