From 71c9fd401dc916b357acc978c6c4859bac25626c Mon Sep 17 00:00:00 2001 From: Clemens Schwaighofer Date: Mon, 2 Oct 2023 14:04:59 +0900 Subject: [PATCH] Logging\ErrorMsg add jump target list --- src/Logging/ErrorMessage.php | 50 +++++++++++++++ .../CoreLibsLoggingErrorMessagesTest.php | 64 +++++++++++++++++++ 2 files changed, 114 insertions(+) diff --git a/src/Logging/ErrorMessage.php b/src/Logging/ErrorMessage.php index acd6cf8..332cd01 100644 --- a/src/Logging/ErrorMessage.php +++ b/src/Logging/ErrorMessage.php @@ -17,6 +17,8 @@ class ErrorMessage { /** @var array */ private array $error_str = []; + /** @var array */ + private array $jump_targets; /** @var \CoreLibs\Logging\Logging $log */ public \CoreLibs\Logging\Logging $log; @@ -61,6 +63,8 @@ class ErrorMessage * highlight is a list of other target points to highlight * for highlight targets css names are $level without a prefix and should be * nested in the target element "input .error { ... }" + * jump_target: a target id for to jump and message, is stored in separate jump array + * where the target is unique, first one set is used for info message * target_style: if not set uses 'error-' $level as css style. applies to targets or main only * * @param string $error_id Any internal error ID for this error @@ -70,6 +74,9 @@ class ErrorMessage * @param string $target_style Alternate color style for the error message * @param array $highlight Any additional error data as error OR * highlight points for field highlights + * @param array{}|array{target:string,info?:string} $jump_target with "target" for where to jump and + * "info" for string to show in jump list + * target must be set, if info not set, default message used * @param string|null $message If abort/crash, non localized $str * @param array $context Additionl info for abort/crash messages * @param bool|null $log_error [=null] log level 'error' to error, if null use global, @@ -82,6 +89,7 @@ class ErrorMessage string $target = '', string $target_style = '', array $highlight = [], + array $jump_target = [], ?string $message = null, array $context = [], ?bool $log_error = null, @@ -103,6 +111,8 @@ class ErrorMessage 'target_style' => $target_style, 'highlight' => $highlight, ]; + // set a jump target + $this->setJumpTarget($jump_target['target'] ?? null, $jump_target['info'] ?? null); // write to log for abort/crash switch ($level) { case 'notice': @@ -152,6 +162,9 @@ class ErrorMessage * @param string $target_style Alternate color style for the error message * @param array $highlight Any additional error data as error OR * highlight points for field highlights + * @param array{}|array{target:string,info?:string} $jump_target with "target" for where to jump and + * "info" for string to show in jump list + * target must be set, if info not set, default message used * @param string|null $message If abort/crash, non localized $str * @param array $context Additionl info for abort/crash messages * @param bool|null $log_error [=null] log level 'error' to error, if null use global, @@ -164,6 +177,7 @@ class ErrorMessage string $target = '', string $target_style = '', array $highlight = [], + array $jump_target = [], ?string $message = null, array $context = [], ?bool $log_error = null, @@ -175,12 +189,38 @@ class ErrorMessage $target, $target_style, $highlight, + $jump_target, $message, $context, $log_error ); } + /** + * Set a jump target. This can be used to jump directly a frontend html block + * with the target id set + * + * @param string|null $target + * @param string|null $info + * @return void + */ + public function setJumpTarget( + ?string $target, + ?string $info, + ): void { + if ( + empty($target) || + !empty($this->jump_targets[$target]) + // also check if this is an alphanumeric string? css id compatible? + ) { + return; + } + if (empty($info)) { + $info = 'Jump to: ' . $target; + } + $this->jump_targets[$target] = $info; + } + // ********************************************************************* // GETTERS // ********************************************************************* @@ -223,6 +263,16 @@ class ErrorMessage ]; } + /** + * Return the jump target list + * + * @return array{}|array{string,string} List of jump targets with info text, or empty array if not set + */ + public function getJumpTarget(): array + { + return $this->jump_targets ?? []; + } + // ********************************************************************* // FLAG SETTERS // ********************************************************************* diff --git a/test/phpunit/Logging/CoreLibsLoggingErrorMessagesTest.php b/test/phpunit/Logging/CoreLibsLoggingErrorMessagesTest.php index 3393415..8f543cb 100644 --- a/test/phpunit/Logging/CoreLibsLoggingErrorMessagesTest.php +++ b/test/phpunit/Logging/CoreLibsLoggingErrorMessagesTest.php @@ -421,6 +421,70 @@ final class CoreLibsLoggingErrorMessagesTest extends TestCase ); } } + + /** + * Undocumented function + * + * @testdox Test jump target set and reporting + * + * @return void + */ + public function testJumpTarget(): void + { + $log = new \CoreLibs\Logging\Logging([ + 'log_file_id' => 'testErrorMessagesLogDebug', + 'log_folder' => self::LOG_FOLDER, + 'log_level' => Level::Debug, + 'log_per_run' => true + ]); + $em = new \CoreLibs\Logging\ErrorMessage($log); + $em->setJumpTarget( + 'target-f', + 'Target text' + ); + $this->assertEquals( + [ + 'target-f' => 'Target text' + ], + $em->getJumpTarget() + ); + // set same target, keep as before + $em->setJumpTarget( + 'target-f', + 'Other text' + ); + $this->assertEquals( + [ + 'target-f' => 'Target text' + ], + $em->getJumpTarget() + ); + // add new now two messages + $em->setJumpTarget( + 'target-s', + 'More text' + ); + $this->assertEquals( + [ + 'target-f' => 'Target text', + 'target-s' => 'More text' + ], + $em->getJumpTarget() + ); + // add empty info + $em->setJumpTarget( + 'target-e', + '' + ); + $this->assertEquals( + [ + 'target-f' => 'Target text', + 'target-s' => 'More text', + 'target-e' => 'Jump to: target-e' + ], + $em->getJumpTarget() + ); + } } // __END__