Skip to content

Commit

Permalink
Added proxy getters (->address) to the built-in Billpayer class
Browse files Browse the repository at this point in the history
  • Loading branch information
fulopattila122 committed Jun 2, 2024
1 parent 422165b commit 5d72bc5
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 1 deletion.
12 changes: 12 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,18 @@

## 4.x Series

## Unreleased
##### 2024-XX-YY

- Added the follwing getters to the default Billpayer model (proxies down to the underlying address):
- `country_id`
- `province_id`
- `postalcode`
- `city`
- `street_address` (fetches $billpayer->address->address) - can't use `address` since that collides with the address() relation
- `address2`
- `access_code`

## 4.0.0
##### 2024-04-25

Expand Down
49 changes: 48 additions & 1 deletion Models/Billpayer.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

namespace Vanilo\Order\Models;

use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Model;
use Konekt\Address\Models\AddressProxy;
use Vanilo\Contracts\Address;
Expand All @@ -25,9 +26,19 @@
* work temporarily as of v0.1. Probably will be moved to the
* billing module or another module, if it survives at all
*/

/**
* @property-read string $country_id
* @property-read string|null $province_id
* @property-read string|null $postalcode
* @property-read string $city
* @property-read string $street_address
* @property-read string|null $address2
* @property-read string|null $access_code
*/
class Billpayer extends Model implements BillpayerContract, VaniloBillpayerContract
{
protected $guarded = ['id', 'address_id'];
protected $guarded = ['id', 'created_at', 'updated_at'];

public function isEuRegistered(): bool
{
Expand Down Expand Up @@ -97,4 +108,40 @@ public function getFullName(): string
{
return $this->getFirstName() . ' ' . $this->getLastName();
}

protected function countryId(): Attribute
{
return Attribute::get(fn ($value) => $this->getBillingAddress()->getCountryCode());
}

protected function provinceId(): Attribute
{
return Attribute::get(fn ($value) => $this->getBillingAddress()->province_id);
}

protected function postalcode(): Attribute
{
return Attribute::get(fn ($value) => $this->getBillingAddress()->getPostalCode());
}

protected function city(): Attribute
{
return Attribute::get(fn ($value) => $this->getBillingAddress()->getCity());
}

/** @todo this would collide with the address() thus the `address` -> `street_address` workaround */
protected function streetAddress(): Attribute
{
return Attribute::get(fn ($value) => $this->getBillingAddress()->getAddress());
}

protected function address2(): Attribute
{
return Attribute::get(fn ($value) => $this->getBillingAddress()->getAddress2());
}

protected function accessCode(): Attribute
{
return Attribute::get(fn ($value) => $this->getBillingAddress()->access_code);
}
}
28 changes: 28 additions & 0 deletions Tests/BillpayerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
namespace Vanilo\Order\Tests;

use Vanilo\Order\Models\Billpayer;
use Vanilo\Order\Tests\Dummies\Address;

class BillpayerTest extends TestCase
{
Expand All @@ -27,4 +28,31 @@ public function it_returns_proper_bool_types_when_the_model_is_empty()
$this->assertIsBool($billPayer->isEuRegistered());
$this->assertIsBool($billPayer->isIndividual());
}

/** @test */
public function it_proxies_its_missing_address_fields_to_its_underlying_address_object()
{
$address = Address::create([
'name' => 'Sample Name',
'country_id' => 'NL',
'province_id' => '1',
'postalcode' => '1234 AB',
'city' => 'Buiswijk',
'address' => 'Line 1',
'address2' => 'Line 2',
'access_code' => 'CODE CODE',
])->fresh();

$billPayer = Billpayer::create([
'address_id' => $address->id,
]);

$this->assertEquals('NL', $billPayer->country_id);
$this->assertEquals('1', $billPayer->province_id);
$this->assertEquals('1234 AB', $billPayer->postalcode);
$this->assertEquals('Buiswijk', $billPayer->city);
$this->assertEquals('Line 1', $billPayer->street_address);
$this->assertEquals('Line 2', $billPayer->address2);
$this->assertEquals('CODE CODE', $billPayer->access_code);
}
}

0 comments on commit 5d72bc5

Please sign in to comment.