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 App\Traits\SettingsTrait; | use App\Traits\SettingsTrait; | ||||
use Carbon\Carbon; | use Carbon\Carbon; | ||||
use Iatstuti\Database\Support\NullableFields; | use Iatstuti\Database\Support\NullableFields; | ||||
use Illuminate\Database\Eloquent\Model; | use Illuminate\Database\Eloquent\Model; | ||||
use Illuminate\Support\Facades\DB; | use Illuminate\Support\Facades\DB; | ||||
/** | /** | ||||
* 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 | * @property int $balance Current balance in cents | ||||
* @property string $currency Currency code | |||||
* @property ?string $description Description | |||||
* @property string $id Unique identifier | |||||
* @property ?\App\User $owner Owner (can be null when owner is deleted) | |||||
* @property int $user_id Owner's identifier | |||||
*/ | */ | ||||
class Wallet extends Model | class Wallet extends Model | ||||
{ | { | ||||
use NullableFields; | use NullableFields; | ||||
use SettingsTrait; | use SettingsTrait; | ||||
public $incrementing = false; | public $incrementing = false; | ||||
protected $keyType = 'string'; | protected $keyType = 'string'; | ||||
public $timestamps = false; | public $timestamps = false; | ||||
/** | |||||
* The attributes' default values. | |||||
* | |||||
* @var array | |||||
*/ | |||||
protected $attributes = [ | protected $attributes = [ | ||||
'balance' => 0, | 'balance' => 0, | ||||
'currency' => 'CHF' | |||||
]; | ]; | ||||
/** | |||||
* The attributes that are mass assignable. | |||||
* | |||||
* @var array | |||||
*/ | |||||
protected $fillable = [ | protected $fillable = [ | ||||
'currency' | 'currency', | ||||
'description' | |||||
]; | ]; | ||||
/** | |||||
* The attributes that can be not set. | |||||
* | |||||
* @var array | |||||
*/ | |||||
protected $nullable = [ | protected $nullable = [ | ||||
'description', | 'description', | ||||
]; | ]; | ||||
/** | |||||
* The types of attributes to which its values will be cast | |||||
* | |||||
* @var array | |||||
*/ | |||||
protected $casts = [ | protected $casts = [ | ||||
'balance' => 'integer', | 'balance' => 'integer', | ||||
]; | ]; | ||||
/** | /** | ||||
* Add a controller to this wallet. | * Add a controller to this wallet. | ||||
* | * | ||||
▲ Show 20 Lines • Show All 290 Lines • ▼ Show 20 Lines | class Wallet extends Model | ||||
* @param string $locale A locale for the output | * @param string $locale A locale for the output | ||||
* | * | ||||
* @return string String representation, e.g. "9.99 CHF" | * @return string String representation, e.g. "9.99 CHF" | ||||
*/ | */ | ||||
public function money(int $amount, $locale = 'de_DE') | public function money(int $amount, $locale = 'de_DE') | ||||
{ | { | ||||
$amount = round($amount / 100, 2); | $amount = round($amount / 100, 2); | ||||
// Prefer intl extension's number formatter | |||||
if (class_exists('NumberFormatter')) { | |||||
$nf = new \NumberFormatter($locale, \NumberFormatter::CURRENCY); | $nf = new \NumberFormatter($locale, \NumberFormatter::CURRENCY); | ||||
$result = $nf->formatCurrency($amount, $this->currency); | $result = $nf->formatCurrency($amount, $this->currency); | ||||
// Replace non-breaking space | // Replace non-breaking space | ||||
return str_replace("\xC2\xA0", " ", $result); | return str_replace("\xC2\xA0", " ", $result); | ||||
} | } | ||||
return sprintf('%.2f %s', $amount, $this->currency); | |||||
} | |||||
/** | /** | ||||
* 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 \Illuminate\Database\Eloquent\Relations\BelongsTo | * @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'); | ||||
▲ Show 20 Lines • Show All 51 Lines • Show Last 20 Lines |