73 lines
2.6 KiB
PHP

<?php
namespace App\Services;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\App;
class Service {
private $language = 'en'; // Default language set as private attribute
private $role = 'default';
private $filePaths;
public function __construct() {
$this->language = App::getLocale();
$this->role = session('user_role');
$this->setFilePaths();
}
public function setLanguage($language) {
$this->language = $language;
$this->setFilePaths(); // Update file paths when language changes
}
public function setRole($role) {
$this->role = $role;
$this->setFilePaths(); // Update file paths when language changes
}
private function setFilePaths() {
$basePath = "app/json/roles/{$this->role}/{$this->language}/";
$this->filePaths = [
'overview_a' => $basePath . 'polisplexity_overview_a.json',
'resources_a' => $basePath . 'polisplexity_resources_a.json',
'tokenomics_a' => $basePath . 'polisplexity_tokenomics_a.json',
'overview_b' => $basePath . 'polisplexity_overview_b.json',
'resources_b' => $basePath . 'polisplexity_resources_b.json',
'tokenomics_b' => $basePath . 'polisplexity_tokenomics_b.json',
'overview_c' => $basePath . 'polisplexity_overview_c.json',
'resources_c' => $basePath . 'polisplexity_resources_c.json',
'tokenomics_c' => $basePath . 'polisplexity_tokenomics_c.json',
];
}
public function get($methodName) {
$methodName = strtolower($methodName); // Ensure method name is in the correct case
if (array_key_exists($methodName, $this->filePaths)) {
return $this->loadJsonData($this->filePaths[$methodName],$methodName);
}
throw new \Exception("Method not found or not associated with a file path");
}
private function loadJsonData($filePath, $methodName) {
$jsonFilePath = storage_path($filePath);
if (!file_exists($jsonFilePath)) {
// If file does not exist, use default role and language
$this->role = 'default';
$this->language = 'en';
$this->setFilePaths(); // Update file paths with default role and language
$filePath = $this->filePaths[$methodName];
$jsonFilePath = storage_path($filePath);
}
if (!file_exists($jsonFilePath)) {
// If default file also does not exist, return an empty array
return [];
}
$jsonData = file_get_contents($jsonFilePath);
return json_decode($jsonData, true);
}
}