forked from corowne/lorekeeper
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1eac0fb
commit 0f89e0a
Showing
12 changed files
with
638 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,254 @@ | ||
<?php | ||
|
||
namespace App\Models\Currency; | ||
|
||
use App\Models\Model; | ||
use App\Models\Currency\Currency; | ||
|
||
class CurrencyConversion extends Model { | ||
|
||
/** | ||
* The attributes that are mass assignable. | ||
* | ||
* @var array | ||
*/ | ||
protected $fillable = [ | ||
'currency_id', 'conversion_id', 'rate', | ||
]; | ||
|
||
/** | ||
* The table associated with the model. | ||
* | ||
* @var string | ||
*/ | ||
protected $table = 'currency_conversions'; | ||
/** | ||
* Validation rules for creation. | ||
* | ||
* @var array | ||
*/ | ||
public static $createRules = [ | ||
'currency_id' => 'required|exists:currencies,id', | ||
'conversion_id' => 'required|exists:currencies,id', | ||
'rate' => 'required|numeric', | ||
]; | ||
|
||
/** | ||
* Validation rules for updating. | ||
* | ||
* @var array | ||
*/ | ||
public static $updateRules = [ | ||
'currency_id' => 'required|exists:currencies,id', | ||
'conversion_id' => 'required|exists:currencies,id', | ||
'rate' => 'required|numeric', | ||
]; | ||
|
||
/********************************************************************************************** | ||
RELATIONSHIPS | ||
**********************************************************************************************/ | ||
|
||
/** | ||
* Get the currency that the conversion is for. | ||
*/ | ||
public function currency() { | ||
return $this->belongsTo(Currency::class, 'currency_id'); | ||
} | ||
|
||
/** | ||
* Get the currency that is converted to. | ||
*/ | ||
public function convert() { | ||
return $this->belongsTo(Currency::class, 'conversion_id'); | ||
} | ||
|
||
/********************************************************************************************** | ||
ACCESSORS | ||
**********************************************************************************************/ | ||
|
||
/** | ||
* Displays the currency as an icon with tooltip. | ||
* | ||
* @return string | ||
*/ | ||
public function getDisplayIconAttribute() { | ||
return '<img src="'.$this->currencyIconUrl.'" title="'.$this->name.($this->abbreviation ? ' ('.$this->abbreviation.')' : '').'" data-toggle="tooltip" alt="'.$this->name.'"/>'; | ||
} | ||
|
||
/** | ||
* Gets the file directory containing the model's image. | ||
* | ||
* @return string | ||
*/ | ||
public function getImageDirectoryAttribute() { | ||
return 'images/data/currencies'; | ||
} | ||
|
||
/** | ||
* Gets the file name of the model's image. | ||
* | ||
* @return string | ||
*/ | ||
public function getCurrencyImageFileNameAttribute() { | ||
return $this->hash.$this->id.'-image.png'; | ||
} | ||
|
||
/** | ||
* Gets the file name of the model's icon image. | ||
* | ||
* @return string | ||
*/ | ||
public function getCurrencyIconFileNameAttribute() { | ||
return $this->hash.$this->id.'-icon.png'; | ||
} | ||
|
||
/** | ||
* Gets the path to the file directory containing the model's image. | ||
* | ||
* @return string | ||
*/ | ||
public function getCurrencyImagePathAttribute() { | ||
return public_path($this->imageDirectory); | ||
} | ||
|
||
/** | ||
* Gets the path to the file directory containing the model's icon image. | ||
* | ||
* @return string | ||
*/ | ||
public function getCurrencyIconPathAttribute() { | ||
return public_path($this->imageDirectory); | ||
} | ||
|
||
/** | ||
* Gets the URL of the model's image. | ||
* | ||
* @return string | ||
*/ | ||
public function getCurrencyImageUrlAttribute() { | ||
if (!$this->has_image) { | ||
return null; | ||
} | ||
|
||
return asset($this->imageDirectory.'/'.$this->currencyImageFileName); | ||
} | ||
|
||
/** | ||
* Gets the URL of the model's icon image. | ||
* | ||
* @return string | ||
*/ | ||
public function getCurrencyIconUrlAttribute() { | ||
if (!$this->has_icon) { | ||
return null; | ||
} | ||
|
||
return asset($this->imageDirectory.'/'.$this->currencyIconFileName); | ||
} | ||
|
||
/********************************************************************************************** | ||
ACCESSORS | ||
**********************************************************************************************/ | ||
|
||
/** | ||
* Displays the model's name, linked to its encyclopedia page. | ||
* | ||
* @return string | ||
*/ | ||
public function getDisplayNameAttribute() { | ||
return '<a href="'.$this->url.'" class="display-currency">'.$this->name.'</a>'; | ||
} | ||
|
||
/** | ||
* Gets the URL of the model's encyclopedia page. | ||
* | ||
* @return string | ||
*/ | ||
public function getUrlAttribute() { | ||
return url('world/currencies?name='.$this->name); | ||
} | ||
|
||
/** | ||
* Gets the currency's asset type for asset management. | ||
* | ||
* @return string | ||
*/ | ||
public function getAssetTypeAttribute() { | ||
return 'currencies'; | ||
} | ||
|
||
/** | ||
* Gets the admin edit URL. | ||
* | ||
* @return string | ||
*/ | ||
public function getAdminUrlAttribute() { | ||
return url('admin/data/currencies/edit/'.$this->id); | ||
} | ||
|
||
/** | ||
* Gets the power required to edit this model. | ||
* | ||
* @return string | ||
*/ | ||
public function getAdminPowerAttribute() { | ||
return 'edit_data'; | ||
} | ||
|
||
/********************************************************************************************** | ||
OTHER FUNCTIONS | ||
**********************************************************************************************/ | ||
|
||
/** | ||
* Displays a given value of the currency with icon, abbreviation or name. | ||
* | ||
* @param mixed $value | ||
* | ||
* @return string | ||
*/ | ||
public function display($value) { | ||
$ret = '<span class="display-currency">'.$value.' '; | ||
if ($this->has_icon) { | ||
$ret .= $this->displayIcon; | ||
} elseif ($this->abbreviation) { | ||
$ret .= $this->abbreviation; | ||
} else { | ||
$ret .= $this->name; | ||
} | ||
|
||
return $ret.'</span>'; | ||
} | ||
|
||
/** | ||
* Gets the ratio based on the decimal conversion rate. | ||
*/ | ||
public function ratio($return = false) { | ||
$numerator = $this->rate * 100; // Convert rate to avoid floating point issues | ||
$denominator = 100; | ||
$divisor = $this->gcd($numerator, $denominator); // Find GCD to simplify ratio | ||
|
||
// Simplify the ratio | ||
$numerator /= $divisor; | ||
$denominator /= $divisor; | ||
|
||
if ($return) { | ||
return [$numerator, $denominator]; | ||
} | ||
return $numerator . ":" . $denominator; | ||
} | ||
|
||
/** | ||
* Gets the greatest common divisor of two numbers. | ||
*/ | ||
private function gcd($a, $b) { | ||
return $b ? $this->gcd($b, $a % $b) : $a; | ||
} | ||
} |
Oops, something went wrong.