Skip to content

Commit b3bdf59

Browse files
author
rotimi
committed
Added getContainerItem(string $containerKey, mixed $fallbackValue=null): mixed to BaseErrorRendererTrait
1 parent 77f6105 commit b3bdf59

File tree

3 files changed

+46
-20
lines changed

3 files changed

+46
-20
lines changed

src/BaseErrorRendererTrait.php

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,22 +30,35 @@ public function setContainer(?\Psr\Container\ContainerInterface $newContainer):
3030
return $this;
3131
}
3232

33-
public function getLocalizedText(string $localeKey, string $fallbackText=''): string {
33+
public function getContainerItem(string $containerKey, mixed $fallbackValue=null): mixed {
3434

3535
if(
3636
$this->container instanceof \Psr\Container\ContainerInterface
37-
&& $this->container->has(ContainerKeys::LOCALE_OBJ)
38-
&& $this->container->get(ContainerKeys::LOCALE_OBJ) instanceof \Vespula\Locale\Locale
37+
&& $this->container->has($containerKey)
3938
) {
39+
return $this->container->get($containerKey);
40+
}
41+
42+
return $fallbackValue;
43+
}
44+
45+
public function getLocalizedText(string $localeKey, string $fallbackText=''): string {
46+
47+
/**
48+
* @psalm-suppress MixedAssignment
49+
*/
50+
$localeObj = $this->getContainerItem(ContainerKeys::LOCALE_OBJ);
51+
52+
if( $localeObj instanceof \Vespula\Locale\Locale ) {
53+
4054
/**
4155
* @psalm-suppress MixedAssignment
4256
* @psalm-suppress MixedMethodCall
4357
*/
44-
$localizedText = $this->container->get(ContainerKeys::LOCALE_OBJ)->gettext($localeKey);
58+
$localizedText = $localeObj->gettext($localeKey);
4559

4660
// ($localizedText === $localeKey) when $localeKey is not found in the locale object
4761
return ($localizedText === $localeKey) ? $fallbackText : $localizedText;
48-
4962
}
5063

5164
return $fallbackText;

src/LogErrorRenderer.php

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,22 +22,13 @@ public function __invoke(\Throwable $exception, bool $displayErrorDetails): stri
2222
$nl = PHP_EOL;
2323
$logErrorDetails = false;
2424
$text = "{$this->getErrorTitle($exception)}{$nl}";
25+
26+
/** @psalm-suppress MixedAssignment */
27+
$appSettings = (array)$this->getContainerItem(ContainerKeys::APP_SETTINGS, []);
2528

26-
/** @psalm-suppress PossiblyNullReference */
27-
if(
28-
($this->getContainer() instanceof ContainerInterface)
29-
&& $this->getContainer()->has(ContainerKeys::APP_SETTINGS)
30-
) {
31-
/** @psalm-suppress PossiblyNullReference */
32-
/** @psalm-suppress MixedAssignment */
33-
$appSettings = $this->getContainer()
34-
->get(ContainerKeys::APP_SETTINGS);
35-
if(
36-
\is_array($appSettings)
37-
&& \array_key_exists(AppSettingsKeys::LOG_ERROR_DETAILS, $appSettings)
38-
) {
39-
$logErrorDetails = (bool)$appSettings[AppSettingsKeys::LOG_ERROR_DETAILS];
40-
}
29+
if( \array_key_exists(AppSettingsKeys::LOG_ERROR_DETAILS, $appSettings) ) {
30+
31+
$logErrorDetails = (bool)$appSettings[AppSettingsKeys::LOG_ERROR_DETAILS];
4132
}
4233

4334
if($exception instanceof \Slim\Exception\HttpException) {

tests/HtmlErrorRendererTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,28 @@ public function testThatGetContainerAndSetContainerWorkAsExpected() {
150150
self::assertNotSame($contaner1, $html_renderer_no_template_file->getContainer());
151151
}
152152

153+
public function testThatGetContainerItemWorkAsExpected() {
154+
155+
$html_renderer_no_template_file = new \SlimMvcTools\HtmlErrorRenderer('');
156+
157+
self::assertNull($html_renderer_no_template_file->getContainerItem(ContainerKeys::LOCALE_OBJ)); // no container
158+
self::assertEquals($html_renderer_no_template_file->getContainerItem(ContainerKeys::LOCALE_OBJ, -777), -777); // no container with fallback value of -777
159+
160+
$contaner1 = $this->getContainer();
161+
162+
$html_renderer_no_template_file->setContainer($contaner1);
163+
self::assertNotNull($html_renderer_no_template_file->getContainerItem(ContainerKeys::LOCALE_OBJ));
164+
self::assertNotNull($html_renderer_no_template_file->getContainerItem(ContainerKeys::LOCALE_OBJ, -777));
165+
self::assertInstanceOf(
166+
\Vespula\Locale\Locale::class,
167+
$html_renderer_no_template_file->getContainerItem(ContainerKeys::LOCALE_OBJ)
168+
);
169+
self::assertInstanceOf(
170+
\Vespula\Locale\Locale::class,
171+
$html_renderer_no_template_file->getContainerItem(ContainerKeys::LOCALE_OBJ, -777)
172+
);
173+
}
174+
153175
public function testThatGetLocalizedTextWorkAsExpected() {
154176

155177
$html_renderer_no_template_file = new \SlimMvcTools\HtmlErrorRenderer('');

0 commit comments

Comments
 (0)