Logging\ErrorMsg fix for jump target list return array

This commit is contained in:
2023-10-02 17:33:49 +09:00
parent f414224c54
commit 64e60d97e7
2 changed files with 45 additions and 14 deletions

View File

@@ -17,8 +17,8 @@ class ErrorMessage
{ {
/** @var array<int,array{id:string,level:string,str:string,target:string,target_style:string,highlight:string[]}> */ /** @var array<int,array{id:string,level:string,str:string,target:string,target_style:string,highlight:string[]}> */
private array $error_str = []; private array $error_str = [];
/** @var array<string,string> */ /** @var array<string,array{info:string,level:string}> */
private array $jump_targets; private array $jump_targets = [];
/** @var \CoreLibs\Logging\Logging $log */ /** @var \CoreLibs\Logging\Logging $log */
public \CoreLibs\Logging\Logging $log; public \CoreLibs\Logging\Logging $log;
@@ -112,7 +112,7 @@ class ErrorMessage
'highlight' => $highlight, 'highlight' => $highlight,
]; ];
// set a jump target // set a jump target
$this->setJumpTarget($jump_target['target'] ?? null, $jump_target['info'] ?? null); $this->setJumpTarget($jump_target['target'] ?? null, $jump_target['info'] ?? null, $level);
// write to log for abort/crash // write to log for abort/crash
switch ($level) { switch ($level) {
case 'notice': case 'notice':
@@ -202,15 +202,18 @@ class ErrorMessage
* *
* @param string|null $target * @param string|null $target
* @param string|null $info * @param string|null $info
* @param string $level [='error']
* @return void * @return void
*/ */
public function setJumpTarget( public function setJumpTarget(
?string $target, ?string $target,
?string $info, ?string $info,
string $level = 'error',
): void { ): void {
if ( if (
empty($target) || empty($target) ||
!empty($this->jump_targets[$target]) array_key_exists($target, $this->jump_targets)
// !empty($this->jump_targets[$target])
// also check if this is an alphanumeric string? css id compatible? // also check if this is an alphanumeric string? css id compatible?
) { ) {
return; return;
@@ -218,7 +221,11 @@ class ErrorMessage
if (empty($info)) { if (empty($info)) {
$info = 'Jump to: ' . $target; $info = 'Jump to: ' . $target;
} }
$this->jump_targets[$target] = $info; $level = MessageLevel::fromName($level)->name;
$this->jump_targets[$target] = [
'info' => $info,
'level' => $level,
];
} }
// ********************************************************************* // *********************************************************************
@@ -266,11 +273,21 @@ class ErrorMessage
/** /**
* Return the jump target list * Return the jump target list
* *
* @return array{}|array{string,string} List of jump targets with info text, or empty array if not set * @return array{}|array<int,array{target:string,info:string,level:string}> List of jump targets with info text,
* or empty array if not set
*/ */
public function getJumpTarget(): array public function getJumpTarget(): array
{ {
return $this->jump_targets ?? []; $_jump_target = [];
foreach ($this->jump_targets as $target => $jump) {
$_jump_target[] = array_merge(
$jump,
[
'target' => $target,
]
);
}
return $_jump_target;
} }
// ********************************************************************* // *********************************************************************

View File

@@ -444,7 +444,7 @@ final class CoreLibsLoggingErrorMessagesTest extends TestCase
); );
$this->assertEquals( $this->assertEquals(
[ [
'target-f' => 'Target text' ['target' => 'target-f', 'info' => 'Target text', 'level' => 'error']
], ],
$em->getJumpTarget() $em->getJumpTarget()
); );
@@ -455,7 +455,7 @@ final class CoreLibsLoggingErrorMessagesTest extends TestCase
); );
$this->assertEquals( $this->assertEquals(
[ [
'target-f' => 'Target text' ['target' => 'target-f', 'info' => 'Target text', 'level' => 'error']
], ],
$em->getJumpTarget() $em->getJumpTarget()
); );
@@ -466,8 +466,8 @@ final class CoreLibsLoggingErrorMessagesTest extends TestCase
); );
$this->assertEquals( $this->assertEquals(
[ [
'target-f' => 'Target text', ['target' => 'target-f', 'info' => 'Target text', 'level' => 'error'],
'target-s' => 'More text' ['target' => 'target-s', 'info' => 'More text', 'level' => 'error'],
], ],
$em->getJumpTarget() $em->getJumpTarget()
); );
@@ -478,9 +478,23 @@ final class CoreLibsLoggingErrorMessagesTest extends TestCase
); );
$this->assertEquals( $this->assertEquals(
[ [
'target-f' => 'Target text', ['target' => 'target-f', 'info' => 'Target text', 'level' => 'error'],
'target-s' => 'More text', ['target' => 'target-s', 'info' => 'More text', 'level' => 'error'],
'target-e' => 'Jump to: target-e' ['target' => 'target-e', 'info' => 'Jump to: target-e', 'level' => 'error'],
],
$em->getJumpTarget()
);
// add through message
$em->setErrorMsg('E-101', 'abort', 'Abort message', jump_target:[
'target' => 'abort-target',
'info' => 'Abort error'
]);
$this->assertEquals(
[
['target' => 'target-f', 'info' => 'Target text', 'level' => 'error'],
['target' => 'target-s', 'info' => 'More text', 'level' => 'error'],
['target' => 'target-e', 'info' => 'Jump to: target-e', 'level' => 'error'],
['target' => 'abort-target', 'info' => 'Abort error', 'level' => 'abort'],
], ],
$em->getJumpTarget() $em->getJumpTarget()
); );