-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Patrick Breen
committed
Mar 10, 2021
1 parent
6154a69
commit bf80acd
Showing
11 changed files
with
475 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tipoff\Addresses\Database\Factories; | ||
|
||
use Illuminate\Database\Eloquent\Factories\Factory; | ||
use Tipoff\Addresses\Models\Address; | ||
use Tipoff\Addresses\Models\DomesticAddress; | ||
use Tipoff\Authorization\Models\User; | ||
|
||
class AddressFactory extends Factory | ||
{ | ||
protected $model = Address::class; | ||
|
||
public function definition() | ||
{ | ||
$user = User::factory()->create(); | ||
|
||
return [ | ||
'domestic_address_id' => randomOrCreate(DomesticAddress::class), | ||
'addressable_type' => get_class($user), | ||
'addressable_id' => $user->id, | ||
'type' => $this->faker->randomElement(['shipping', 'billing']), | ||
'creator_id' => randomOrCreate(app('user')), | ||
'updater_id' => randomOrCreate(app('user')), | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tipoff\Addresses\Traits; | ||
|
||
|
||
use Illuminate\Support\Collection; | ||
use Tipoff\Addresses\Models\Address; | ||
use Tipoff\Addresses\Models\City; | ||
use Tipoff\Addresses\Models\DomesticAddress; | ||
use Tipoff\Addresses\Models\Zip; | ||
|
||
/** | ||
* @property Collection addresses | ||
*/ | ||
trait HasAddresses | ||
{ | ||
/** | ||
* @param string $line1 | ||
* @param string|null $line2 | ||
* @param string|City $city | ||
* @param string|Zip $zip | ||
* @return DomesticAddress | ||
*/ | ||
public static function createDomesticAddress(string $line1, ?string $line2, $city, $zip): DomesticAddress | ||
{ | ||
return DomesticAddress::createDomesticAddress($line1, $line2, $city, $zip); | ||
} | ||
|
||
public function getAddressByType(string $type): ?Address | ||
{ | ||
return $this->addresses | ||
->filter(function (Address $address) use ($type) { | ||
return $address->type === $type; | ||
}) | ||
->first(); | ||
} | ||
|
||
public function setAddressByType(string $type, DomesticAddress $domesticAddress): Address | ||
{ | ||
$address = $this->getAddressByType($type); | ||
if (!$address) { | ||
$address = new Address(); | ||
$address->type = $type; | ||
$address->addressable()->associate($this); | ||
} | ||
|
||
$address->domesticAddress()->associate($domesticAddress)->save(); | ||
$address->load('domesticAddress'); | ||
$this->load('addresses'); | ||
|
||
return $address; | ||
} | ||
|
||
public function getAddress(): ?Address | ||
{ | ||
return $this->getAddressByType(get_class($this)); | ||
} | ||
|
||
public function setAddress(DomesticAddress $domesticAddress): Address | ||
{ | ||
return $this->setAddressByType(get_class($this), $domesticAddress); | ||
} | ||
|
||
public function addresses() | ||
{ | ||
return $this->morphMany(Address::class, 'addressable'); | ||
} | ||
} |
Oops, something went wrong.