refactor solution to PHP through AI

This commit is contained in:
2026-07-20 14:27:17 +02:00
parent a77f6f19cf
commit 151bc70775
21 changed files with 2563 additions and 41 deletions
+610
View File
@@ -0,0 +1,610 @@
<?php
declare(strict_types=1);
namespace ElixForms\Common;
require_once __DIR__ . '/QueryParamHelper.php';
require_once __DIR__ . '/ElixFormsElement.php';
/**
* Classe astratta base per le custom page PHP di elixForms.
* Equivalente PHP di ElixFormsComponentAbstract.tsx nel progetto React.
*
* Ogni pagina custom deve:
* 1. Estendere questa classe
* 2. Sovrascrivere createCustomFormFields() per definire i campi del form
* 3. Opzionalmente sovrascrivere renderExtraContentPre() / renderExtraContentPost()
* 4. Chiamare render() per generare l'HTML completo della pagina
*
* Uso tipico:
* ```php
* $page = new MiaPaginaCustom([
* 'moduleName' => 'Nome Modulo',
* 'cardTitle' => 'Titolo Card',
* // ...altre proprietà...
* ]);
* echo $page->render();
* ```
*/
abstract class ElixFormsComponent
{
/**
* Proprietà della pagina (equivalente a IElixFormsComponentProperties).
*
* Chiavi supportate:
* - isDarkTheme: bool (opzionale)
* - userDisplayName: string (opzionale)
* - additionalFieldsJson: string (opzionale, JSON schema per campi dinamici)
* - moduleName: string (obbligatorio)
* - headerTitle: string (opzionale)
* - headerHeroImageSrc: string (opzionale)
* - cardTitle: string (obbligatorio)
* - cardDescription: string (opzionale)
* - alertInfoTitle: string (opzionale)
* - alertInfoMessage: string (opzionale)
* - instructionsTitle: string (opzionale)
* - instructionsMessage: string (opzionale)
* - submitText: string (opzionale, default 'INVIA')
*
* @var array<string, mixed>
*/
protected array $properties;
/**
* Nomi dei parametri obbligatori che elixForms passa in query string.
* Se mancano, la pagina mostra un errore di autenticazione.
*
* @var string[]
*/
private array $mandatoryFormFieldNames = [
'RWE2_MODULE_ID',
'RWE2_REQUEST_ID',
'custom-workflow-back-url',
'custom-workflow-generic-id',
'custom-workflow-current-tabrel-genid',
'custom-workflow-source-field',
'crc',
'MODULE_TESTMODE_KEY',
'ELANG',
];
/**
* @param array<string, mixed> $properties Proprietà della pagina
*/
public function __construct(array $properties)
{
$this->properties = $properties;
}
// =========================================================================
// Metodi estensibili (da sovrascrivere nelle pagine figlie)
// =========================================================================
/**
* Genera i campi custom del form.
* Da sovrascrivere nelle pagine figlie per definire i campi specifici.
*
* @return string HTML dei campi custom
*/
protected function createCustomFormFields(): string
{
return '';
}
/**
* Contenuto extra da renderizzare PRIMA del form.
* Sovrascrivere per aggiungere contenuto personalizzato.
*
* @return string HTML del contenuto extra
*/
protected function renderExtraContentPre(): string
{
return '';
}
/**
* Contenuto extra da renderizzare DOPO il form.
* Sovrascrivere per aggiungere contenuto personalizzato.
*
* @return string HTML del contenuto extra
*/
protected function renderExtraContentPost(): string
{
return '';
}
// =========================================================================
// Metodi factory per campi form
// =========================================================================
/**
* Crea un campo di testo (input type="text").
*
* @param string $paramName Nome/ID del campo (corrisponde alla colonna elixForms)
* @param string $label Etichetta del campo
* @param bool $required Se il campo è obbligatorio
* @return string HTML del campo
*/
protected function createTextInput(string $paramName, string $label, bool $required = false): string
{
$currentValue = htmlspecialchars(QueryParamHelper::getDecodedTextFromQuery($paramName) ?? '', ENT_QUOTES, 'UTF-8');
$requiredAttr = $required ? ' required' : '';
$labelHtml = '<label class="form-label fw-semibold" for="' . htmlspecialchars($paramName) . '">' . htmlspecialchars($label) . '</label>';
$inputHtml = '<input id="' . htmlspecialchars($paramName) . '" name="' . htmlspecialchars($paramName) . '" class="form-control" type="text" value="' . $currentValue . '"' . $requiredAttr . ' />';
return (new ElixFormsElement($labelHtml, $inputHtml))->render();
}
/**
* Crea un campo textarea.
*
* @param string $paramName Nome/ID del campo
* @param string $label Etichetta del campo
* @param bool $required Se il campo è obbligatorio
* @return string HTML del campo
*/
protected function createTextAreaInput(string $paramName, string $label, bool $required = false): string
{
$currentValue = htmlspecialchars(QueryParamHelper::getDecodedTextFromQuery($paramName) ?? '', ENT_QUOTES, 'UTF-8');
$requiredAttr = $required ? ' required' : '';
$labelHtml = '<label class="form-label fw-semibold" for="' . htmlspecialchars($paramName) . '">' . htmlspecialchars($label) . '</label>';
$inputHtml = '<textarea id="' . htmlspecialchars($paramName) . '" name="' . htmlspecialchars($paramName) . '" class="form-control" rows="4"' . $requiredAttr . '>' . $currentValue . '</textarea>';
return (new ElixFormsElement($labelHtml, $inputHtml))->render();
}
/**
* Crea un campo numerico (input type="number").
*
* @param string $paramName Nome/ID del campo
* @param string $label Etichetta del campo
* @param bool $required Se il campo è obbligatorio
* @return string HTML del campo
*/
protected function createNumberInput(string $paramName, string $label, bool $required = false): string
{
$currentValue = htmlspecialchars(QueryParamHelper::getDecodedTextFromQuery($paramName) ?? '', ENT_QUOTES, 'UTF-8');
$requiredAttr = $required ? ' required' : '';
$labelHtml = '<label class="form-label fw-semibold" for="' . htmlspecialchars($paramName) . '">' . htmlspecialchars($label) . '</label>';
$inputHtml = '<input id="' . htmlspecialchars($paramName) . '" name="' . htmlspecialchars($paramName) . '" class="form-control" type="number" value="' . $currentValue . '"' . $requiredAttr . ' />';
return (new ElixFormsElement($labelHtml, $inputHtml))->render();
}
/**
* Crea un campo booleano (radio Sì/No).
*
* @param string $paramName Nome/ID del campo
* @param string $label Etichetta del campo
* @param bool $required Se il campo è obbligatorio
* @return string HTML del campo
*/
protected function createBooleanInput(string $paramName, string $label, bool $required = false): string
{
$currentValue = QueryParamHelper::getDecodedTextFromQuery($paramName) ?? '';
$requiredAttr = $required ? ' required' : '';
$escapedName = htmlspecialchars($paramName);
$escapedLabel = htmlspecialchars($label);
$labelHtml = '<span class="form-label fw-semibold">' . $escapedLabel . '</span>';
$options = [
['value' => 'true', 'label' => 'Sì'],
['value' => 'false', 'label' => 'No'],
];
$inputHtml = '<div class="d-flex flex-column gap-2" role="radiogroup" aria-label="' . $escapedLabel . '">';
foreach ($options as $option) {
$id = $escapedName . '_' . $option['value'];
$checked = $currentValue === $option['value'] ? ' checked' : '';
$inputHtml .= <<<HTML
<div class="form-check">
<input id="{$id}" class="form-check-input" type="radio" name="{$escapedName}" value="{$option['value']}"{$checked}{$requiredAttr} />
<label class="form-check-label" for="{$id}">{$option['label']}</label>
</div>
HTML;
}
$inputHtml .= '</div>';
return (new ElixFormsElement($labelHtml, $inputHtml))->render();
}
/**
* Crea un campo radio con opzioni personalizzate.
*
* @param string $paramName Nome/ID del campo
* @param string $label Etichetta del campo
* @param array<array{value: int, label: string}> $options Opzioni radio
* @param bool $required Se il campo è obbligatorio
* @return string HTML del campo
*/
protected function createRadioInput(string $paramName, string $label, array $options, bool $required = false): string
{
$currentValue = (string)(QueryParamHelper::getOptionFromQuery($paramName) ?? '');
$requiredAttr = $required ? ' required' : '';
$escapedName = htmlspecialchars($paramName);
$escapedLabel = htmlspecialchars($label);
$labelHtml = '<span class="form-label fw-semibold">' . $escapedLabel . '</span>';
$inputHtml = '<div class="d-flex flex-column gap-2" role="radiogroup" aria-label="' . $escapedLabel . '">';
foreach ($options as $option) {
$id = $escapedName . '_' . $option['value'];
$checked = $currentValue === (string)$option['value'] ? ' checked' : '';
$optionLabel = htmlspecialchars($option['label']);
$inputHtml .= <<<HTML
<div class="form-check">
<input id="{$id}" class="form-check-input" type="radio" name="{$escapedName}" value="{$option['value']}"{$checked}{$requiredAttr} />
<label class="form-check-label" for="{$id}">{$optionLabel}</label>
</div>
HTML;
}
$inputHtml .= '</div>';
return (new ElixFormsElement($labelHtml, $inputHtml))->render();
}
/**
* Crea un campo checkbox con opzioni multiple.
* I valori selezionati vengono inviati come stringa separata da virgola in un input hidden.
*
* @param string $paramName Nome/ID del campo (usato per l'hidden input)
* @param string $label Etichetta del campo
* @param array<array{value: int, label: string, required?: bool}> $options Opzioni checkbox
* @return string HTML del campo
*/
protected function createCheckboxInput(string $paramName, string $label, array $options): string
{
$checkedValues = QueryParamHelper::getCheckedFromQuery($paramName);
$currentValue = implode(',', $checkedValues);
$escapedName = htmlspecialchars($paramName);
$escapedLabel = htmlspecialchars($label);
$labelHtml = '<label class="form-label fw-semibold" for="' . $escapedName . '">' . $escapedLabel . '</label>';
$inputHtml = '<div class="d-flex flex-column gap-2" role="group" aria-label="' . $escapedLabel . '">';
foreach ($options as $entryIndex => $entry) {
$id = $escapedName . '_' . $entryIndex;
$isChecked = in_array($entryIndex, $checkedValues) ? ' checked' : '';
$optionLabel = htmlspecialchars($entry['label']);
$inputHtml .= <<<HTML
<div class="form-check">
<input id="{$id}" class="form-check-input" type="checkbox" value="{$entryIndex}" data-param="{$escapedName}"{$isChecked} />
<label class="form-check-label" for="{$id}">{$optionLabel}</label>
</div>
HTML;
}
$inputHtml .= '</div>';
// Hidden input che contiene il valore aggregato (aggiornato via JS client-side)
$inputHtml .= '<input type="hidden" id="' . $escapedName . '" name="' . $escapedName . '" value="' . htmlspecialchars($currentValue) . '" />';
return (new ElixFormsElement($labelHtml, $inputHtml))->render();
}
/**
* Crea un campo dropdown (select).
*
* @param string $paramName Nome/ID del campo
* @param string $label Etichetta del campo
* @param array<array{value: int, label: string}> $options Opzioni dropdown
* @param bool $required Se il campo è obbligatorio
* @return string HTML del campo
*/
protected function createDropdownInput(string $paramName, string $label, array $options, bool $required = false): string
{
$queryValue = QueryParamHelper::getOptionFromQuery($paramName);
$currentValue = $queryValue !== null ? (string)$queryValue : '';
$requiredAttr = $required ? ' required' : '';
$escapedName = htmlspecialchars($paramName);
$escapedLabel = htmlspecialchars($label);
$labelHtml = '<label class="form-label fw-semibold" for="' . $escapedName . '">' . $escapedLabel . '</label>';
$inputHtml = '<select id="' . $escapedName . '" name="' . $escapedName . '" class="form-select"' . $requiredAttr . '>';
$inputHtml .= '<option value="">Seleziona...</option>';
foreach ($options as $option) {
$selected = $currentValue === (string)$option['value'] ? ' selected' : '';
$optionLabel = htmlspecialchars($option['label']);
$inputHtml .= '<option value="' . $option['value'] . '"' . $selected . '>' . $optionLabel . '</option>';
}
$inputHtml .= '</select>';
return (new ElixFormsElement($labelHtml, $inputHtml))->render();
}
/**
* Crea un campo nascosto (hidden input).
* L'ID dell'hidden ha il suffisso _hidden per evitare conflitti.
*
* @param string $paramName Nome del campo
* @return string HTML dell'input hidden
*/
protected function createHiddenInput(string $paramName): string
{
$value = htmlspecialchars(QueryParamHelper::getDecodedTextFromQuery($paramName) ?? '', ENT_QUOTES, 'UTF-8');
$escapedName = htmlspecialchars($paramName);
return '<input type="hidden" id="' . $escapedName . '_hidden" name="' . $escapedName . '" value="' . $value . '" />';
}
// =========================================================================
// Metodi interni di rendering
// =========================================================================
/**
* Genera gli hidden input per tutti i parametri obbligatori elixForms.
*
* @return string HTML degli hidden input
*/
private function createMandatoryFormFields(): string
{
$html = '';
foreach ($this->mandatoryFormFieldNames as $paramName) {
$html .= $this->createHiddenInput($paramName);
}
return $html;
}
/**
* Genera i campi form da uno schema JSON (additionalFieldsJson).
*
* @return string HTML dei campi aggiuntivi
*/
private function createAdditionalFormFields(): string
{
$json = $this->properties['additionalFieldsJson'] ?? '[]';
$schema = json_decode($json, true);
if (!is_array($schema)) {
return '';
}
$html = '';
foreach ($schema as $field) {
$html .= $this->renderField($field);
}
return $html;
}
/**
* Renderizza un singolo campo form in base al tipo specificato nello schema.
*
* @param array<string, mixed> $field Definizione del campo
* @return string HTML del campo
*/
private function renderField(array $field): string
{
$key = $field['key'] ?? '';
$fieldLabel = $field['label'] ?? '';
$required = $field['required'] ?? false;
$fieldOptions = $field['options'] ?? [];
return match ($field['type'] ?? '') {
'text' => $this->createTextInput($key, $fieldLabel, $required),
'textarea' => $this->createTextAreaInput($key, $fieldLabel, $required),
'number' => $this->createNumberInput($key, $fieldLabel, $required),
'boolean' => $this->createBooleanInput($key, $fieldLabel, $required),
'radio' => $this->createRadioInput($key, $fieldLabel, $fieldOptions, $required),
'checkbox' => $this->createCheckboxInput($key, $fieldLabel, $fieldOptions),
'dropdown' => $this->createDropdownInput($key, $fieldLabel, $fieldOptions, $required),
default => '',
};
}
// =========================================================================
// Render principale
// =========================================================================
/**
* Genera l'HTML completo della pagina.
* Include: head con CSS, header Bootstrap Italia, breadcrumb, card con form, footer.
*
* @return string HTML completo della pagina
*/
public function render(): string
{
$userDisplayName = $this->properties['userDisplayName'] ?? '';
$moduleName = htmlspecialchars($this->properties['moduleName'] ?? '');
$headerTitle = $this->properties['headerTitle'] ?? '';
$headerHeroImageSrc = $this->properties['headerHeroImageSrc'] ?? '';
$cardTitle = $this->properties['cardTitle'] ?? '';
$cardDescription = $this->properties['cardDescription'] ?? '';
$alertInfoTitle = $this->properties['alertInfoTitle'] ?? '';
$alertInfoMessage = $this->properties['alertInfoMessage'] ?? '';
$instructionsTitle = $this->properties['instructionsTitle'] ?? '';
$instructionsMessage = $this->properties['instructionsMessage'] ?? '';
$submitText = $this->properties['submitText'] ?? 'INVIA';
// Verifica parametri obbligatori
$missingParams = array_filter($this->mandatoryFormFieldNames, fn($p) => !isset($_GET[$p]));
$hasAuthenticationError = count($missingParams) > 0;
// Contenuti del form
$mandatoryFields = $this->createMandatoryFormFields();
$additionalFields = $this->createAdditionalFormFields();
$customFields = $this->createCustomFormFields();
$extraContentPre = $this->renderExtraContentPre();
$extraContentPost = $this->renderExtraContentPost();
// Costruisci l'HTML della pagina
ob_start();
?>
<!DOCTYPE html>
<html lang="it">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title><?= htmlspecialchars($cardTitle) ?> — elixForms</title>
<meta name="description" content="<?= htmlspecialchars($cardDescription) ?>">
<link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Titillium+Web:wght@300;400;600;700&display=swap">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.8/dist/css/bootstrap.min.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-italia@2.18.2/dist/css/bootstrap-italia.min.css">
<link rel="stylesheet" href="https://console-unipr.elixforms.it/rwe2/css/design-bs/util.css">
<link rel="stylesheet" href="https://console-unipr.elixforms.it/rwe2/css/themes/blu-italia.css">
</head>
<body>
<div>
<header class="it-header-wrapper text-white">
<?php if ($userDisplayName): ?>
<div class="it-header-slim-wrapper">
<div class="container">
<div class="row">
<div class="col-12">
<div class="it-header-slim-wrapper-content">
<div class="nav-mobile"></div>
<div class="it-header-slim-right-zone">
<div class="it-access-top-wrapper">
<?= htmlspecialchars($userDisplayName) ?>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<?php endif; ?>
<div class="it-header-center-wrapper">
<div class="container">
<div class="it-header-center-content-wrapper">
<div class="it-brand-wrapper">
<a href="https://www.unipr.it/" title="Torna alla homepage dell'Università di Parma">
<img class="icon" src="https://procedure.unipr.it/UploadImgs/12_marchio_UNIPR_neg_b1_squareLogo.png" alt="Università di Parma" />
<div class="it-brand-text">
<p class="h2 no_toc">Università di Parma</p>
</div>
</a>
</div>
<div class="it-right-zone"></div>
</div>
</div>
</div>
</header>
<div class="container my-2 mx-4">
<div class="cmp-breadcrumb" role="navigation">
<nav class="breadcrumb-container" aria-label="breadcrumb">
<ol class="breadcrumb">
<li class="breadcrumb-item">Procedure Online<span class="separator">&gt;</span></li>
<li class="breadcrumb-item active" aria-current="page"><?= $moduleName ?></li>
</ol>
</nav>
</div>
<?php if ($headerHeroImageSrc): ?>
<img src="<?= htmlspecialchars($headerHeroImageSrc) ?>" alt="Hero Image" class="hero-image img-fluid mb-3" />
<?php endif; ?>
<?php if ($headerTitle): ?>
<p class="h2"><?= htmlspecialchars($headerTitle) ?></p>
<?php endif; ?>
<div class="container">
<div class="card shadow-sm mb-4">
<div class="card-body">
<?php if ($cardTitle): ?>
<p class="h3 mb-3"><?= htmlspecialchars($cardTitle) ?></p>
<?php endif; ?>
<?php if ($cardDescription): ?>
<p class="lead text-secondary"><?= htmlspecialchars($cardDescription) ?></p>
<?php endif; ?>
<?php if ($alertInfoTitle || $alertInfoMessage): ?>
<div class="custom-alert custom-alert-warning" role="status">
<?php if ($alertInfoTitle): ?><div><b><?= htmlspecialchars($alertInfoTitle) ?></b></div><?php endif; ?>
<?php if ($alertInfoMessage): ?><div class="small"><?= htmlspecialchars($alertInfoMessage) ?></div><?php endif; ?>
</div>
<?php endif; ?>
<?php if ($hasAuthenticationError): ?>
<div class="alert alert-danger" role="alert">Accesso non autorizzato!</div>
<?php else: ?>
<?= $extraContentPre ?>
<?php if ($instructionsTitle || $instructionsMessage): ?>
<div class="custom-alert custom-alert-info" role="status">
<?php if ($instructionsTitle): ?><div><b><?= htmlspecialchars($instructionsTitle) ?></b></div><?php endif; ?>
<?php if ($instructionsMessage): ?><div class="small"><?= htmlspecialchars($instructionsMessage) ?></div><?php endif; ?>
</div>
<?php endif; ?>
<form action="https://procedure.unipr.it/rwe2/ComeBackToElixAndSave" method="post" accept-charset="ISO-8859-1" class="mt-3">
<?= $mandatoryFields ?>
<?= $additionalFields ?>
<?= $customFields ?>
<div class="mt-4 d-flex justify-content-end">
<button type="submit" class="btn btn-primary"><?= htmlspecialchars($submitText) ?> <span class="it-arrow-right text-white ms-2"></span></button>
</div>
</form>
<?= $extraContentPost ?>
<?php endif; ?>
</div>
</div>
</div>
</div>
<footer class="it-footer text-white">
<div class="it-footer-small-prints clearfix">
<div class="container">
<section>
<div class="row clearfix">
<div class="col-sm-12">
<div class="footer-content text-center p-2">
<div class="subfooter-top">powered by <span class="highlight">elixForms</span></div>
</div>
</div>
</div>
</section>
</div>
</div>
</footer>
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.8/dist/js/bootstrap.bundle.min.js"></script>
<?= $this->renderCheckboxScript() ?>
</body>
</html>
<?php
return ob_get_clean();
}
/**
* Genera il JavaScript per la gestione client-side dei checkbox.
* Aggiorna l'hidden input con i valori selezionati separati da virgola.
*
* @return string Tag <script> con il codice JS
*/
private function renderCheckboxScript(): string
{
return <<<'SCRIPT'
<script>
document.addEventListener('DOMContentLoaded', function() {
document.querySelectorAll('input[type="checkbox"][data-param]').forEach(function(checkbox) {
checkbox.addEventListener('change', function() {
var paramName = this.getAttribute('data-param');
var hiddenInput = document.getElementById(paramName);
if (!hiddenInput) return;
var checkboxes = document.querySelectorAll('input[type="checkbox"][data-param="' + paramName + '"]');
var values = [];
checkboxes.forEach(function(cb) {
if (cb.checked) {
values.push(cb.value);
}
});
hiddenInput.value = values.join(',');
});
});
});
</script>
SCRIPT;
}
}
+59
View File
@@ -0,0 +1,59 @@
<?php
declare(strict_types=1);
namespace ElixForms\Common;
/**
* Wrapper per layout label + input in un layout a due colonne Bootstrap.
* Equivalente PHP di ElixFormsElement.tsx nel progetto React.
*
* Genera markup compatibile con il design system Bootstrap Italia:
* - Colonna sinistra (col-12 col-md-4): label
* - Colonna destra (col-12 col-md-8): input
*/
class ElixFormsElement
{
private string $labelHtml;
private string $inputHtml;
private string $separator;
/**
* @param string $labelHtml HTML della label
* @param string $inputHtml HTML dell'input
* @param string $separator Separatore opzionale tra label e input
*/
public function __construct(string $labelHtml, string $inputHtml, string $separator = '')
{
if (empty($labelHtml) || empty($inputHtml)) {
throw new \InvalidArgumentException('labelHtml and inputHtml are required.');
}
$this->labelHtml = $labelHtml;
$this->inputHtml = $inputHtml;
$this->separator = $separator;
}
/**
* Genera l'HTML del campo form con layout a due colonne.
*
* @return string HTML renderizzato
*/
public function render(): string
{
$sep = $this->separator !== '' ? htmlspecialchars($this->separator) : '';
return <<<HTML
<div class="row mb-3 align-items-start">
<div class="col-12 col-md-4">
<div class="text-md-end pe-md-3 mt-2">
{$this->labelHtml}{$sep}
</div>
</div>
<div class="col-12 col-md-8">
{$this->inputHtml}
</div>
</div>
HTML;
}
}
+153
View File
@@ -0,0 +1,153 @@
<?php
declare(strict_types=1);
namespace ElixForms\Common;
/**
* Client HTTP robusto per effettuare chiamate REST esterne.
* Utilizza l'estensione cURL di PHP ed implementa best practices per
* la gestione degli header, dell'autenticazione e degli errori.
*/
class HttpClient
{
private array $defaultHeaders = [];
private ?string $username = null;
private ?string $password = null;
private int $timeout = 10;
/**
* @param array $defaultHeaders Header predefiniti per ogni richiesta
*/
public function __construct(array $defaultHeaders = [])
{
$this->defaultHeaders = $defaultHeaders;
}
/**
* Imposta le credenziali per l'autenticazione Basic.
*/
public function setBasicAuth(string $username, string $password): self
{
$this->username = $username;
$this->password = $password;
return $this;
}
/**
* Imposta il timeout massimo per la connessione e l'esecuzione.
*/
public function setTimeout(int $seconds): self
{
$this->timeout = $seconds;
return $this;
}
/**
* Esegue una richiesta HTTP GET.
*
* @param string $url URL della richiesta
* @param array $queryParams Parametri query aggiuntivi
* @param array $headers Header specifici per questa richiesta
* @return string Risposta in formato testuale
* @throws \RuntimeException In caso di errore curl o codice di stato non 2xx
*/
public function get(string $url, array $queryParams = [], array $headers = []): string
{
if (!empty($queryParams)) {
$separator = (strpos($url, '?') === false) ? '?' : '&';
$url .= $separator . http_build_query($queryParams);
}
return $this->request($url, 'GET', null, $headers);
}
/**
* Esegue una richiesta HTTP POST.
*
* @param string $url URL della richiesta
* @param mixed $data Dati da inviare nel body (array, stringa o JSON)
* @param array $headers Header specifici per questa richiesta
* @return string Risposta in formato testuale
* @throws \RuntimeException In caso di errore curl o codice di stato non 2xx
*/
public function post(string $url, $data, array $headers = []): string
{
return $this->request($url, 'POST', $data, $headers);
}
/**
* Metodo interno per eseguire la richiesta tramite cURL.
*/
private function request(string $url, string $method, $data = null, array $headers = []): string
{
$ch = curl_init();
if ($ch === false) {
throw new \RuntimeException('Impossibile inizializzare cURL.');
}
// Configura le opzioni cURL di base
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, $this->timeout);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $this->timeout);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_MAXREDIRS, 3);
// Disabilita la verifica SSL in ambiente di sviluppo locale se necessario,
// ma di default è attiva per sicurezza.
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
// Gestione metodo HTTP
$method = strtoupper($method);
if ($method === 'POST') {
curl_setopt($ch, CURLOPT_POST, true);
if ($data !== null) {
if (is_array($data)) {
$postData = http_build_query($data);
} else {
$postData = $data;
}
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
}
} elseif ($method !== 'GET') {
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
}
// Costruisci gli header
$mergedHeaders = array_merge($this->defaultHeaders, $headers);
$formattedHeaders = [];
foreach ($mergedHeaders as $name => $value) {
$formattedHeaders[] = "{$name}: {$value}";
}
if (!empty($formattedHeaders)) {
curl_setopt($ch, CURLOPT_HTTPHEADER, $formattedHeaders);
}
// Autenticazione Basic
if ($this->username !== null && $this->password !== null) {
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, "{$this->username}:{$this->password}");
}
// Esegui la richiesta
$response = curl_exec($ch);
$error = curl_error($ch);
$errno = curl_errno($ch);
$statusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
// curl_close non è più necessario in PHP 8.0+ ed è deprecato in PHP 8.5+
if ($errno !== 0) {
throw new \RuntimeException("Errore cURL durante la chiamata a {$url}: [{$errno}] {$error}");
}
if ($statusCode < 200 || $statusCode >= 300) {
throw new \RuntimeException("Richiesta fallita con codice di stato HTTP {$statusCode}.");
}
return (string)$response;
}
}
+91
View File
@@ -0,0 +1,91 @@
<?php
declare(strict_types=1);
namespace ElixForms\Common;
/**
* Utility statica per leggere parametri dalla URL corrente ($_GET).
* Equivalente PHP di QueryParamHelper.tsx nel progetto React.
*/
class QueryParamHelper
{
/**
* Ottiene un array di valori numerici dalla query string (per checkbox).
* Il parametro è una stringa di valori separati da virgola (es. "1,3,5").
*
* @param string $paramName Nome del parametro nella query string
* @return int[] Array di interi
*/
public static function getCheckedFromQuery(string $paramName): array
{
$raw = $_GET[$paramName] ?? '';
if ($raw === '') {
return [];
}
$parts = explode(',', (string)$raw);
$result = [];
foreach ($parts as $part) {
$trimmed = trim($part);
if ($trimmed !== '' && is_numeric($trimmed)) {
$result[] = (int)$trimmed;
}
}
return $result;
}
/**
* Ottiene un singolo valore numerico dalla query string (per radio/dropdown).
*
* @param string $paramName Nome del parametro nella query string
* @return int|null Valore numerico o null se non presente
*/
public static function getOptionFromQuery(string $paramName): ?int
{
if (!isset($_GET[$paramName])) {
return null;
}
$value = $_GET[$paramName];
if (!is_numeric($value)) {
return null;
}
return (int)$value;
}
/**
* Ottiene un valore booleano dalla query string.
*
* @param string $paramName Nome del parametro nella query string
* @return bool|null true/false o null se non presente
*/
public static function getBooleanFromQuery(string $paramName): ?bool
{
if (!isset($_GET[$paramName])) {
return null;
}
return strtolower((string)$_GET[$paramName]) === 'true';
}
/**
* Ottiene un valore testuale decodificato dalla query string.
*
* @param string $paramName Nome del parametro nella query string
* @return string|null Valore decodificato o null se non presente
*/
public static function getDecodedTextFromQuery(string $paramName): ?string
{
if (!isset($_GET[$paramName])) {
return null;
}
return urldecode((string)$_GET[$paramName]);
}
}