Story 3.1: data projects json

This commit is contained in:
2026-02-04 17:03:04 +01:00
parent c9369df683
commit d520fe848f
5 changed files with 185 additions and 23 deletions

View File

@@ -8,4 +8,74 @@ function include_template(string $name, array $data = []): void
{
extract($data, EXTR_SKIP);
include __DIR__ . "/../templates/{$name}.php";
}
/**
* Charge et parse un fichier JSON
*/
function loadJsonData(string $filename): array
{
$path = __DIR__ . "/../data/{$filename}";
if (!file_exists($path)) {
error_log("JSON file not found: {$filename}");
return [];
}
$content = file_get_contents($path);
$data = json_decode($content, true);
if (json_last_error() !== JSON_ERROR_NONE) {
error_log("JSON parse error in {$filename}: " . json_last_error_msg());
return [];
}
return $data;
}
/**
* Récupère tous les projets
*/
function getProjects(): array
{
$data = loadJsonData('projects.json');
return $data['projects'] ?? [];
}
/**
* Récupère les projets par catégorie
*/
function getProjectsByCategory(string $category): array
{
return array_values(array_filter(getProjects(), fn($p) => ($p['category'] ?? '') === $category));
}
/**
* Récupère un projet par son slug
*/
function getProjectBySlug(string $slug): ?array
{
foreach (getProjects() as $project) {
if (($project['slug'] ?? '') === $slug) {
return $project;
}
}
return null;
}
/**
* Récupère les technologies uniques de tous les projets
*/
function getAllTechnologies(): array
{
$technologies = [];
foreach (getProjects() as $project) {
foreach ($project['technologies'] ?? [] as $tech) {
if (!in_array($tech, $technologies, true)) {
$technologies[] = $tech;
}
}
}
sort($technologies);
return $technologies;
}