Changeset View
Changeset View
Standalone View
Standalone View
src/app/Wallet.php
<?php | <?php | ||||
namespace App; | namespace App; | ||||
use App\User; | use App\User; | ||||
use Iatstuti\Database\Support\NullableFields; | use Iatstuti\Database\Support\NullableFields; | ||||
use Illuminate\Database\Eloquent\Model; | use Illuminate\Database\Eloquent\Model; | ||||
/** | /** | ||||
* The eloquent definition of a wallet -- a container with a chunk of change. | * The eloquent definition of a wallet -- a container with a chunk of change. | ||||
* | * | ||||
* A wallet is owned by an {@link \App\User}. | * A wallet is owned by an {@link \App\User}. | ||||
* | |||||
* @property integer $balance | |||||
*/ | */ | ||||
class Wallet extends Model | class Wallet extends Model | ||||
{ | { | ||||
use NullableFields; | use NullableFields; | ||||
/** | |||||
Our table name for the shall be 'wallet'. | |||||
@var string | |||||
*/ | |||||
public $incrementing = false; | public $incrementing = false; | ||||
protected $keyType = 'string'; | protected $keyType = 'string'; | ||||
public $timestamps = false; | public $timestamps = false; | ||||
protected $attributes = [ | protected $attributes = [ | ||||
'balance' => 0.00, | 'balance' => 0.00, | ||||
'currency' => 'CHF' | 'currency' => 'CHF' | ||||
Show All 17 Lines | class Wallet extends Model | ||||
* Add a controller to this wallet. | * Add a controller to this wallet. | ||||
* | * | ||||
* @param \App\User $user The user to add as a controller to this wallet. | * @param \App\User $user The user to add as a controller to this wallet. | ||||
* | * | ||||
* @return void | * @return void | ||||
*/ | */ | ||||
public function addController(User $user) | public function addController(User $user) | ||||
{ | { | ||||
if (!$this->controllers()->get()->contains($user)) { | if (!$this->controllers->contains($user)) { | ||||
$this->controllers()->save($user); | $this->controllers()->save($user); | ||||
} | } | ||||
} | } | ||||
/** | /** | ||||
* Remove a controller from this wallet. | * Remove a controller from this wallet. | ||||
* | * | ||||
* @param \App\User $user The user to remove as a controller from this wallet. | * @param \App\User $user The user to remove as a controller from this wallet. | ||||
* | * | ||||
* @return void | * @return void | ||||
*/ | */ | ||||
public function removeController(User $user) | public function removeController(User $user) | ||||
{ | { | ||||
if ($this->controllers()->get()->contains($user)) { | if ($this->controllers->contains($user)) { | ||||
$this->controllers()->detach($user); | $this->controllers()->detach($user); | ||||
} | } | ||||
} | } | ||||
/** | /** | ||||
* Add an amount of pecunia to this wallet's balance. | * Add an amount of pecunia to this wallet's balance. | ||||
* | * | ||||
* @param float $amount The amount of pecunia to add. | * @param float $amount The amount of pecunia to add. | ||||
Show All 23 Lines | public function debit(float $amount) | ||||
$this->save(); | $this->save(); | ||||
return $this; | return $this; | ||||
} | } | ||||
/** | /** | ||||
* Controllers of this wallet. | * Controllers of this wallet. | ||||
* | * | ||||
* @return \App\User[] | * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany | ||||
*/ | */ | ||||
public function controllers() | public function controllers() | ||||
{ | { | ||||
return $this->belongsToMany( | return $this->belongsToMany( | ||||
'App\User', // The foreign object definition | 'App\User', // The foreign object definition | ||||
'user_accounts', // The table name | 'user_accounts', // The table name | ||||
'wallet_id', // The local foreign key | 'wallet_id', // The local foreign key | ||||
'user_id' // The remote foreign key | 'user_id' // The remote foreign key | ||||
); | ); | ||||
} | } | ||||
/** | /** | ||||
* Entitlements billed to this wallet. | * Entitlements billed to this wallet. | ||||
* | * | ||||
* @return Entitlement[] | * @return \Illuminate\Database\Eloquent\Relations\HasMany | ||||
*/ | */ | ||||
public function entitlements() | public function entitlements() | ||||
{ | { | ||||
return $this->hasMany('App\Entitlement'); | return $this->hasMany('App\Entitlement'); | ||||
} | } | ||||
/** | /** | ||||
* The owner of the wallet -- the wallet is in his/her back pocket. | * The owner of the wallet -- the wallet is in his/her back pocket. | ||||
* | * | ||||
* @return User | * @return \Illuminate\Database\Eloquent\Relations\BelongsTo | ||||
*/ | */ | ||||
public function owner() | public function owner() | ||||
{ | { | ||||
return $this->belongsTo('App\User', 'user_id', 'id'); | return $this->belongsTo('App\User', 'user_id', 'id'); | ||||
} | } | ||||
} | } |