mirror of
https://github.com/Combodo/iTop.git
synced 2026-04-23 10:38:45 +02:00
N°8771 - Add Symfony form component to iTop core
- IO
This commit is contained in:
@@ -6,81 +6,166 @@
|
||||
|
||||
namespace Combodo\iTop\Forms\Block;
|
||||
|
||||
use Combodo\iTop\Forms\Block\IO\FormInput;
|
||||
use Combodo\iTop\Forms\Block\IO\FormOutput;
|
||||
|
||||
/**
|
||||
* Abstract form block.
|
||||
*
|
||||
* A form block describe a form (complex or simple type).
|
||||
* A complex form have sub blocks.
|
||||
* It defines its inputs and outputs.
|
||||
*
|
||||
*/
|
||||
abstract class AbstractFormBlock
|
||||
{
|
||||
/** @var string form block name */
|
||||
private string $sName;
|
||||
|
||||
/** @var array form block options */
|
||||
private array $aOptions = [];
|
||||
|
||||
/** @var array form sub blocks */
|
||||
private array $aSubFormBlocks = [];
|
||||
|
||||
/** @var array form block inputs */
|
||||
private array $aFormInputs = [];
|
||||
|
||||
/** @var array form block outputs */
|
||||
private array $aFormOutputs = [];
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*
|
||||
* @param string $sName
|
||||
* @param array $aOptions
|
||||
*/
|
||||
public function __construct(string $sName, array $aOptions = [])
|
||||
{
|
||||
$this->sName = $sName;
|
||||
$this->aOptions = $aOptions;
|
||||
|
||||
$this->aOptions['form_block'] = $this;
|
||||
|
||||
$this->InitInputs();
|
||||
$this->InitOutputs();
|
||||
}
|
||||
|
||||
public function GetName(){
|
||||
/**
|
||||
* Return the form block name.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function GetName(): string
|
||||
{
|
||||
return $this->sName;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the form block options.
|
||||
* Options will be passed to FormType for building.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function GetOptions(): array
|
||||
{
|
||||
return $this->aOptions;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a sub form.
|
||||
*
|
||||
* @param AbstractFormBlock $oSubFormBlock
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function AddSubFormBlock(AbstractFormBlock $oSubFormBlock): AbstractFormBlock
|
||||
{
|
||||
$this->aSubFormBlocks[] = $oSubFormBlock;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the sub forms.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function GetSubFormBlocks(): array
|
||||
{
|
||||
return $this->aSubFormBlocks;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add an input declaration.
|
||||
*
|
||||
* @param FormInput $oFormInput
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function AddInput(FormInput $oFormInput): void
|
||||
{
|
||||
$this->aFormInputs[$oFormInput->GetName()] = $oFormInput;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get an input declaration.
|
||||
*
|
||||
* @param string $sName
|
||||
*
|
||||
* @return FormInput
|
||||
* @throws FormBlockException
|
||||
*/
|
||||
public function GetInput(string $sName): FormInput
|
||||
{
|
||||
if(!array_key_exists($sName, $this->aFormInputs)) {
|
||||
throw new FormBlockException('Missing input ' . $sName . ' for ' . $this->sName);
|
||||
}
|
||||
|
||||
return $this->aFormInputs[$sName];
|
||||
}
|
||||
|
||||
/**
|
||||
* Add an output declaration.
|
||||
*
|
||||
* @param FormOutput $oFormOutput
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function AddOutput(FormOutput $oFormOutput): void
|
||||
{
|
||||
$this->aFormOutputs[$oFormOutput->GetName()] = $oFormOutput;
|
||||
}
|
||||
|
||||
public function GetOutput(string $sName): ?FormOutput
|
||||
/**
|
||||
* Get an output declaration.
|
||||
*
|
||||
* @param string $sName
|
||||
*
|
||||
* @return FormOutput
|
||||
* @throws FormBlockException
|
||||
*/
|
||||
public function GetOutput(string $sName): FormOutput
|
||||
{
|
||||
if(!array_key_exists($sName, $this->aFormOutputs)) {
|
||||
throw new FormBlockException('Missing ouput ' . $sName . ' for ' . $this->sName);
|
||||
}
|
||||
|
||||
return $this->aFormOutputs[$sName];
|
||||
}
|
||||
|
||||
/**
|
||||
* Attach an input to a block output.
|
||||
*
|
||||
* @param string $sInputName
|
||||
* @param string $sOutputBlockName
|
||||
* @param string $sOutputName
|
||||
*
|
||||
* @return $this
|
||||
* @throws \Combodo\iTop\Forms\Block\FormBlockException
|
||||
* @throws FormBlockException
|
||||
*/
|
||||
public function DependsOn(string $sInputName, string $sOutputBlockName, string $sOutputName): AbstractFormBlock
|
||||
{
|
||||
$oFormInput = $this->GetInput($sInputName);
|
||||
if (is_null($oFormInput)) {
|
||||
throw new FormBlockException('Missing input ' . $sInputName . ' for ' . $this->sName);
|
||||
}
|
||||
$oFormInput->Connect($sOutputBlockName, $sOutputName);
|
||||
|
||||
return $this;
|
||||
@@ -99,7 +184,7 @@ abstract class AbstractFormBlock
|
||||
public function GetConnections(): array
|
||||
{
|
||||
$aConnections = [];
|
||||
/** @var \Combodo\iTop\Forms\Block\FormInput $oFormInput */
|
||||
/** @var FormInput $oFormInput */
|
||||
foreach ($this->aFormInputs as $oFormInput) {
|
||||
if ($oFormInput->HasConnections()) {
|
||||
$aConnections[$oFormInput->GetName()] = $oFormInput->GetConnections();
|
||||
@@ -108,8 +193,31 @@ abstract class AbstractFormBlock
|
||||
return $aConnections;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the form type.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
abstract public function GetFormType(): string;
|
||||
|
||||
/**
|
||||
* Initialize inputs.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
abstract public function InitInputs(): void;
|
||||
|
||||
/**
|
||||
* Initialize outputs.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
abstract public function InitOutputs(): void;
|
||||
|
||||
/**
|
||||
* Initialize options.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
abstract public function InitOptions(): void;
|
||||
}
|
||||
Reference in New Issue
Block a user