Skip to content

Instantly share code, notes, and snippets.

@npapratovic
Created January 19, 2026 13:55
Show Gist options
  • Select an option

  • Save npapratovic/45a59cdf07b41029a170d0ea95957f6d to your computer and use it in GitHub Desktop.

Select an option

Save npapratovic/45a59cdf07b41029a170d0ea95957f6d to your computer and use it in GitHub Desktop.
<?php
namespace App\Enums;
// Industry enum
enum IndustryEnum: string
{
case HVAC = 'HVAC';
case LOGISTICS = 'Logistics';
case ENGINEERING = 'Engineering';
case OIL_GAS = 'Oil & Gas';
}
<?php
namespace App\Enums;
use App\Enums\IndustryEnum;
enum VariableEnum: string
{
case TEMPERATURE = 'temperature';
case HUMIDITY = 'humidity';
case PRESSURE = 'pressure';
case VELOCITY = 'velocity';
case VOLUME = 'volume';
case MASS = 'mass';
// Type-safe mapping to industries
const INDUSTRY_MAPPING = [
self::TEMPERATURE->value => IndustryEnum::HVAC,
self::HUMIDITY->value => IndustryEnum::HVAC,
self::PRESSURE->value => IndustryEnum::OIL_GAS,
self::VELOCITY->value => IndustryEnum::ENGINEERING,
self::VOLUME->value => IndustryEnum::LOGISTICS,
self::MASS->value => IndustryEnum::LOGISTICS,
];
/**
* Get the industry enum for a variable
*/
public function industry(): IndustryEnum
{
return self::INDUSTRY_MAPPING[$this->value];
}
/**
* Get all variables for a specific industry
*/
public static function variablesForIndustry(IndustryEnum $industry): array
{
return array_map(
fn($var) => self::from($var),
array_keys(
array_filter(self::INDUSTRY_MAPPING, fn($ind) => $ind === $industry)
)
);
}
}
// In Controller:
$var = VariableEnum::TEMPERATURE;
// Get industry
$industry = $var->industry();
echo $industry->value; // HVAC
// Get all variables for LOGISTICS
$logisticsVars = VariableEnum::variablesForIndustry(IndustryEnum::LOGISTICS);
foreach ($logisticsVars as $v) {
echo $v->value . "\n"; // VOLUME, MASS
}
- obadva enuma primaju string i vraćaju nazad industriju kojoj varijabla pripada ili sve varijable od određene industrije
// routes/api.php
use App\Http\Controllers\Api\VariableIndustryController;
Route::get('/variables-industries', [VariableIndustryController::class, 'index']);
Route::get('/industry-from-variable/{variable}', [VariableIndustryController::class, 'getIndustryFromVariable']);
Route::get('/variables-from-industry/{industry}', [VariableIndustryController::class, 'getVariablesFromIndustry']);
- primjer API kontroler:
<?php
namespace App\Http\Controllers\Api;
use App\Http\Controllers\Controller;
use App\Enums\IndustryEnum;
use App\Enums\VariableEnum;
class VariableIndustryController extends Controller
{
/**
* Return JSON with industries and their variables in a nested structure
*/
public function index()
{
$industries = [];
foreach (IndustryEnum::cases() as $industry) {
$industries[] = [
'industry' => $industry->value,
'variables' => array_map(
fn($variable) => $variable->value,
VariableEnum::variablesForIndustry($industry)
)
];
}
$allIndustries = array_map(fn($industry) => $industry->value, IndustryEnum::cases());
$allVariables = array_map(fn($variable) => $variable->value, VariableEnum::cases());
return response()->json([
'industries' => $industries,
'all_industries' => $allIndustries,
'all_variables' => $allVariables,
]);
}
/*
getIndustryFromVariable(string $variable) returns the industry of that variable.
getVariablesFromIndustry(string $industry) returns all variables for a given industry.
*/
/**
* Get industry from a variable
*/
public function getIndustryFromVariable(string $variable): JsonResponse
{
try {
$industry = VariableEnum::from($variable)->industry();
return response()->json([
'variable' => $variable,
'industry' => $industry->value,
]);
} catch (\ValueError $e) {
return response()->json([
'error' => "Variable '{$variable}' does not exist"
], 404);
}
}
/**
* Get variables from an industry
*/
public function getVariablesFromIndustry(string $industry): JsonResponse
{
try {
$industryEnum = IndustryEnum::from($industry);
$variables = array_map(
fn($variable) => $variable->value,
VariableEnum::variablesForIndustry($industryEnum)
);
return response()->json([
'industry' => $industryEnum->value,
'variables' => $variables,
]);
} catch (\ValueError $e) {
return response()->json([
'error' => "Industry '{$industry}' does not exist"
], 404);
}
}
}
- evo ga kako će izgledati response:
{
"industries": [
{
"industry": "HVAC",
"variables": ["temperature", "humidity"]
},
{
"industry": "LOGISTICS",
"variables": ["volume", "mass"]
},
{
"industry": "ENGINEERING",
"variables": ["velocity"]
},
{
"industry": "OIL_GAS",
"variables": ["pressure"]
}
],
"all_industries": ["HVAC", "LOGISTICS", "ENGINEERING", "OIL_GAS"],
"all_variables": ["temperature", "humidity", "pressure", "velocity", "volume", "mass"]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment