* @since 3.0.0 */ class FieldUIBlockFactory extends AbstractUIBlockFactory { /** @inheritDoc */ public const TWIG_TAG_NAME = 'UIField'; /** @inheritDoc */ public const UI_BLOCK_CLASS_NAME = Field::class; /** * @api * @param $aParams * * @return \Combodo\iTop\Application\UI\Base\Component\Field\Field */ public static function MakeFromParams($aParams) { $oValue = new Html($aParams['value']); $oField = new Field($aParams['label'], $oValue); $aParamsMapping = [ 'layout' => 'SetLayout', 'attcode' => 'SetAttCode', 'atttype' => 'SetAttType', 'attlabel' => 'SetAttLabel', 'value_raw' => 'SetValueRaw', 'comments' => 'SetComments', 'input_id' => 'SetInputId', 'input_type' => 'SetInputType', ]; foreach ($aParamsMapping as $sParamKey => $sFieldMethod) { self::UpdateFieldFromParams($oField, $sFieldMethod, $aParams, $sParamKey); } if (isset($aParams['attflags'])) { $aParamsFlagsMapping = [ OPT_ATT_HIDDEN => 'SetIsHidden', OPT_ATT_READONLY => 'SetIsReadOnly', OPT_ATT_MANDATORY => 'SetIsMandatory', OPT_ATT_MUSTCHANGE => 'SetMustChange', OPT_ATT_MUSTPROMPT => 'SetMustPrompt', OPT_ATT_SLAVE => 'SetIsSlave', ]; foreach ($aParamsFlagsMapping as $sConstant => $sFieldMethod) { self::UpdateFlagsFieldFromParams($oField, $sFieldMethod, $aParams['attflags'], $sConstant); } } return $oField; } private static function UpdateFieldFromParams($oField, $sMethodName, $aParams, $sKey): void { if (isset($aParams[$sKey])) { $oField->$sMethodName($aParams[$sKey]); } } private static function UpdateFlagsFieldFromParams($oField, $sMethodName, $iParamsFlags, $iConstant): void { $oField->$sMethodName((($iParamsFlags & $iConstant) === $iConstant)); } /** * @api * @param string $sLabel * @param \Combodo\iTop\Application\UI\Base\UIBlock $oInput * @param string|null $sLayout * * @return \Combodo\iTop\Application\UI\Base\Component\Field\Field */ public static function MakeFromObject(string $sLabel, UIBlock $oInput, ?string $sLayout = null) { $oField = new Field($sLabel, $oInput); if (!is_null($sLayout)) { $oField->SetLayout($sLayout); } return $oField; } /** * @api * @param string $sLabel * @param string $sValueHtml * * @return \Combodo\iTop\Application\UI\Base\Component\Field\Field */ public static function MakeLarge(string $sLabel, string $sValueHtml = '') { $oField = new Field($sLabel, new Html($sValueHtml)); $oField->SetLayout(Field::ENUM_FIELD_LAYOUT_LARGE); return $oField; } /** * @api * @param string $sLabel * @param string $sValueHtml * * @return \Combodo\iTop\Application\UI\Base\Component\Field\Field */ public static function MakeSmall(string $sLabel, string $sValueHtml = '') { $oField = new Field($sLabel, new Html($sValueHtml)); $oField->SetLayout(Field::ENUM_FIELD_LAYOUT_SMALL); return $oField; } /** * @api * @param string $sLabel * @param string $sLayout * @param string|null $sId * * @return \Combodo\iTop\Application\UI\Base\Component\Field\Field */ public static function MakeStandard(string $sLabel = '', string $sLayout = Field::ENUM_FIELD_LAYOUT_SMALL, ?string $sId = null) { $oField = new Field($sLabel, null, $sId); $oField->SetLayout($sLayout); return $oField; } }