Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"Custom models functionality" #1

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion config/balance.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
<?php

use Geow\Balance\Models\Balance;

return [
/**
* Default table name.
* If you have changed the migration, make sure you change this too.
*/
'table' => 'balances'
'table' => 'balances',

/**
* Default model to use.
* Change this if you have extended the model.
*/
'model' => Balance::class,
];
12 changes: 12 additions & 0 deletions src/Concerns/BalanceInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace Geow\Balance\Concerns;

use Illuminate\Database\Eloquent\Relations\MorphTo;

interface BalanceInterface
{
public function balanceable(): MorphTo;
}
3 changes: 2 additions & 1 deletion src/Models/Balance.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@

namespace Geow\Balance\Models;

use Geow\Balance\Concerns\BalanceInterface;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\MorphTo;
use Illuminate\Support\Number;

class Balance extends Model
class Balance extends Model implements BalanceInterface
{
protected $guarded = [];

Expand Down
13 changes: 7 additions & 6 deletions src/Traits/HasBalance.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

namespace Geow\Balance\Traits;

use Geow\Balance\Concerns\BalanceInterface;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Geow\Balance\Models\Balance;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\MorphMany;
use Illuminate\Support\Number;

Expand All @@ -13,7 +14,7 @@ trait HasBalance

public function credits(): MorphMany
{
return $this->morphMany(Balance::class, 'balanceable');
return $this->morphMany(config('balance.model'), 'balanceable');
}

public function withCurrency(string $currency): self
Expand All @@ -37,17 +38,17 @@ protected function creditCurrency(): Attribute
);
}

public function increaseCredit(int $amount, ?string $reason = null): Balance
public function increaseCredit(int $amount, ?string $reason = null): BalanceInterface
{
return $this->createCredit($amount, $reason);
}

public function decreaseCredit(int $amount, ?string $reason = null): Balance
public function decreaseCredit(int $amount, ?string $reason = null): BalanceInterface
{
return $this->createCredit(-1 * abs($amount), $reason);
}

public function setCredit(int $amount, ?string $reason = null): Balance
public function setCredit(int $amount, ?string $reason = null): BalanceInterface
{
return $this->createCredit($amount, $reason);
}
Expand All @@ -62,7 +63,7 @@ public function hasCredit(): bool
return $this->credit > 0;
}

protected function createCredit(int $amount, ?string $reason = null): Balance
protected function createCredit(int $amount, ?string $reason = null): Model|BalanceInterface
{
return $this->credits()->create([
'amount' => $amount,
Expand Down
80 changes: 80 additions & 0 deletions tests/ExampleTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,85 @@
<?php

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\MorphTo;
use Geow\Balance\Concerns\BalanceInterface;
use Geow\Balance\Traits\HasBalance;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Support\Facades\Number;

it('can test', function () {
expect(true)->toBeTrue();
});

it ('can use any model configured in the config balance.model and implements the BalanceInterface', function () {
config()->set('database.default', 'testing');

// Run the test user migration
$migration = include __DIR__.'/create_test_user_table.php.stub';
$migration->up();

// Run the balance migration
$migration = include __DIR__.'/create_balances_custom_table.php.stub';
$migration->up();

config()->set('balance.table', 'balances_custom');
config()->set('balance.model', CustomBalance::class);

$user = User::create([
'name' => 'John Doe',
'email' => 'john@doe.com',
]);
$user->setCredit(2000);
$this->assertEquals(2000, $user->credit);
$user->increaseCredit(1000);
$this->assertEquals(3000, $user->credit);
$user->decreaseCredit(500);
$this->assertEquals(2500, $user->credit);

$this->assertEquals('$25.00', $user->creditCurrency);
});


// Create a model that uses the HasBalance trait and test that it can increase, decrease, set and reset the balance
class User extends Model
{
use HasBalance;

protected $fillable = ['name', 'email'];
}


class CustomBalance extends Model implements BalanceInterface
{
protected $guarded = [];

// before saving the model ensure the custom column is the name of the model that originated the balance
protected static function boot()
{
parent::boot();

static::saving(function ($model) {
$model->custom = get_class($model->balanceable);
});
}

public function __construct(array $attributes = [])
{
parent::__construct($attributes);

$this->guarded[] = $this->primaryKey;
$this->table = config('balance.table');
}

protected function amountCurrency(): Attribute
{
return Attribute::make(
get: fn () => Number::currency($this->amount / 100),
);
}

public function balanceable(): MorphTo
{
return $this->morphTo();
}
}
20 changes: 20 additions & 0 deletions tests/create_balances_custom_table.php.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

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

return new class extends Migration
{
public function up()
{
Schema::create('balances_custom', function (Blueprint $table) {
$table->id();
$table->morphs('balanceable');
$table->integer('amount');
$table->text('reason')->nullable();
$table->text('custom')->nullable();
$table->timestamps();
});
}
};
23 changes: 23 additions & 0 deletions tests/create_test_user_table.php.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

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

return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('email')->unique();
$table->timestamps();
});
}
};
Loading