187 lines
No EOL
5.7 KiB
PHP
187 lines
No EOL
5.7 KiB
PHP
<?php
|
|
|
|
namespace App\Lib\Helpers;
|
|
|
|
use Illuminate\Support\Str;
|
|
|
|
class StaticPageSectionHelper
|
|
{
|
|
/** Get value string from string|array MiniCrud|object MiniCrud
|
|
* @param mixed $x key
|
|
* @return string
|
|
*/
|
|
private static function value(mixed $x): string
|
|
{
|
|
if (is_string($x)) return trim($x);
|
|
|
|
if (is_array($x)) {
|
|
$v = $x['value'] ?? '';
|
|
return is_string($v) ? trim($v) : '';
|
|
}
|
|
|
|
if (is_object($x)) {
|
|
$v = $x->value ?? '';
|
|
return is_string($v) ? trim($v) : '';
|
|
}
|
|
|
|
return '';
|
|
}
|
|
|
|
/** Get alt/title from array MiniCrud image
|
|
* @param array $x picto array
|
|
* @return string
|
|
*/
|
|
private static function imgAlt(array $x): string
|
|
{
|
|
if (is_array($x)) {
|
|
$v = $x['alt'] ?? '';
|
|
return is_string($v) ? trim($v) : '';
|
|
}
|
|
return '';
|
|
}
|
|
|
|
/** Get alt/title from array MiniCrud image
|
|
* @param array $x picto array
|
|
* @return string
|
|
*/
|
|
private static function imgTitle(array $x): string
|
|
{
|
|
if (is_array($x)) {
|
|
$v = $x['title'] ?? '';
|
|
return is_string($v) ? trim($v) : '';
|
|
}
|
|
return '';
|
|
}
|
|
|
|
/** Extrait des points depuis HTML: priorise <li>, sinon split par <br>/newlines
|
|
* @param string $html content
|
|
* @return array
|
|
*/
|
|
private static function extractContent(string $html): array
|
|
{
|
|
$html = trim($html);
|
|
|
|
$title = null;
|
|
$points = [];
|
|
$asterisc = null;
|
|
|
|
// ASTERISC
|
|
if (preg_match('/<p>\s*\*(.*?)<\/p>/is', $html, $match)) {
|
|
$asterisc = strip_tags($match[1]);
|
|
$html = str_replace($match[0], '', $html);
|
|
}
|
|
|
|
// titre
|
|
if (preg_match('/<(h[1-3])[^>]*>(.*?)<\/\1>/is', $html, $match)) {
|
|
$title = trim(strip_tags($match[2], '<strong><br>'));
|
|
$html = str_replace($match[0], '', $html);
|
|
}
|
|
|
|
// List
|
|
if (stripos($html, '<li') !== false) {
|
|
preg_match_all('/<li[^>]*>(.*?)<\/li>/is', $html, $matches);
|
|
$points = collect($matches[1] ?? [])
|
|
->map(fn ($x) => trim(strip_tags($x, '<strong><br>')))
|
|
->filter()
|
|
->values()
|
|
->all();
|
|
} else {
|
|
$text = preg_replace('/<br\s*\/?>/i', "\n", $html);
|
|
$text = strip_tags($text, '<strong>');
|
|
$lines = preg_split("/\r\n|\n|\r/", $text);
|
|
|
|
$points = collect($lines)
|
|
->map(fn ($x) => trim($x))
|
|
->filter()
|
|
->values()
|
|
->all();
|
|
}
|
|
|
|
return [
|
|
'title' => $title,
|
|
'points' => $points,
|
|
'asterisc' => $asterisc,
|
|
];
|
|
}
|
|
|
|
/**
|
|
* Build Items "cahier de charge" cards
|
|
*@param array $data infos from data field
|
|
*/
|
|
public static function buildItems(array $data): array
|
|
{
|
|
|
|
// collect des sections par index (1..N)
|
|
$indexes = collect(array_keys($data))
|
|
->filter(fn ($k) => is_string($k) && str_starts_with($k, 'category_section_'))
|
|
->map(fn ($k) => (int) str_replace('category_section_', '', $k))
|
|
->filter(fn ($i) => $i > 0)
|
|
->unique()
|
|
->sort()
|
|
->values();
|
|
|
|
// transforme to "rows"
|
|
$rows = $indexes->map(function (int $i) use ($data) {
|
|
$categoryKey = self::value($data["category_section_$i"] ?? '');
|
|
|
|
$titleArr = $data["title_section_$i"] ?? null;
|
|
$descArr = $data["description_section_$i"] ?? null;
|
|
$pictoArr = $data["picto_section_$i"] ?? null;
|
|
|
|
$title = self::value($titleArr);
|
|
$desc = self::value($descArr);
|
|
$picto = self::value($pictoArr);
|
|
|
|
return [
|
|
'index' => $i,
|
|
'categoryKey' => $categoryKey,
|
|
'categoryLabel' => self::value($data[$categoryKey] ?? $categoryKey),
|
|
'sectionTitle' => $title,
|
|
'descriptionHtml' => $desc,
|
|
'iconPath' => $picto,
|
|
'iconAlt' => self::imgAlt($pictoArr) ?: $title,
|
|
'iconDesc' => self::imgTitle($pictoArr) ?: $title,
|
|
];
|
|
})
|
|
->filter(fn ($r) => $r['categoryKey'] !== '' && $r['sectionTitle'] !== '')
|
|
->values();
|
|
|
|
$groupedByTab = $rows->groupBy('categoryKey');
|
|
|
|
// build final $items
|
|
$items = [];
|
|
|
|
foreach ($groupedByTab as $tabKey => $sections) {
|
|
$tabLabel = self::value($data[$tabKey] ?? $tabKey);
|
|
$slugKey = Str::slug($tabLabel, '_');
|
|
if ($slugKey === '') $slugKey = $tabKey;
|
|
|
|
$items[$slugKey] = $sections
|
|
->sortBy('index')
|
|
->map(function ($s) {
|
|
$content = self::extractContent($s['descriptionHtml']);
|
|
|
|
return [
|
|
'category' => $s['sectionTitle'],
|
|
'icon' => [
|
|
'path' => $s['iconPath'],
|
|
'alt' => $s['iconAlt'],
|
|
'description' => $s['iconDesc'],
|
|
],
|
|
'content' => [
|
|
array_filter([
|
|
'title' => $content['title'],
|
|
'points' => $content['points'],
|
|
'asterisc' => $content['asterisc']
|
|
])
|
|
],
|
|
'class' => 'card-template-sm',
|
|
];
|
|
})
|
|
->values()
|
|
->all();
|
|
}
|
|
|
|
return $items;
|
|
}
|
|
} |