[^\]]+)]]/"; // Outputs const OUTPUT_RESULT = "result"; const OUTPUT_RESULT_INVERT = "result_invert"; /** @inheritdoc */ protected function RegisterIO(IORegister $oIORegister): void { parent::RegisterIO($oIORegister); $oIORegister->AddOutput(self::OUTPUT_RESULT, BooleanIOFormat::class); $oIORegister->AddOutput(self::OUTPUT_RESULT_INVERT, BooleanIOFormat::class); } /** @inheritdoc */ public function AllInputsReadyEvent(): void { parent::AllInputsReadyEvent(); $this->ComputeExpression(FormEvents::POST_SET_DATA); $this->ComputeExpression(FormEvents::POST_SUBMIT); } /** * Compute the expression and set the output values. * * @param string $sEventType * * @return void */ public function ComputeExpression(string $sEventType): void { try{ $sExpression = $this->GetOption('expression'); $sValue = preg_replace_callback( self::EXPRESSION_PATTERN, function(array $aMatches) use ($sEventType): ?string { $oInput = $this->GetInput($aMatches['input']); if(!$oInput->HasEventValue($sEventType)){ throw new FormsException('Unable to compute expression: input '.$aMatches['input'].' has no value for event '.$sEventType.'.'); } return $oInput->GetValue($sEventType); }, $sExpression); $result = ''; eval('$result = '.$sValue.';'); $this->GetOutput(self::OUTPUT_RESULT)->SetValue($sEventType, new BooleanIOFormat($result)); $this->GetOutput(self::OUTPUT_RESULT_INVERT)->SetValue($sEventType, new BooleanIOFormat(!$result)); $this->GetOutput(self::OUTPUT_VALUE)->SetValue($sEventType, new RawFormat($result)); } catch(\Exception $e){ IssueLog::Exception('Compute expression block issue', $e); } } }