Skip to content

Commit 55b1dca

Browse files
authored
Merge pull request #5 from whitecube/feature-custom-model
Feature custom model
2 parents f8e2f27 + 2ef819d commit 55b1dca

File tree

7 files changed

+57
-11
lines changed

7 files changed

+57
-11
lines changed

README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,22 @@ Manage acquisition, selling & renting prices for products and services in yo
77
composer require whitecube/laravel-prices
88
```
99

10+
## Configuration
11+
12+
You can publish the config file by running this command:
13+
14+
```shell
15+
php artisan vendor:publish --tag=prices-config
16+
```
17+
18+
Once your configuration file is created in `config/prices.php`, you can edit the Price model to a custom Price model by changing:
19+
20+
```php
21+
return [
22+
'model' => \App\Models\CustomPriceModel::class,
23+
];
24+
```
25+
1026
## Quick overview
1127

1228
This package lets you attach prices to anything you want, and keeps a history of the price changes overtime.

config/prices.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
return [
3+
/*
4+
|--------------------------------------------------------------------------
5+
| Prices model class
6+
|--------------------------------------------------------------------------
7+
|
8+
| The price model class can be changed to have more flexibility. You just have
9+
| to change it down here
10+
|
11+
*/
12+
'model' => Whitecube\LaravelPrices\Models\Price::class,
13+
];

src/HasPrices.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
use DateTime;
66
use Illuminate\Database\Eloquent\Model;
7-
use Whitecube\LaravelPrices\Models\Price;
87

98
trait HasPrices
109
{
@@ -15,7 +14,7 @@ trait HasPrices
1514
*/
1615
public function prices()
1716
{
18-
return $this->morphMany(Price::class, 'priceable');
17+
return $this->morphMany(config('prices.model'), 'priceable');
1918
}
2019

2120
/**
@@ -25,13 +24,13 @@ public function prices()
2524
*/
2625
public function price()
2726
{
28-
return $this->morphOne(Price::class, 'priceable')->where('type', $this->getDefaultPriceType())->current();
27+
return $this->morphOne(config('prices.model'), 'priceable')->where('type', $this->getDefaultPriceType())->current();
2928
}
3029

3130
/**
3231
* Easy accessor to get the current price as a whitecube/php-prices object
3332
*
34-
* @return \Whitecube\Price\Price|null
33+
* @return \Illuminate\Database\Eloquent\Model|null
3534
*/
3635
public function getPriceAttribute()
3736
{
@@ -43,10 +42,10 @@ public function getPriceAttribute()
4342
/**
4443
* Set (attach) a new price to this item
4544
*
46-
* @param Price $price
45+
* @param \Illuminate\Database\Eloquent\Model $price
4746
* @return \Illuminate\Database\Eloquent\Model
4847
*/
49-
public function setPriceAttribute(Price $price): Model
48+
public function setPriceAttribute(Model $price): Model
5049
{
5150
return $this->prices()->save($price);
5251
}
@@ -71,7 +70,8 @@ public function setPrice(
7170
DateTime $activated_at = null
7271
): static
7372
{
74-
$this->price = new Price($arguments, $amount, $minor, $currency, $type, $activated_at);
73+
$model = config('prices.model');
74+
$this->price = new $model($arguments, $amount, $minor, $currency, $type, $activated_at);
7575

7676
return $this;
7777
}
@@ -80,7 +80,7 @@ public function setPrice(
8080
* Get the price, as a model or as a whitecube/php-prices instance
8181
*
8282
* @param boolean $asModel
83-
* @return \Whitecube\Price\Price|\Whitecube\LaravelPrices\Models\Price
83+
* @return \Whitecube\Price\Price|\Illuminate\Database\Eloquent\Model
8484
*/
8585
public function getPrice(bool $asModel = false)
8686
{

src/LaravelPricesServiceProvider.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,19 @@ class LaravelPricesServiceProvider extends ServiceProvider
88
{
99
public function boot()
1010
{
11-
$this->loadMigrationsFrom(__DIR__.'/../database/migrations');
11+
$this->loadMigrationsFrom(__DIR__ . '/../database/migrations');
12+
13+
$this->publishes([
14+
$this->getConfigPath() => config_path('prices.php')
15+
], 'prices-config');
16+
17+
if (! file_exists(config_path('prices.php'))) {
18+
$this->mergeConfigFrom($this->getConfigPath(), 'prices');
19+
}
20+
}
21+
22+
protected function getConfigPath(): string
23+
{
24+
return __DIR__ . '/../config/prices.php';
1225
}
1326
}

tests/Feature/PricesTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<?php
22

33
use Whitecube\Price\Price as PhpPrice;
4-
use Whitecube\LaravelPrices\Models\Price;
54
use Whitecube\LaravelPrices\Tests\Fixtures\PriceableItem;
5+
use Whitecube\LaravelPrices\Models\Price;
66

77
test('a price can be set on a priceable item via the relationship', function() {
88
$priceable_item = new PriceableItem(['id' => '1234']);
@@ -14,6 +14,7 @@
1414
'activated_at' => now()->addWeek()
1515
]);
1616

17+
1718
$this->assertNotNull($price);
1819
$this->assertInstanceOf(Price::class, $price);
1920
$this->assertSame($priceable_item->id, $price->priceable_id);

tests/Feature/ScopesTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
use Whitecube\LaravelPrices\Models\Price;
44
use Whitecube\LaravelPrices\Tests\Fixtures\PriceableItem;
55

6-
76
test('the current scope can return the correct price', function() {
87
$priceable_item = new PriceableItem(['id' => 1234]);
98

tests/Pest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@
1717
uses(TestCase::class)->in('Feature');
1818
uses(RefreshDatabase::class)->in('Feature');
1919

20+
beforeEach(function () {
21+
config(['price.model' => Price::class]);
22+
});
23+
2024
/*
2125
|--------------------------------------------------------------------------
2226
| Expectations

0 commit comments

Comments
 (0)