Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/dev-context-translator' into dev…
Browse files Browse the repository at this point in the history
…-datetime-period
  • Loading branch information
cevro committed Mar 20, 2024
2 parents 227fe7b + 73282c1 commit e11e9bf
Show file tree
Hide file tree
Showing 16 changed files with 326 additions and 209 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
},
"require-dev": {
"squizlabs/php_codesniffer": "3.7.1",
"nette/tester": "^v2.4.0"
"nette/tester": "^v2.4.0",
"phpstan/phpstan": "1.10.26"
},
"authors": [
{
Expand Down
353 changes: 217 additions & 136 deletions composer.lock

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
parameters:
level: 9
paths:
- src
- tests
15 changes: 12 additions & 3 deletions src/Components/DIComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,37 @@

namespace Fykosak\Utils\Components;

use Fykosak\Utils\Localization\GettextTranslator;
use Nette\Application\UI\{Control, Template};
use Nette\DI\Container;
use Nette\Localization\Translator;

/**
* @property \Nette\Bridges\ApplicationLatte\Template $template
*/
abstract class DIComponent extends Control
{
protected readonly Container $container;
protected ?Translator $translator;
protected ?GettextTranslator $translator;

public function __construct(Container $container)
{
$this->container = $container;
$container->callInjects($this);
}

public function injectTranslator(?Translator $translator): void
protected function getContext(): Container
{
return $this->container;
}

public function injectTranslator(?GettextTranslator $translator): void
{
$this->translator = $translator;
}

protected function createTemplate(): Template
{
/** @var \Nette\Bridges\ApplicationLatte\Template $template */
$template = parent::createTemplate();
$template->setTranslator($this->translator);
return $template;
Expand Down
12 changes: 9 additions & 3 deletions src/Localization/GettextTranslator.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@

class GettextTranslator implements Translator
{
/** @param class-string<LangEnum>|string $langEnumClass */
public LangEnum & \BackedEnum $lang;

/**
* @phpstan-param class-string<LangEnum>|string $langEnumClass
*/
public function __construct(
private readonly string $langEnumClass,
) {
Expand All @@ -22,6 +26,9 @@ public function setLang(LangEnum & \BackedEnum $lang): void
if (!$lang instanceof $this->langEnumClass) {
throw new UnsupportedLanguageException($lang);
}
$this->lang = $lang;


putenv('LANGUAGE=' . $lang->getLocale()); // for the sake of CLI tests
setlocale(LC_MESSAGES, $lang->getLocale());
setlocale(LC_TIME, $lang->getLocale());
Expand All @@ -32,8 +39,7 @@ public function setLang(LangEnum & \BackedEnum $lang): void

/**
* @param mixed|string $message
* @param array $parameters
* @return string
* @param string|int $parameters
*/
public function translate($message, ...$parameters): string
{
Expand Down
29 changes: 0 additions & 29 deletions src/Localization/LocalizedSting.php

This file was deleted.

37 changes: 37 additions & 0 deletions src/Localization/LocalizedString.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

declare(strict_types=1);

namespace Fykosak\Utils\Localization;

/**
* @template TLangs of string
*/
class LocalizedString
{
/**
* @phpstan-var array<TLangs,string>
*/
private array $texts;

/**
* @phpstan-param array<TLangs,string> $texts
*/
public function __construct(array $texts)
{
$this->texts = $texts;
}

public function getText(LangEnum & \BackedEnum $lang): ?string
{
return $this->texts[$lang->value] ?? null;
}

/**
* @phpstan-return array<TLangs,string>
*/
public function __serialize(): array
{
return $this->texts;
}
}
14 changes: 6 additions & 8 deletions src/Logging/FlashMessageDump.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,13 @@

class FlashMessageDump
{
public static function dump(Logger $logger, Control $control, bool $clear = true): void
public static function dump(MemoryLogger $logger, Control $control, bool $clear = true): void
{
if ($logger instanceof MemoryLogger) {
foreach ($logger->getMessages() as $message) {
$control->flashMessage($message->text, $message->level->value);
}
if ($clear) {
$logger->clear();
}
foreach ($logger->getMessages() as $message) {
$control->flashMessage($message->text, $message->level->value);
}
if ($clear) {
$logger->clear();
}
}
}
1 change: 1 addition & 0 deletions src/Logging/MemoryLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

class MemoryLogger implements Logger
{
/** @var Message[] */
private array $messages = [];

/**
Expand Down
15 changes: 9 additions & 6 deletions src/Logging/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,23 @@

namespace Fykosak\Utils\Logging;

use Nette\SmartObject;
use Nette\Utils\Html;

class Message
{
use SmartObject;

public function __construct(public readonly string $message, public readonly MessageLevel $level)
{
public function __construct(
public readonly string | Html $text,
public readonly MessageLevel $level
) {
}

/**
* @phpstan-return array{text:string,level:string}
*/
public function __toArray(): array
{
return [
'text' => $this->message,
'text' => ($this->text instanceof Html) ? $this->text->toHtml() : $this->text,
'level' => $this->level,
];
}
Expand Down
8 changes: 3 additions & 5 deletions src/Price/MultiCurrencyPrice.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,14 @@

namespace Fykosak\Utils\Price;

use Nette\SmartObject;

final class MultiCurrencyPrice
{
use SmartObject;

/** @var Price[] */
private array $prices = [];

/** @param Price[] $prices */
/**
* @param Price[]|null $prices
*/
public function __construct(?array $prices = [])
{
foreach ($prices as $price) {
Expand Down
16 changes: 10 additions & 6 deletions src/Price/Price.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@

namespace Fykosak\Utils\Price;

use Nette\SmartObject;

/**
* @phpstan-type TSerializedPrice array{currency:string,amount:float}
*/
final class Price
{
use SmartObject;

public function __construct(public readonly Currency $currency, private float $amount = 0)
{
public function __construct(
public readonly Currency $currency,
private float $amount = 0,
) {
}

/**
Expand Down Expand Up @@ -40,6 +41,9 @@ public function __toString(): string
return $this->currency->format($this->amount);
}

/**
* @phpstan-return TSerializedPrice
*/
public function __serialize(): array
{
return [
Expand Down
7 changes: 4 additions & 3 deletions src/UI/Navigation/NavItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@
namespace Fykosak\Utils\UI\Navigation;

use Fykosak\Utils\UI\Title;
use Nette\SmartObject;

class NavItem
{
use SmartObject;

/**
* @phpstan-param array<string,scalar> $linkParams
* @phpstan-param NavItem[] $children
*/
public function __construct(
public readonly Title $title,
public readonly string $destination = '#',
Expand Down
3 changes: 1 addition & 2 deletions src/UI/Navigation/NavigationItemComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ class NavigationItemComponent extends DIComponent
{
public function render(NavItem $item): void
{
$this->template->item = $item;
$this->template->render(__DIR__ . DIRECTORY_SEPARATOR . 'navigationItem.latte');
$this->template->render(__DIR__ . DIRECTORY_SEPARATOR . 'navigationItem.latte', ['item' => $item]);
}
}
8 changes: 4 additions & 4 deletions src/UI/PageTitle.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,18 @@
class PageTitle extends Title
{
public function __construct(
string $title,
string | Html $title,
?string $icon = null,
public readonly ?string $subTitle = null,
public readonly string | Html | null $subTitle = null,
?string $id = null
) {
parent::__construct($title, icon: $icon, id: $id);
}

public function toHtml(bool $includeSubHeadline = false): Html
public function toHtml(bool $includeSubTitle = false): Html
{
$container = parent::toHtml();
if ($includeSubHeadline && $this->subTitle) {
if ($includeSubTitle && $this->subTitle) {
$container->addHtml(
Html::el('small')
->addAttributes(['class' => 'ml-2 ms-2 text-secondary small'])
Expand Down
9 changes: 6 additions & 3 deletions src/UI/Title.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,14 @@

class Title
{
public readonly string $id;

public function __construct(
public readonly string $title,
public readonly string | Html $title,
public readonly ?string $icon = null,
public readonly ?string $id = null,
?string $id = null,
) {
$this->id = $id ?? Random::generate(10, 'a-z');
}

public function toHtml(): Html
Expand All @@ -23,7 +26,7 @@ public function toHtml(): Html
$container->addHtml(
Html::el('i')->addAttributes(
[
'id' => $this->id ?? Random::generate(10, 'a-z'),
'id' => $this->id,
'class' => $this->icon . ' mr-2 me-2',
]
)
Expand Down

0 comments on commit e11e9bf

Please sign in to comment.