moneta-sdk-php/src/MonetaSdkSettings.php

58 lines
1.6 KiB
PHP

<?php
namespace Moneta;
class MonetaSdkSettings {
/**
* Config file names
*/
const SETTINGS_ACCOUNT = '/account.php';
const SETTINGS_API = '/api.php';
const SETTINGS_STORAGE = '/storage.php';
const SETTINGS_URL = '/url.php';
/**
* Exception messages text
*/
const EXCEPTION_ERROR_PARAM_NAME = 'There is no such parameter: ';
const EXCEPTION_NO_PARAM_FILE = "Config file not found: ";
/**
* @param null $configPath
* @param @nameParam
* @return array
* @throws MonetaSdkException
*/
public static function getSettings($nameParam, ?string $configPath = null) {
$configPath = $configPath ? $configPath : dirname(__DIR__) . '/config';
$arrayAccountSettings = self::getSettingsFromConfigFiles($configPath . self::SETTINGS_ACCOUNT);
$arrayApiSettings = require dirname(__DIR__) . '/config/api.php';
$arrayUrlSettings = self::getSettingsFromConfigFiles($configPath . self::SETTINGS_URL);
$allSettings = array_merge(
$arrayAccountSettings,
$arrayApiSettings,
$arrayUrlSettings
);
if (!isset($allSettings[$nameParam])) {
throw new MonetaSdkException(self::EXCEPTION_ERROR_PARAM_NAME . $nameParam);
}
return $allSettings[$nameParam];
}
/**
* @param $fileName
* @return array
* @throws MonetaSdkException
*/
private static function getSettingsFromConfigFiles($fileName) {
if (!file_exists($fileName)) {
throw new MonetaSdkException(self::EXCEPTION_NO_PARAM_FILE . $fileName);
}
return require($fileName);
}
}