Files
iTop/sources/Forms/Block/IO/FormOutput.php
2025-10-24 14:46:59 +02:00

91 lines
1.9 KiB
PHP

<?php
/*
* @copyright Copyright (C) 2010-2025 Combodo SARL
* @license http://opensource.org/licenses/AGPL-3.0
*/
namespace Combodo\iTop\Forms\Block\IO;
use Combodo\iTop\Forms\Converter\AbstractOutputConverter;
use Symfony\Component\Form\FormEvents;
class FormOutput extends AbstractFormIO
{
private string $sName;
private string $sType;
private null|AbstractOutputConverter $oConverter;
private array $aValues = [];
public function __construct(string $sName, string $sType, AbstractOutputConverter $oConverter = null)
{
$this->sName = $sName;
$this->sType = $sType;
$this->oConverter = $oConverter;
}
public function GetName(): string
{
return $this->sName;
}
public function SetName(string $sName): void
{
$this->sName = $sName;
}
public function GetType(): string
{
return $this->sType;
}
public function SetType(string $sType): void
{
$this->sType = $sType;
}
public function ConvertValue(mixed $oData): mixed
{
if (is_null($this->oConverter)) {
return $oData;
}
return $this->oConverter->Convert($oData);
}
public function UpdateOutputValue(mixed $oData, string $sEventType): void
{
$this->aValues[$sEventType] = $this->ConvertValue($oData);
}
public function GetValue(string $sEventType): mixed
{
return $this->aValues[$sEventType] ?? null;
}
public function Value(): mixed
{
if(array_key_exists(FormEvents::POST_SUBMIT, $this->aValues) ){
return $this->aValues[FormEvents::POST_SUBMIT];
}
if(array_key_exists(FormEvents::POST_SET_DATA, $this->aValues) ){
return $this->aValues[FormEvents::POST_SET_DATA];
}
return null;
}
public function HasValue(string $sEventType): bool
{
return array_key_exists($sEventType, $this->aValues) && $this->aValues[$sEventType] !== null;
}
public function HasValues(): bool
{
return count($this->aValues) > 0;
}
public function GetValues(): array
{
return $this->aValues;
}
}