class Curs{
public static $currencies = [
['name_cur'=>'Рублей','short_cur'=>'₽','code_cur'=>'RUB','ratio_cur'=>1,'default_cur'=>1,'display_cur'=>1,],
['name_cur'=>'Dollar','short_cur'=>'$','code_cur'=>'USD','ratio_cur'=>1,'default_cur'=>1,'display_cur'=>1,],
['name_cur'=>'Euro','short_cur'=>'€','code_cur'=>'EUR','ratio_cur'=>1,'default_cur'=>1,'display_cur'=>1,],
['name_cur'=>'Pound','short_cur'=>'£','code_cur'=>'GBP','ratio_cur'=>1,'default_cur'=>1,'display_cur'=>1,],
['name_cur'=>'円','short_cur'=>'¥','code_cur'=>'JPY','ratio_cur'=>1,'default_cur'=>1,'display_cur'=>1,],
['name_cur'=>'元','short_cur'=>'Ұ','code_cur'=>'CNY','ratio_cur'=>1,'default_cur'=>1,'display_cur'=>1,],
];
}
public static $currencies = [
(object)['name_cur'=>'Рублей','short_cur'=>'₽','code_cur'=>'RUB','ratio_cur'=>1,'default_cur'=>1,'display_cur'=>1,],
(object)['name_cur'=>'Dollar','short_cur'=>'$','code_cur'=>'USD','ratio_cur'=>1,'default_cur'=>1,'display_cur'=>1,],
...........
];
<?php
enum CurrenciesEnum: string
{
case RUB = 'RUB';
case USD = 'USD';
case EUR = 'EUR';
case GBP = 'GBP';
case JPY = 'JPY';
case CNY = 'CNY';
private const DATA = [
'RUB' => ['name' => 'Рублей', 'short' => '₽', 'ratio' => 1, 'default' => 1, 'display' => 1],
'USD' => ['name' => 'Dollar', 'short' => '$', 'ratio' => 1, 'default' => 1, 'display' => 1],
'EUR' => ['name' => 'Euro', 'short' => '€', 'ratio' => 1, 'default' => 1, 'display' => 1],
'GBP' => ['name' => 'Pound', 'short' => '£', 'ratio' => 1, 'default' => 1, 'display' => 1],
'JPY' => ['name' => '円', 'short' => '¥', 'ratio' => 1, 'default' => 1, 'display' => 1],
'CNY' => ['name' => '元', 'short' => 'Ұ', 'ratio' => 1, 'default' => 1, 'display' => 1],
];
public function code(): string
{
return $this->value;
}
public function name(): string
{
$this->insureValue();
return self::DATA[$this->value]['name'];
}
public function short(): string
{
$this->insureValue();
return self::DATA[$this->value]['short'];
}
public function ratio(): string
{
$this->insureValue();
return self::DATA[$this->value]['ratio'];
}
public function default(): string
{
$this->insureValue();
return self::DATA[$this->value]['default'];
}
public function display(): string
{
$this->insureValue();
return self::DATA[$this->value]['display'];
}
private function insureValue(): void
{
if (!isset(self::DATA[$this->value])) {
throw new InvalidArgumentException($this->value);
}
}
}
echo CurrenciesEnum::RUB->code(), PHP_EOL;
echo CurrenciesEnum::RUB->name(), PHP_EOL;
echo CurrenciesEnum::RUB->short(), PHP_EOL;
<?php
enum CurrenciesEnum
{
case RUB;
case USD;
case EUR;
case GBP;
case JPY;
case CNY;
public function code(): string
{
return $this->name;
}
public function name(): string
{
return self::getField($this->name, 'name');
}
public function short(): string
{
return self::getField($this->name, 'short');
}
public function ratio(): string
{
return self::getField($this->name, 'ratio');
}
public function default(): string
{
return self::getField($this->name, 'default');
}
public function display(): string
{
return self::getField($this->name, 'display');
}
private static function getField(string $currency, string $field): string
{
$data = [
self::RUB->name => ['name' => 'Рублей', 'short' => '₽', 'ratio' => 1, 'default' => 1, 'display' => 1],
self::USD->name => ['name' => 'Dollar', 'short' => '$', 'ratio' => 1, 'default' => 1, 'display' => 1],
self::EUR->name => ['name' => 'Euro', 'short' => '€', 'ratio' => 1, 'default' => 1, 'display' => 1],
self::GBP->name => ['name' => 'Pound', 'short' => '£', 'ratio' => 1, 'default' => 1, 'display' => 1],
self::JPY->name => ['name' => '円', 'short' => '¥', 'ratio' => 1, 'default' => 1, 'display' => 1],
self::CNY->name => ['name' => '元', 'short' => 'Ұ', 'ratio' => 1, 'default' => 1, 'display' => 1],
];
if (!isset($data[$currency])) {
throw new InvalidArgumentException($currency);
}
return $data[$currency][$field];
}
}
echo CurrenciesEnum::RUB->code(), PHP_EOL;
echo CurrenciesEnum::RUB->name(), PHP_EOL;
echo CurrenciesEnum::RUB->short(), PHP_EOL;
final class Currency
{
public function __construct(
readonly string $code,
readonly string $name,
readonly string $short,
readonly int $ratio = 1,
readonly int $default = 1,
readonly int $display = 1,
)
{
}
}
final class CurrenciesStorage
{
private static self|null $instance = null;
/** @var Currency[] */
private array $currencies = [];
private function __construct()
{
}
public static function getInstance(): self
{
if (self::$instance === null) {
self::$instance = new self();
}
return self::$instance;
}
public function add(Currency $currency): void
{
$this->currencies[$currency->code] = $currency;
}
public function has(string $code): bool
{
return isset($this->currencies[$code]);
}
public function get(string $code): Currency|null
{
return $this->currencies[$code] ?? null;
}
public function all(): array
{
return $this->currencies;
}
}
$storage = CurrenciesStorage::getInstance();
$storage->add(new Currency('RUB', 'Рублей', '₽'));
$storage->add(new Currency('USD', 'Dollar', '$'));
$storage->add(new Currency('EUR', 'Euro', '€'));
// Получить данные по одному объекту
echo $storage->get('RUB')->code, PHP_EOL;
echo $storage->get('RUB')->name, PHP_EOL;
echo $storage->get('RUB')->short, PHP_EOL;
// Получить все объекты
var_dump($storage->all());
<?php
class Curs{
public $currencies;
function __construct($curencyArray){
$this->currencies = new class($curencyArray){
function __construct($arr){
foreach($arr as $crn){
$this->{strtolower($crn['code_cur'])} = (object)$crn;
}
}
};
}
};
$currencies = [
['name_cur'=>'Рублей','short_cur'=>'₽','code_cur'=>'RUB','ratio_cur'=>1,'default_cur'=>1,'display_cur'=>1,],
['name_cur'=>'Dollar','short_cur'=>'$','code_cur'=>'USD','ratio_cur'=>1,'default_cur'=>1,'display_cur'=>1,],
['name_cur'=>'Euro','short_cur'=>'€','code_cur'=>'EUR','ratio_cur'=>1,'default_cur'=>1,'display_cur'=>1,],
['name_cur'=>'Pound','short_cur'=>'£','code_cur'=>'GBP','ratio_cur'=>1,'default_cur'=>1,'display_cur'=>1,],
['name_cur'=>'円','short_cur'=>'¥','code_cur'=>'JPY','ratio_cur'=>1,'default_cur'=>1,'display_cur'=>1,],
['name_cur'=>'元','short_cur'=>'Ұ','code_cur'=>'CNY','ratio_cur'=>1,'default_cur'=>1,'display_cur'=>1,],
];
$curs = new Curs($currencies);
var_dump($curs->currencies->usd);
/*
object(stdClass)#4 (6) {
["name_cur"]=>
string(6) "Dollar"
["short_cur"]=>
string(1) "$"
["code_cur"]=>
string(3) "USD"
["ratio_cur"]=>
int(1)
["default_cur"]=>
int(1)
["display_cur"]=>
int(1)
}
*/