From a4a8bbb90f67bff3bfe305cd6774734411eb790f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20Mi=C5=A1o=20=C4=8Cerve=C5=88=C3=A1k?= Date: Tue, 24 Jan 2023 18:33:58 +0100 Subject: [PATCH 01/31] add lang as translator state --- src/Localization/GettextTranslator.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Localization/GettextTranslator.php b/src/Localization/GettextTranslator.php index 92a3d89..b3cd4b7 100644 --- a/src/Localization/GettextTranslator.php +++ b/src/Localization/GettextTranslator.php @@ -8,10 +8,10 @@ class GettextTranslator implements Translator { - /** @var array[lang] => locale */ public array $locales; private string $localeDir; + public ?string $lang = null; public function __construct(array $locales, string $localeDir) { @@ -26,6 +26,7 @@ public function __construct(array $locales, string $localeDir) */ public function setLang(string $lang): void { + $this->lang = $lang; if (!isset($this->locales[$lang])) { throw new UnsupportedLanguageException($lang); } From 6c299c7e8ad90f5a9bdae5d318ea149e668fea5b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20Mi=C5=A1o=20=C4=8Cerve=C5=88=C3=A1k?= Date: Mon, 17 Jul 2023 19:35:18 +0200 Subject: [PATCH 02/31] fix localizedString --- composer.lock | 256 +++++++++--------- ...LocalizedSting.php => LocalizedString.php} | 19 +- 2 files changed, 137 insertions(+), 138 deletions(-) rename src/Localization/{LocalizedSting.php => LocalizedString.php} (54%) diff --git a/composer.lock b/composer.lock index b2a205a..643a198 100644 --- a/composer.lock +++ b/composer.lock @@ -8,31 +8,31 @@ "packages": [ { "name": "latte/latte", - "version": "v2.10.7", + "version": "v2.11.6", "source": { "type": "git", "url": "https://github.com/nette/latte.git", - "reference": "a69d0b9598652438b5754ae5c1abc217d5003d98" + "reference": "af4dbb23a6044b1cf4904830a9b00246318c93d4" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/nette/latte/zipball/a69d0b9598652438b5754ae5c1abc217d5003d98", - "reference": "a69d0b9598652438b5754ae5c1abc217d5003d98", + "url": "https://api.github.com/repos/nette/latte/zipball/af4dbb23a6044b1cf4904830a9b00246318c93d4", + "reference": "af4dbb23a6044b1cf4904830a9b00246318c93d4", "shasum": "" }, "require": { "ext-json": "*", "ext-tokenizer": "*", - "php": ">=7.1 <8.2" + "php": ">=7.1 <8.3" }, "conflict": { "nette/application": "<2.4.1" }, "require-dev": { "nette/php-generator": "^3.3.4", - "nette/tester": "~2.0", + "nette/tester": "^2.0", "nette/utils": "^3.0", - "phpstan/phpstan": "^0.12", + "phpstan/phpstan": "^1", "tracy/tracy": "^2.3" }, "suggest": { @@ -48,7 +48,7 @@ "type": "library", "extra": { "branch-alias": { - "dev-master": "2.10-dev" + "dev-master": "2.11-dev" } }, "autoload": { @@ -86,33 +86,33 @@ ], "support": { "issues": "https://github.com/nette/latte/issues", - "source": "https://github.com/nette/latte/tree/v2.10.7" + "source": "https://github.com/nette/latte/tree/v2.11.6" }, - "time": "2021-12-21T11:22:49+00:00" + "time": "2022-11-04T14:53:52+00:00" }, { "name": "nette/application", - "version": "v3.1.5", + "version": "v3.1.11", "source": { "type": "git", "url": "https://github.com/nette/application.git", - "reference": "fa5da6a90ff71724353568894a4839aec627eae3" + "reference": "b03bd4971b03e3fa582ac40ea429446cd00788bb" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/nette/application/zipball/fa5da6a90ff71724353568894a4839aec627eae3", - "reference": "fa5da6a90ff71724353568894a4839aec627eae3", + "url": "https://api.github.com/repos/nette/application/zipball/b03bd4971b03e3fa582ac40ea429446cd00788bb", + "reference": "b03bd4971b03e3fa582ac40ea429446cd00788bb", "shasum": "" }, "require": { "nette/component-model": "^3.0", "nette/http": "^3.0.2", "nette/routing": "^3.0.2", - "nette/utils": "^3.2.1", + "nette/utils": "^3.2.1 || ~4.0.0", "php": ">=7.2" }, "conflict": { - "latte/latte": "<2.7.1 || >=3.0", + "latte/latte": "<2.7.1 || >=3.0.0 <3.0.5 || >=3.1", "nette/caching": "<3.1", "nette/di": "<3.0.7", "nette/forms": "<3.0", @@ -120,7 +120,8 @@ "tracy/tracy": "<2.5" }, "require-dev": { - "latte/latte": "^2.10.2", + "jetbrains/phpstorm-attributes": "dev-master", + "latte/latte": "^2.10.2 || ^3.0.3", "mockery/mockery": "^1.0", "nette/di": "^v3.0", "nette/forms": "^3.0", @@ -177,26 +178,26 @@ ], "support": { "issues": "https://github.com/nette/application/issues", - "source": "https://github.com/nette/application/tree/v3.1.5" + "source": "https://github.com/nette/application/tree/v3.1.11" }, - "time": "2021-12-20T12:24:49+00:00" + "time": "2023-04-28T10:09:21+00:00" }, { "name": "nette/component-model", - "version": "v3.0.2", + "version": "v3.0.3", "source": { "type": "git", "url": "https://github.com/nette/component-model.git", - "reference": "20a39df12009029c7e425bc5e0439ee4ab5304af" + "reference": "9d97c0e1916bbf8e306283ab187834501fd4b1f5" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/nette/component-model/zipball/20a39df12009029c7e425bc5e0439ee4ab5304af", - "reference": "20a39df12009029c7e425bc5e0439ee4ab5304af", + "url": "https://api.github.com/repos/nette/component-model/zipball/9d97c0e1916bbf8e306283ab187834501fd4b1f5", + "reference": "9d97c0e1916bbf8e306283ab187834501fd4b1f5", "shasum": "" }, "require": { - "nette/utils": "^2.5 || ^3.0", + "nette/utils": "^2.5 || ^3.0 || ~4.0.0", "php": ">=7.1" }, "require-dev": { @@ -239,45 +240,42 @@ ], "support": { "issues": "https://github.com/nette/component-model/issues", - "source": "https://github.com/nette/component-model/tree/v3.0.2" + "source": "https://github.com/nette/component-model/tree/v3.0.3" }, - "time": "2021-08-25T14:52:12+00:00" + "time": "2023-01-09T20:16:05+00:00" }, { "name": "nette/di", - "version": "v3.0.12", + "version": "v3.1.2", "source": { "type": "git", "url": "https://github.com/nette/di.git", - "reference": "11c236b9f7bbfc5a95e7b24742ad8847936feeb5" + "reference": "355cefbd71011a76b670fda3340d612a6944f972" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/nette/di/zipball/11c236b9f7bbfc5a95e7b24742ad8847936feeb5", - "reference": "11c236b9f7bbfc5a95e7b24742ad8847936feeb5", + "url": "https://api.github.com/repos/nette/di/zipball/355cefbd71011a76b670fda3340d612a6944f972", + "reference": "355cefbd71011a76b670fda3340d612a6944f972", "shasum": "" }, "require": { "ext-tokenizer": "*", - "nette/neon": "^3.3", - "nette/php-generator": "^3.5.4", - "nette/robot-loader": "^3.2", - "nette/schema": "^1.1", - "nette/utils": "^3.1.6", - "php": ">=7.1 <8.2" - }, - "conflict": { - "nette/bootstrap": "<3.0" + "nette/neon": "^3.3 || ^4.0", + "nette/php-generator": "^3.5.4 || ^4.0", + "nette/robot-loader": "^3.2 || ~4.0.0", + "nette/schema": "^1.2", + "nette/utils": "^3.2.5 || ~4.0.0", + "php": ">=7.2 <8.3" }, "require-dev": { - "nette/tester": "^2.2", - "phpstan/phpstan": "^0.12", - "tracy/tracy": "^2.3" + "nette/tester": "^2.4", + "phpstan/phpstan": "^1.0", + "tracy/tracy": "^2.9" }, "type": "library", "extra": { "branch-alias": { - "dev-master": "3.0-dev" + "dev-master": "3.1-dev" } }, "autoload": { @@ -314,22 +312,22 @@ ], "support": { "issues": "https://github.com/nette/di/issues", - "source": "https://github.com/nette/di/tree/v3.0.12" + "source": "https://github.com/nette/di/tree/v3.1.2" }, - "time": "2021-12-15T21:05:11+00:00" + "time": "2023-03-13T14:03:15+00:00" }, { "name": "nette/finder", - "version": "v2.5.3", + "version": "v2.6.0", "source": { "type": "git", "url": "https://github.com/nette/finder.git", - "reference": "64dc25b7929b731e72a1bc84a9e57727f5d5d3e8" + "reference": "991aefb42860abeab8e003970c3809a9d83cb932" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/nette/finder/zipball/64dc25b7929b731e72a1bc84a9e57727f5d5d3e8", - "reference": "64dc25b7929b731e72a1bc84a9e57727f5d5d3e8", + "url": "https://api.github.com/repos/nette/finder/zipball/991aefb42860abeab8e003970c3809a9d83cb932", + "reference": "991aefb42860abeab8e003970c3809a9d83cb932", "shasum": "" }, "require": { @@ -347,7 +345,7 @@ "type": "library", "extra": { "branch-alias": { - "dev-master": "2.5-dev" + "dev-master": "2.6-dev" } }, "autoload": { @@ -381,41 +379,40 @@ ], "support": { "issues": "https://github.com/nette/finder/issues", - "source": "https://github.com/nette/finder/tree/v2.5.3" + "source": "https://github.com/nette/finder/tree/v2.6.0" }, - "time": "2021-12-12T17:43:24+00:00" + "time": "2022-10-13T01:31:15+00:00" }, { "name": "nette/forms", - "version": "v3.1.6", + "version": "v3.1.11", "source": { "type": "git", "url": "https://github.com/nette/forms.git", - "reference": "4ed52434b61d7e532cb3bc77b048717703b91b0b" + "reference": "64cdc2d6796a8fe1265bb21a6ee5e9ff93e2b3a4" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/nette/forms/zipball/4ed52434b61d7e532cb3bc77b048717703b91b0b", - "reference": "4ed52434b61d7e532cb3bc77b048717703b91b0b", + "url": "https://api.github.com/repos/nette/forms/zipball/64cdc2d6796a8fe1265bb21a6ee5e9ff93e2b3a4", + "reference": "64cdc2d6796a8fe1265bb21a6ee5e9ff93e2b3a4", "shasum": "" }, "require": { "nette/component-model": "^3.0", "nette/http": "^3.1", - "nette/utils": "^3.2.1", - "php": ">=7.2 <8.2" + "nette/utils": "^3.2.5 || ~4.0.0", + "php": ">=7.2 <8.3" }, "conflict": { - "latte/latte": ">=3.0", - "nette/di": "<3.0-stable" + "latte/latte": ">=3.1" }, "require-dev": { - "latte/latte": "^2.10.2", + "latte/latte": "^2.10.2 || ^3.0.3", "nette/application": "^3.0", "nette/di": "^3.0", - "nette/tester": "^2.0", - "phpstan/phpstan-nette": "^0.12", - "tracy/tracy": "^2.4" + "nette/tester": "^2.4", + "phpstan/phpstan-nette": "^1", + "tracy/tracy": "^2.9" }, "type": "library", "extra": { @@ -456,27 +453,27 @@ ], "support": { "issues": "https://github.com/nette/forms/issues", - "source": "https://github.com/nette/forms/tree/v3.1.6" + "source": "https://github.com/nette/forms/tree/v3.1.11" }, - "time": "2021-11-09T11:56:09+00:00" + "time": "2023-03-08T23:56:24+00:00" }, { "name": "nette/http", - "version": "v3.1.5", + "version": "v3.2.2", "source": { "type": "git", "url": "https://github.com/nette/http.git", - "reference": "8146c2f2a262691a7139f9c56007961dcc5c1f42" + "reference": "9105c26de3dd47da5e7cf6b4132b5d871f835e25" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/nette/http/zipball/8146c2f2a262691a7139f9c56007961dcc5c1f42", - "reference": "8146c2f2a262691a7139f9c56007961dcc5c1f42", + "url": "https://api.github.com/repos/nette/http/zipball/9105c26de3dd47da5e7cf6b4132b5d871f835e25", + "reference": "9105c26de3dd47da5e7cf6b4132b5d871f835e25", "shasum": "" }, "require": { - "nette/utils": "^3.1", - "php": ">=7.2 <8.2" + "nette/utils": "^3.2.1 || ~4.0.0", + "php": ">=7.2 <8.3" }, "conflict": { "nette/di": "<3.0.3", @@ -485,9 +482,9 @@ "require-dev": { "nette/di": "^3.0", "nette/security": "^3.0", - "nette/tester": "^2.0", - "phpstan/phpstan": "^0.12", - "tracy/tracy": "^2.4" + "nette/tester": "^2.4", + "phpstan/phpstan": "^1.0", + "tracy/tracy": "^2.8" }, "suggest": { "ext-fileinfo": "to detect type of uploaded files" @@ -495,7 +492,7 @@ "type": "library", "extra": { "branch-alias": { - "dev-master": "3.1-dev" + "dev-master": "3.2-dev" } }, "autoload": { @@ -534,22 +531,22 @@ ], "support": { "issues": "https://github.com/nette/http/issues", - "source": "https://github.com/nette/http/tree/v3.1.5" + "source": "https://github.com/nette/http/tree/v3.2.2" }, - "time": "2021-11-29T18:56:42+00:00" + "time": "2023-03-18T14:55:56+00:00" }, { "name": "nette/neon", - "version": "v3.3.2", + "version": "v3.3.3", "source": { "type": "git", "url": "https://github.com/nette/neon.git", - "reference": "54b287d8c2cdbe577b02e28ca1713e275b05ece2" + "reference": "22e384da162fab42961d48eb06c06d3ad0c11b95" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/nette/neon/zipball/54b287d8c2cdbe577b02e28ca1713e275b05ece2", - "reference": "54b287d8c2cdbe577b02e28ca1713e275b05ece2", + "url": "https://api.github.com/repos/nette/neon/zipball/22e384da162fab42961d48eb06c06d3ad0c11b95", + "reference": "22e384da162fab42961d48eb06c06d3ad0c11b95", "shasum": "" }, "require": { @@ -602,27 +599,27 @@ ], "support": { "issues": "https://github.com/nette/neon/issues", - "source": "https://github.com/nette/neon/tree/v3.3.2" + "source": "https://github.com/nette/neon/tree/v3.3.3" }, - "time": "2021-11-25T15:57:41+00:00" + "time": "2022-03-10T02:04:26+00:00" }, { "name": "nette/php-generator", - "version": "v3.6.5", + "version": "v3.6.9", "source": { "type": "git", "url": "https://github.com/nette/php-generator.git", - "reference": "9370403f9d9c25b51c4596ded1fbfe70347f7c82" + "reference": "d31782f7bd2ae84ad06f863391ec3fb77ca4d0a6" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/nette/php-generator/zipball/9370403f9d9c25b51c4596ded1fbfe70347f7c82", - "reference": "9370403f9d9c25b51c4596ded1fbfe70347f7c82", + "url": "https://api.github.com/repos/nette/php-generator/zipball/d31782f7bd2ae84ad06f863391ec3fb77ca4d0a6", + "reference": "d31782f7bd2ae84ad06f863391ec3fb77ca4d0a6", "shasum": "" }, "require": { "nette/utils": "^3.1.2", - "php": ">=7.2 <8.2" + "php": ">=7.2 <8.3" }, "require-dev": { "nette/tester": "^2.4", @@ -670,22 +667,22 @@ ], "support": { "issues": "https://github.com/nette/php-generator/issues", - "source": "https://github.com/nette/php-generator/tree/v3.6.5" + "source": "https://github.com/nette/php-generator/tree/v3.6.9" }, - "time": "2021-11-24T16:23:44+00:00" + "time": "2022-10-04T11:49:47+00:00" }, { "name": "nette/robot-loader", - "version": "v3.4.1", + "version": "v3.4.2", "source": { "type": "git", "url": "https://github.com/nette/robot-loader.git", - "reference": "e2adc334cb958164c050f485d99c44c430f51fe2" + "reference": "970c8f82be98ec54180c88a468cd2b057855d993" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/nette/robot-loader/zipball/e2adc334cb958164c050f485d99c44c430f51fe2", - "reference": "e2adc334cb958164c050f485d99c44c430f51fe2", + "url": "https://api.github.com/repos/nette/robot-loader/zipball/970c8f82be98ec54180c88a468cd2b057855d993", + "reference": "970c8f82be98ec54180c88a468cd2b057855d993", "shasum": "" }, "require": { @@ -737,32 +734,32 @@ ], "support": { "issues": "https://github.com/nette/robot-loader/issues", - "source": "https://github.com/nette/robot-loader/tree/v3.4.1" + "source": "https://github.com/nette/robot-loader/tree/v3.4.2" }, - "time": "2021-08-25T15:53:54+00:00" + "time": "2022-12-14T15:41:06+00:00" }, { "name": "nette/routing", - "version": "v3.0.2", + "version": "v3.0.4", "source": { "type": "git", "url": "https://github.com/nette/routing.git", - "reference": "5532e7e3612e13def357f089c1a5c25793a16843" + "reference": "eaefe6375303799366f3e43977daaf33f5f89b95" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/nette/routing/zipball/5532e7e3612e13def357f089c1a5c25793a16843", - "reference": "5532e7e3612e13def357f089c1a5c25793a16843", + "url": "https://api.github.com/repos/nette/routing/zipball/eaefe6375303799366f3e43977daaf33f5f89b95", + "reference": "eaefe6375303799366f3e43977daaf33f5f89b95", "shasum": "" }, "require": { - "nette/http": "^3.0", - "nette/utils": "^3.0", + "nette/http": "^3.0 || ~4.0.0", + "nette/utils": "^3.0 || ~4.0.0", "php": ">=7.1" }, "require-dev": { "nette/tester": "^2.0", - "phpstan/phpstan": "^0.12", + "phpstan/phpstan": "^1", "tracy/tracy": "^2.3" }, "type": "library", @@ -799,31 +796,31 @@ ], "support": { "issues": "https://github.com/nette/routing/issues", - "source": "https://github.com/nette/routing/tree/v3.0.2" + "source": "https://github.com/nette/routing/tree/v3.0.4" }, - "time": "2021-02-06T04:08:30+00:00" + "time": "2023-01-18T04:58:41+00:00" }, { "name": "nette/schema", - "version": "v1.2.2", + "version": "v1.2.3", "source": { "type": "git", "url": "https://github.com/nette/schema.git", - "reference": "9a39cef03a5b34c7de64f551538cbba05c2be5df" + "reference": "abbdbb70e0245d5f3bf77874cea1dfb0c930d06f" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/nette/schema/zipball/9a39cef03a5b34c7de64f551538cbba05c2be5df", - "reference": "9a39cef03a5b34c7de64f551538cbba05c2be5df", + "url": "https://api.github.com/repos/nette/schema/zipball/abbdbb70e0245d5f3bf77874cea1dfb0c930d06f", + "reference": "abbdbb70e0245d5f3bf77874cea1dfb0c930d06f", "shasum": "" }, "require": { "nette/utils": "^2.5.7 || ^3.1.5 || ^4.0", - "php": ">=7.1 <8.2" + "php": ">=7.1 <8.3" }, "require-dev": { "nette/tester": "^2.3 || ^2.4", - "phpstan/phpstan-nette": "^0.12", + "phpstan/phpstan-nette": "^1.0", "tracy/tracy": "^2.7" }, "type": "library", @@ -861,31 +858,32 @@ ], "support": { "issues": "https://github.com/nette/schema/issues", - "source": "https://github.com/nette/schema/tree/v1.2.2" + "source": "https://github.com/nette/schema/tree/v1.2.3" }, - "time": "2021-10-15T11:40:02+00:00" + "time": "2022-10-13T01:24:26+00:00" }, { "name": "nette/utils", - "version": "v3.2.6", + "version": "v3.2.9", "source": { "type": "git", "url": "https://github.com/nette/utils.git", - "reference": "2f261e55bd6a12057442045bf2c249806abc1d02" + "reference": "c91bac3470c34b2ecd5400f6e6fdf0b64a836a5c" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/nette/utils/zipball/2f261e55bd6a12057442045bf2c249806abc1d02", - "reference": "2f261e55bd6a12057442045bf2c249806abc1d02", + "url": "https://api.github.com/repos/nette/utils/zipball/c91bac3470c34b2ecd5400f6e6fdf0b64a836a5c", + "reference": "c91bac3470c34b2ecd5400f6e6fdf0b64a836a5c", "shasum": "" }, "require": { - "php": ">=7.2 <8.2" + "php": ">=7.2 <8.3" }, "conflict": { "nette/di": "<3.0.6" }, "require-dev": { + "jetbrains/phpstorm-attributes": "dev-master", "nette/tester": "~2.0", "phpstan/phpstan": "^1.0", "tracy/tracy": "^2.3" @@ -946,32 +944,32 @@ ], "support": { "issues": "https://github.com/nette/utils/issues", - "source": "https://github.com/nette/utils/tree/v3.2.6" + "source": "https://github.com/nette/utils/tree/v3.2.9" }, - "time": "2021-11-24T15:47:23+00:00" + "time": "2023-01-18T03:26:20+00:00" } ], "packages-dev": [ { "name": "nette/tester", - "version": "v2.4.1", + "version": "v2.4.3", "source": { "type": "git", "url": "https://github.com/nette/tester.git", - "reference": "b54326b3c1a2c6c76d2662a06b5ad5a10d822e98" + "reference": "451f6e97b117797e817446de8d19fe06e54fd33f" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/nette/tester/zipball/b54326b3c1a2c6c76d2662a06b5ad5a10d822e98", - "reference": "b54326b3c1a2c6c76d2662a06b5ad5a10d822e98", + "url": "https://api.github.com/repos/nette/tester/zipball/451f6e97b117797e817446de8d19fe06e54fd33f", + "reference": "451f6e97b117797e817446de8d19fe06e54fd33f", "shasum": "" }, "require": { - "php": ">=7.2 <8.2" + "php": ">=7.2 <8.3" }, "require-dev": { "ext-simplexml": "*", - "phpstan/phpstan": "^0.12" + "phpstan/phpstan": "^1.0" }, "bin": [ "src/tester" @@ -1023,9 +1021,9 @@ ], "support": { "issues": "https://github.com/nette/tester/issues", - "source": "https://github.com/nette/tester/tree/v2.4.1" + "source": "https://github.com/nette/tester/tree/v2.4.3" }, - "time": "2021-08-24T14:13:00+00:00" + "time": "2022-08-29T16:39:38+00:00" }, { "name": "squizlabs/php_codesniffer", diff --git a/src/Localization/LocalizedSting.php b/src/Localization/LocalizedString.php similarity index 54% rename from src/Localization/LocalizedSting.php rename to src/Localization/LocalizedString.php index c778f7b..4baea30 100644 --- a/src/Localization/LocalizedSting.php +++ b/src/Localization/LocalizedString.php @@ -4,27 +4,28 @@ namespace Fykosak\Utils\Localization; -use Nette\SmartObject; - -class LocalizedSting +class LocalizedString { - use SmartObject; - private array $texts; - public function __construct(?array $texts = null) + public function __construct(array $texts) { - $this->texts = $texts ?? []; + $this->texts = $texts; } public function __get(string $lang): ?string + { + return $this->getText($lang); + } + + public function getText(string $lang): ?string { return $this->texts[$lang] ?? null; } - public function __set(string $lang, string $text): void + public function __serialize(): array { - $this->texts[$lang] = $text; + return $this->texts; } public function __toString(): string From a5ecc4b44f1ff60edfdd833ae392c81d35f650c0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20Mi=C5=A1o=20=C4=8Cerve=C5=88=C3=A1k?= Date: Thu, 27 Jul 2023 20:11:41 +0200 Subject: [PATCH 03/31] add phpstan --- composer.json | 3 +- composer.lock | 64 ++++++++++++++++++- phpstan.neon | 5 ++ src/BaseComponent/BaseComponent.php | 10 ++- src/Localization/GettextTranslator.php | 8 ++- src/Localization/LocalizedString.php | 6 ++ src/Logging/MemoryLogger.php | 1 + src/Logging/Message.php | 3 + src/Price/Currency.php | 3 + src/Price/MultiCurrencyPrice.php | 3 + src/UI/Navigation/NavItem.php | 5 ++ src/UI/Navigation/NavigationItemComponent.php | 3 +- src/UI/Title.php | 1 - 13 files changed, 104 insertions(+), 11 deletions(-) create mode 100644 phpstan.neon diff --git a/composer.json b/composer.json index df3f293..44280f9 100644 --- a/composer.json +++ b/composer.json @@ -17,7 +17,8 @@ }, "require-dev": { "squizlabs/php_codesniffer": "3.6.0", - "nette/tester": "^v2.4.0" + "nette/tester": "^v2.4.0", + "phpstan/phpstan": "1.10.26" }, "authors": [ { diff --git a/composer.lock b/composer.lock index 643a198..cda471d 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "83a7a64c94476a8beae242b18908d6b2", + "content-hash": "2663362994d66de181d7da9d6059204d", "packages": [ { "name": "latte/latte", @@ -1025,6 +1025,68 @@ }, "time": "2022-08-29T16:39:38+00:00" }, + { + "name": "phpstan/phpstan", + "version": "1.10.26", + "source": { + "type": "git", + "url": "https://github.com/phpstan/phpstan.git", + "reference": "5d660cbb7e1b89253a47147ae44044f49832351f" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/phpstan/phpstan/zipball/5d660cbb7e1b89253a47147ae44044f49832351f", + "reference": "5d660cbb7e1b89253a47147ae44044f49832351f", + "shasum": "" + }, + "require": { + "php": "^7.2|^8.0" + }, + "conflict": { + "phpstan/phpstan-shim": "*" + }, + "bin": [ + "phpstan", + "phpstan.phar" + ], + "type": "library", + "autoload": { + "files": [ + "bootstrap.php" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "description": "PHPStan - PHP Static Analysis Tool", + "keywords": [ + "dev", + "static analysis" + ], + "support": { + "docs": "https://phpstan.org/user-guide/getting-started", + "forum": "https://github.com/phpstan/phpstan/discussions", + "issues": "https://github.com/phpstan/phpstan/issues", + "security": "https://github.com/phpstan/phpstan/security/policy", + "source": "https://github.com/phpstan/phpstan-src" + }, + "funding": [ + { + "url": "https://github.com/ondrejmirtes", + "type": "github" + }, + { + "url": "https://github.com/phpstan", + "type": "github" + }, + { + "url": "https://tidelift.com/funding/github/packagist/phpstan/phpstan", + "type": "tidelift" + } + ], + "time": "2023-07-19T12:44:37+00:00" + }, { "name": "squizlabs/php_codesniffer", "version": "3.6.0", diff --git a/phpstan.neon b/phpstan.neon new file mode 100644 index 0000000..08566d0 --- /dev/null +++ b/phpstan.neon @@ -0,0 +1,5 @@ +parameters: + level: 9 + paths: + - src + - tests diff --git a/src/BaseComponent/BaseComponent.php b/src/BaseComponent/BaseComponent.php index 7294d81..c64d501 100644 --- a/src/BaseComponent/BaseComponent.php +++ b/src/BaseComponent/BaseComponent.php @@ -4,14 +4,17 @@ namespace Fykosak\Utils\BaseComponent; +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 BaseComponent extends Control { protected Container $container; - protected ?Translator $translator; + protected ?GettextTranslator $translator; public function __construct(Container $container) { @@ -24,13 +27,14 @@ protected function getContext(): Container return $this->container; } - public function injectTranslator(?Translator $translator): void + 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; diff --git a/src/Localization/GettextTranslator.php b/src/Localization/GettextTranslator.php index b3cd4b7..61cc5df 100644 --- a/src/Localization/GettextTranslator.php +++ b/src/Localization/GettextTranslator.php @@ -8,11 +8,14 @@ class GettextTranslator implements Translator { - /** @var array[lang] => locale */ + /** @phpstan-var array */ public array $locales; private string $localeDir; public ?string $lang = null; + /** + * @phpstan-param array $locales + */ public function __construct(array $locales, string $localeDir) { $this->locales = $locales; @@ -50,8 +53,7 @@ public function getSupportedLanguages(): array /** * @param mixed|string $message - * @param array $parameters - * @return string + * @param string|int $parameters */ public function translate($message, ...$parameters): string { diff --git a/src/Localization/LocalizedString.php b/src/Localization/LocalizedString.php index 4baea30..7901db0 100644 --- a/src/Localization/LocalizedString.php +++ b/src/Localization/LocalizedString.php @@ -6,8 +6,14 @@ class LocalizedString { + /** + * @phpstan-var array + */ private array $texts; + /** + * @phpstan-param array $texts + */ public function __construct(array $texts) { $this->texts = $texts; diff --git a/src/Logging/MemoryLogger.php b/src/Logging/MemoryLogger.php index bfb8a7f..efe3c0f 100644 --- a/src/Logging/MemoryLogger.php +++ b/src/Logging/MemoryLogger.php @@ -6,6 +6,7 @@ class MemoryLogger implements Logger { + /** @var Message[] */ private array $messages = []; /** diff --git a/src/Logging/Message.php b/src/Logging/Message.php index 63d7dc6..50ff17f 100644 --- a/src/Logging/Message.php +++ b/src/Logging/Message.php @@ -25,6 +25,9 @@ public function __construct(string $message, string $level) $this->level = $level; } + /** + * @phpstan-return array{'text':string,'level':string} + */ public function __toArray(): array { return [ diff --git a/src/Price/Currency.php b/src/Price/Currency.php index 5929350..65e3cbe 100644 --- a/src/Price/Currency.php +++ b/src/Price/Currency.php @@ -31,6 +31,9 @@ public function __construct(string $currency) $this->value = $currency; } + /** + * @return self[] + */ public static function cases(): array { return [new self(self::CZK), new self(self::EUR)]; diff --git a/src/Price/MultiCurrencyPrice.php b/src/Price/MultiCurrencyPrice.php index 574121e..22e25b5 100644 --- a/src/Price/MultiCurrencyPrice.php +++ b/src/Price/MultiCurrencyPrice.php @@ -13,6 +13,9 @@ final class MultiCurrencyPrice /** @var Price[] */ private array $prices = []; + /** + * @param Price[]|null $prices + */ public function __construct(?array $prices = []) { foreach ($prices as $price) { diff --git a/src/UI/Navigation/NavItem.php b/src/UI/Navigation/NavItem.php index 9ce7cc9..81355ff 100644 --- a/src/UI/Navigation/NavItem.php +++ b/src/UI/Navigation/NavItem.php @@ -12,12 +12,17 @@ class NavItem use SmartObject; public string $destination; + /** @phpstan-var array */ public array $linkParams; public Title $title; /** @var NavItem[] */ public array $children; public bool $active; + /** + * @phpstan-param array $linkParams + * @phpstan-param NavItem[] $children + */ public function __construct( Title $title, string $destination = '#', diff --git a/src/UI/Navigation/NavigationItemComponent.php b/src/UI/Navigation/NavigationItemComponent.php index 34942ff..5db1ad2 100644 --- a/src/UI/Navigation/NavigationItemComponent.php +++ b/src/UI/Navigation/NavigationItemComponent.php @@ -14,7 +14,6 @@ class NavigationItemComponent extends BaseComponent { 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]); } } diff --git a/src/UI/Title.php b/src/UI/Title.php index 9c9147a..3b7aa92 100644 --- a/src/UI/Title.php +++ b/src/UI/Title.php @@ -10,7 +10,6 @@ class Title { public string $title; - public ?string $icon; public ?string $id; From 4fcf013f3c4c90656a09894feb26271b8c88714b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20Mi=C5=A1o=20=C4=8Cerve=C5=88=C3=A1k?= Date: Mon, 31 Jul 2023 20:26:05 +0200 Subject: [PATCH 04/31] LocalizedString --- src/Localization/LocalizedString.php | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/Localization/LocalizedString.php b/src/Localization/LocalizedString.php index 7901db0..18235b0 100644 --- a/src/Localization/LocalizedString.php +++ b/src/Localization/LocalizedString.php @@ -4,31 +4,43 @@ namespace Fykosak\Utils\Localization; +/** + * @template L of string + */ class LocalizedString { /** - * @phpstan-var array + * @phpstan-var array */ private array $texts; /** - * @phpstan-param array $texts + * @phpstan-param array $texts */ public function __construct(array $texts) { $this->texts = $texts; } + /** + * @phpstan-param L $lang + */ public function __get(string $lang): ?string { return $this->getText($lang); } + /** + * @phpstan-param L $lang + */ public function getText(string $lang): ?string { return $this->texts[$lang] ?? null; } + /** + * @phpstan-return array + */ public function __serialize(): array { return $this->texts; From 5d3f8e7256d345102c3c3b0e8fed9574e29fcefa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20Mi=C5=A1o=20=C4=8Cerve=C5=88=C3=A1k?= Date: Tue, 15 Aug 2023 20:55:05 +0200 Subject: [PATCH 05/31] PHPDoc --- src/Logging/FlashMessageDump.php | 14 ++++++-------- src/Logging/Message.php | 2 +- src/Price/Price.php | 6 ++++++ src/UI/Navigation/NavItem.php | 4 ++-- 4 files changed, 15 insertions(+), 11 deletions(-) diff --git a/src/Logging/FlashMessageDump.php b/src/Logging/FlashMessageDump.php index 5da3c26..7111e91 100644 --- a/src/Logging/FlashMessageDump.php +++ b/src/Logging/FlashMessageDump.php @@ -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); - } - if ($clear) { - $logger->clear(); - } + foreach ($logger->getMessages() as $message) { + $control->flashMessage($message->text, $message->level); + } + if ($clear) { + $logger->clear(); } } } diff --git a/src/Logging/Message.php b/src/Logging/Message.php index 50ff17f..8759ce4 100644 --- a/src/Logging/Message.php +++ b/src/Logging/Message.php @@ -26,7 +26,7 @@ public function __construct(string $message, string $level) } /** - * @phpstan-return array{'text':string,'level':string} + * @phpstan-return array{text:string,level:string} */ public function __toArray(): array { diff --git a/src/Price/Price.php b/src/Price/Price.php index d0ae8ce..64fae1b 100644 --- a/src/Price/Price.php +++ b/src/Price/Price.php @@ -6,6 +6,9 @@ use Nette\SmartObject; +/** + * @phpstan-type TSerializedPrice array{currency:string,amount:float} + */ final class Price { use SmartObject; @@ -50,6 +53,9 @@ public function __toString(): string return $this->currency->format($this->amount); } + /** + * @phpstan-return TSerializedPrice + */ public function __serialize(): array { return [ diff --git a/src/UI/Navigation/NavItem.php b/src/UI/Navigation/NavItem.php index 81355ff..3683fef 100644 --- a/src/UI/Navigation/NavItem.php +++ b/src/UI/Navigation/NavItem.php @@ -12,7 +12,7 @@ class NavItem use SmartObject; public string $destination; - /** @phpstan-var array */ + /** @phpstan-var array */ public array $linkParams; public Title $title; /** @var NavItem[] */ @@ -20,7 +20,7 @@ class NavItem public bool $active; /** - * @phpstan-param array $linkParams + * @phpstan-param array $linkParams * @phpstan-param NavItem[] $children */ public function __construct( From 8f3e43a15686c8fe4b5b14c00e3ccc0f7c7a4ca7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20Mi=C5=A1o=20=C4=8Cerve=C5=88=C3=A1k?= Date: Thu, 2 Nov 2023 00:30:02 +0100 Subject: [PATCH 06/31] PageTile accept Html --- src/UI/PageTitle.php | 13 +++++++++---- src/UI/Title.php | 14 +++++++++----- 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/src/UI/PageTitle.php b/src/UI/PageTitle.php index 9b29ef0..983dc7b 100644 --- a/src/UI/PageTitle.php +++ b/src/UI/PageTitle.php @@ -8,18 +8,23 @@ class PageTitle extends Title { - public ?string $subTitle; + /** @var string|Html|null */ + public $subTitle; - public function __construct(?string $id, string $title, ?string $icon = null, ?string $subTitle = null) + /** + * @param string|Html $title + * @param string|Html|null $subTitle + */ + public function __construct(?string $id, $title, ?string $icon = null, $subTitle = null) { parent::__construct($id, $title, $icon); $this->subTitle = $subTitle; } - 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']) diff --git a/src/UI/Title.php b/src/UI/Title.php index 3b7aa92..b5fbeb8 100644 --- a/src/UI/Title.php +++ b/src/UI/Title.php @@ -9,15 +9,19 @@ class Title { - public string $title; + /** @var string|Html */ + public $title; public ?string $icon; - public ?string $id; + public string $id; - public function __construct(?string $id, string $title, ?string $icon = null) + /** + * @param string|Html $title + */ + public function __construct(?string $id, $title, ?string $icon = null) { $this->title = $title; $this->icon = $icon; - $this->id = $id; + $this->id = $id ?? Random::generate(10, 'a-z'); } public function toHtml(): Html @@ -27,7 +31,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', ] ) From b817100d144bb0190ce69115cd016ed2f778d656 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Kr=C5=A1ka?= Date: Wed, 7 Feb 2024 20:49:59 +0100 Subject: [PATCH 07/31] Message accept Html --- src/Logging/Message.php | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/Logging/Message.php b/src/Logging/Message.php index 8759ce4..2c4f953 100644 --- a/src/Logging/Message.php +++ b/src/Logging/Message.php @@ -5,6 +5,7 @@ namespace Fykosak\Utils\Logging; use Nette\SmartObject; +use Nette\Utils\Html; class Message { @@ -16,10 +17,10 @@ class Message public const LVL_INFO = 'info'; public const LVL_PRIMARY = 'primary'; - public string $text; + public Html|string $text; public string $level; - public function __construct(string $message, string $level) + public function __construct(Html|string $message, string $level) { $this->text = $message; $this->level = $level; @@ -31,7 +32,7 @@ public function __construct(string $message, string $level) public function __toArray(): array { return [ - 'text' => $this->text, + 'text' => ($this->text instanceof Html) ? $this->text->toHtml() : $this->text, 'level' => $this->level, ]; } From 73282c116c5abfa0f745d10105d29d5dc2694c3f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Kr=C5=A1ka?= Date: Wed, 7 Feb 2024 21:32:51 +0100 Subject: [PATCH 08/31] php7.4 compatibility --- src/Logging/Message.php | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/Logging/Message.php b/src/Logging/Message.php index 2c4f953..1816464 100644 --- a/src/Logging/Message.php +++ b/src/Logging/Message.php @@ -17,10 +17,13 @@ class Message public const LVL_INFO = 'info'; public const LVL_PRIMARY = 'primary'; - public Html|string $text; + public $text; public string $level; - public function __construct(Html|string $message, string $level) + /** + * @param string|Html $message + */ + public function __construct($message, string $level) { $this->text = $message; $this->level = $level; From 52e4cf2290adb3b2a891bbfc4f68a1fcf6137c86 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20Mi=C5=A1o=20=C4=8Cerve=C5=88=C3=A1k?= Date: Wed, 20 Mar 2024 13:56:23 +0100 Subject: [PATCH 09/31] datetime period --- src/DateTime/DateTimePeriod.php | 64 +++++++++++++++++++++++++++++++++ src/DateTime/Period.php | 12 +++++++ 2 files changed, 76 insertions(+) create mode 100644 src/DateTime/DateTimePeriod.php create mode 100644 src/DateTime/Period.php diff --git a/src/DateTime/DateTimePeriod.php b/src/DateTime/DateTimePeriod.php new file mode 100644 index 0000000..d96f4c3 --- /dev/null +++ b/src/DateTime/DateTimePeriod.php @@ -0,0 +1,64 @@ +begin->getTimestamp() > $this->end->getTimestamp()) { + throw new \LogicException(); + } + } + + public function isBefore(?\DateTimeInterface $dateTime = null): bool + { + $timeStamp = $dateTime ? $dateTime->getTimestamp() : time(); + return $this->begin->getTimestamp() > $timeStamp; + } + + public function isAfter(?\DateTimeInterface $dateTime = null): bool + { + $timeStamp = $dateTime ? $dateTime->getTimestamp() : time(); + return $this->end->getTimestamp() < $timeStamp; + } + + public function isOnGoing(?\DateTimeInterface $dateTime = null): bool + { + $timeStamp = $dateTime ? $dateTime->getTimestamp() : time(); + return $this->begin->getTimestamp() <= $timeStamp + && $this->end->getTimestamp() >= $timeStamp; + } + + public function is(Period $period, ?\DateTimeInterface $dateTime = null): bool + { + return match ($period) { + Period::before => $this->isBefore($dateTime), + Period::after => $this->isAfter($dateTime), + Period::onGoing => $this->isOnGoing($dateTime) + }; + } + + public function getPeriod(?\DateTimeInterface $dateTime = null): Period + { + if ($this->isBefore($dateTime)) { + return Period::before; + } + if ($this->isAfter($dateTime)) { + return Period::after; + } + if ($this->isOnGoing($dateTime)) { + return Period::onGoing; + } + throw new \LogicException(); + } + + public function duration(): \DateInterval + { + return $this->end->diff($this->begin); + } +} diff --git a/src/DateTime/Period.php b/src/DateTime/Period.php new file mode 100644 index 0000000..6a41445 --- /dev/null +++ b/src/DateTime/Period.php @@ -0,0 +1,12 @@ + Date: Wed, 20 Mar 2024 13:57:39 +0100 Subject: [PATCH 10/31] renme to phase --- src/DateTime/DateTimePeriod.php | 16 ++++++++-------- src/DateTime/{Period.php => Phase.php} | 2 +- 2 files changed, 9 insertions(+), 9 deletions(-) rename src/DateTime/{Period.php => Phase.php} (91%) diff --git a/src/DateTime/DateTimePeriod.php b/src/DateTime/DateTimePeriod.php index d96f4c3..3826014 100644 --- a/src/DateTime/DateTimePeriod.php +++ b/src/DateTime/DateTimePeriod.php @@ -34,25 +34,25 @@ public function isOnGoing(?\DateTimeInterface $dateTime = null): bool && $this->end->getTimestamp() >= $timeStamp; } - public function is(Period $period, ?\DateTimeInterface $dateTime = null): bool + public function is(Phase $period, ?\DateTimeInterface $dateTime = null): bool { return match ($period) { - Period::before => $this->isBefore($dateTime), - Period::after => $this->isAfter($dateTime), - Period::onGoing => $this->isOnGoing($dateTime) + Phase::before => $this->isBefore($dateTime), + Phase::after => $this->isAfter($dateTime), + Phase::onGoing => $this->isOnGoing($dateTime) }; } - public function getPeriod(?\DateTimeInterface $dateTime = null): Period + public function getPhase(?\DateTimeInterface $dateTime = null): Phase { if ($this->isBefore($dateTime)) { - return Period::before; + return Phase::before; } if ($this->isAfter($dateTime)) { - return Period::after; + return Phase::after; } if ($this->isOnGoing($dateTime)) { - return Period::onGoing; + return Phase::onGoing; } throw new \LogicException(); } diff --git a/src/DateTime/Period.php b/src/DateTime/Phase.php similarity index 91% rename from src/DateTime/Period.php rename to src/DateTime/Phase.php index 6a41445..c6d43ab 100644 --- a/src/DateTime/Period.php +++ b/src/DateTime/Phase.php @@ -4,7 +4,7 @@ namespace Fykosak\Utils\DateTime; -enum Period +enum Phase { case before; case after; From 227fe7be7672f18c8e94bc54e66b686d71dc1c4e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20Mi=C5=A1o=20=C4=8Cerve=C5=88=C3=A1k?= Date: Wed, 20 Mar 2024 14:00:30 +0100 Subject: [PATCH 11/31] rename period --- src/DateTime/{DateTimePeriod.php => Period.php} | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) rename src/DateTime/{DateTimePeriod.php => Period.php} (71%) diff --git a/src/DateTime/DateTimePeriod.php b/src/DateTime/Period.php similarity index 71% rename from src/DateTime/DateTimePeriod.php rename to src/DateTime/Period.php index 3826014..edc9e0b 100644 --- a/src/DateTime/DateTimePeriod.php +++ b/src/DateTime/Period.php @@ -4,34 +4,31 @@ namespace Fykosak\Utils\DateTime; -class DateTimePeriod +class Period { public function __construct( public readonly \DateTimeImmutable $begin, public readonly \DateTimeImmutable $end, ) { - if ($this->begin->getTimestamp() > $this->end->getTimestamp()) { + if ($this->begin > $this->end) { throw new \LogicException(); } } public function isBefore(?\DateTimeInterface $dateTime = null): bool { - $timeStamp = $dateTime ? $dateTime->getTimestamp() : time(); - return $this->begin->getTimestamp() > $timeStamp; + return $this->begin > ($dateTime ?? new \DateTimeImmutable()); } public function isAfter(?\DateTimeInterface $dateTime = null): bool { - $timeStamp = $dateTime ? $dateTime->getTimestamp() : time(); - return $this->end->getTimestamp() < $timeStamp; + return $this->end < ($dateTime ?? new \DateTimeImmutable()); } public function isOnGoing(?\DateTimeInterface $dateTime = null): bool { - $timeStamp = $dateTime ? $dateTime->getTimestamp() : time(); - return $this->begin->getTimestamp() <= $timeStamp - && $this->end->getTimestamp() >= $timeStamp; + return $this->begin <= ($dateTime ?? new \DateTimeImmutable()) + && $this->end >= ($dateTime ?? new \DateTimeImmutable()); } public function is(Phase $period, ?\DateTimeInterface $dateTime = null): bool From 9daae2277e82ef609669709874c54d67f0ca1e40 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20Mi=C5=A1o=20=C4=8Cerve=C5=88=C3=A1k?= Date: Wed, 20 Mar 2024 15:49:36 +0100 Subject: [PATCH 12/31] clean --- src/Price/Currency.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/Price/Currency.php b/src/Price/Currency.php index 0108b5a..de3d495 100644 --- a/src/Price/Currency.php +++ b/src/Price/Currency.php @@ -4,8 +4,6 @@ namespace Fykosak\Utils\Price; -use Nette\NotImplementedException; - enum Currency: string { case EUR = 'EUR'; From 654b02bf034ecb70612954b444b559f67129c4ba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20Mi=C5=A1o=20=C4=8Cerve=C5=88=C3=A1k?= Date: Tue, 3 Sep 2024 21:46:30 +0200 Subject: [PATCH 13/31] Lang map --- src/BaseComponent/BaseComponent.php | 4 +- src/Localization/GettextTranslator.php | 25 ++++++++++--- src/Localization/LangMap.php | 51 ++++++++++++++++++++++++++ src/Localization/LocalizedString.php | 41 ++++----------------- 4 files changed, 81 insertions(+), 40 deletions(-) create mode 100644 src/Localization/LangMap.php diff --git a/src/BaseComponent/BaseComponent.php b/src/BaseComponent/BaseComponent.php index c64d501..4f28d64 100644 --- a/src/BaseComponent/BaseComponent.php +++ b/src/BaseComponent/BaseComponent.php @@ -5,7 +5,8 @@ namespace Fykosak\Utils\BaseComponent; use Fykosak\Utils\Localization\GettextTranslator; -use Nette\Application\UI\{Control, Template}; +use Nette\Application\UI\Control; +use Nette\Application\UI\Template; use Nette\DI\Container; /** @@ -37,6 +38,7 @@ protected function createTemplate(): Template /** @var \Nette\Bridges\ApplicationLatte\Template $template */ $template = parent::createTemplate(); $template->setTranslator($this->translator); + $template->translator = $this->translator; return $template; } } diff --git a/src/Localization/GettextTranslator.php b/src/Localization/GettextTranslator.php index 61cc5df..9189780 100644 --- a/src/Localization/GettextTranslator.php +++ b/src/Localization/GettextTranslator.php @@ -6,15 +6,19 @@ use Nette\Localization\Translator; +/** + * @phpstan-template TLang of string + */ class GettextTranslator implements Translator { - /** @phpstan-var array */ + /** @phpstan-var array */ public array $locales; private string $localeDir; - public ?string $lang = null; + /** @phpstan-var TLang */ + public string $lang; /** - * @phpstan-param array $locales + * @phpstan-param array $locales */ public function __construct(array $locales, string $localeDir) { @@ -24,7 +28,7 @@ public function __construct(array $locales, string $localeDir) /** * - * @param string $lang ISO 639-1 + * @param TLang $lang ISO 639-1 * @throws UnsupportedLanguageException */ public function setLang(string $lang): void @@ -44,19 +48,30 @@ public function setLang(string $lang): void } /** - * @return string[] + * @return TLang[] */ public function getSupportedLanguages(): array { return array_keys($this->locales); } + /** + * @phpstan-template TValue + * @phpstan-param LangMap $map + * @phpstan-return TValue + */ + public function getVariant(LangMap $map) + { + return $map->get($this->lang); + } + /** * @param mixed|string $message * @param string|int $parameters */ public function translate($message, ...$parameters): string { + if ($message === '' || $message === null) { return ''; } diff --git a/src/Localization/LangMap.php b/src/Localization/LangMap.php new file mode 100644 index 0000000..c078507 --- /dev/null +++ b/src/Localization/LangMap.php @@ -0,0 +1,51 @@ + + */ + protected array $variants; + + /** + * @phpstan-param array $variants + */ + public function __construct(array $variants) + { + $this->variants = $variants; + } + + /** + * @phpstan-param TLang $lang + * @phpstan-return TValue + */ + public function __get(string $lang) + { + return $this->get($lang); + } + + /** + * @phpstan-param TLang $lang + * @phpstan-return TValue + */ + public function get(string $lang) + { + return $this->variants[$lang] ?? null; + } + + /** + * @phpstan-return array + */ + public function __serialize(): array + { + return $this->variants; + } +} diff --git a/src/Localization/LocalizedString.php b/src/Localization/LocalizedString.php index 18235b0..2a6552c 100644 --- a/src/Localization/LocalizedString.php +++ b/src/Localization/LocalizedString.php @@ -5,49 +5,22 @@ namespace Fykosak\Utils\Localization; /** - * @template L of string + * @template TLang of string + * @deprecated + * @phpstan-extends LangMap */ -class LocalizedString +class LocalizedString extends LangMap { /** - * @phpstan-var array - */ - private array $texts; - - /** - * @phpstan-param array $texts - */ - public function __construct(array $texts) - { - $this->texts = $texts; - } - - /** - * @phpstan-param L $lang - */ - public function __get(string $lang): ?string - { - return $this->getText($lang); - } - - /** - * @phpstan-param L $lang + * @phpstan-param TLang $lang */ public function getText(string $lang): ?string { - return $this->texts[$lang] ?? null; - } - - /** - * @phpstan-return array - */ - public function __serialize(): array - { - return $this->texts; + return $this->variants[$lang] ?? null; } public function __toString(): string { - return join('/', $this->texts); + return join('/', $this->variants); } } From 8296c40de49fba851a3b14cf2128e70c69d7bba1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20Mi=C5=A1o=20=C4=8Cerve=C5=88=C3=A1k?= Date: Tue, 10 Sep 2024 22:22:20 +0200 Subject: [PATCH 14/31] support for lang array --- src/Localization/GettextTranslator.php | 19 +++++++++++++++---- src/Localization/LocalizedString.php | 26 -------------------------- 2 files changed, 15 insertions(+), 30 deletions(-) delete mode 100644 src/Localization/LocalizedString.php diff --git a/src/Localization/GettextTranslator.php b/src/Localization/GettextTranslator.php index 9189780..b162f81 100644 --- a/src/Localization/GettextTranslator.php +++ b/src/Localization/GettextTranslator.php @@ -57,16 +57,21 @@ public function getSupportedLanguages(): array /** * @phpstan-template TValue - * @phpstan-param LangMap $map + * @phpstan-param array|LangMap $map * @phpstan-return TValue */ - public function getVariant(LangMap $map) + public function getVariant($map) { - return $map->get($this->lang); + if ($map instanceof LangMap) { + return $map->get($this->lang); + } elseif (is_array($map)) { + return $map[$this->lang]; + } + throw new \InvalidArgumentException();//@phpstan-ignore-line } /** - * @param mixed|string $message + * @param array|LangMap|string|null $message * @param string|int $parameters */ public function translate($message, ...$parameters): string @@ -75,6 +80,12 @@ public function translate($message, ...$parameters): string if ($message === '' || $message === null) { return ''; } + if (is_array($message)) { + return (string)$this->getVariant($message); + } + if ($message instanceof LangMap) { + return (string)$message->get($this->lang); + } if (isset($parameters[0])) { return ngettext($message, $message, (int)$parameters[0]); } else { diff --git a/src/Localization/LocalizedString.php b/src/Localization/LocalizedString.php deleted file mode 100644 index 2a6552c..0000000 --- a/src/Localization/LocalizedString.php +++ /dev/null @@ -1,26 +0,0 @@ - - */ -class LocalizedString extends LangMap -{ - /** - * @phpstan-param TLang $lang - */ - public function getText(string $lang): ?string - { - return $this->variants[$lang] ?? null; - } - - public function __toString(): string - { - return join('/', $this->variants); - } -} From a5d30d08378f1493289ebef1a262a871cbdd8323 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20Mi=C5=A1o=20=C4=8Cerve=C5=88=C3=A1k?= Date: Tue, 10 Sep 2024 22:25:01 +0200 Subject: [PATCH 15/31] update WF --- .github/workflows/php-psr.yml | 2 +- .github/workflows/php.yml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/php-psr.yml b/.github/workflows/php-psr.yml index c230aac..6f2de99 100644 --- a/.github/workflows/php-psr.yml +++ b/.github/workflows/php-psr.yml @@ -28,7 +28,7 @@ jobs: name: Test - if: failure() name: Failure output - uses: actions/upload-artifact@v2 + uses: actions/upload-artifact@v4 with: name: output path: tests/**/*.actual diff --git a/.github/workflows/php.yml b/.github/workflows/php.yml index a83b75c..a54cdc9 100644 --- a/.github/workflows/php.yml +++ b/.github/workflows/php.yml @@ -33,7 +33,7 @@ jobs: path: coverage.html - if: failure() name: Failure output - uses: actions/upload-artifact@v2 + uses: actions/upload-artifact@v4 with: name: output path: tests/**/*.actual From c5563c74ba08ab88a423ae56dd190ef185e1ffcd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20Mi=C5=A1o=20=C4=8Cerve=C5=88=C3=A1k?= Date: Tue, 10 Sep 2024 22:26:16 +0200 Subject: [PATCH 16/31] fix no2 --- .github/workflows/php.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/php.yml b/.github/workflows/php.yml index a54cdc9..d20f815 100644 --- a/.github/workflows/php.yml +++ b/.github/workflows/php.yml @@ -27,7 +27,7 @@ jobs: - run: composer run-script testCoverage name: Test - name: Archive code coverage results - uses: actions/upload-artifact@v2 + uses: actions/upload-artifact@v4 with: name: code-coverage-report path: coverage.html From 9f0a2a918ade63b36b476797c15059aafdcc1e40 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20Mi=C5=A1o=20=C4=8Cerve=C5=88=C3=A1k?= Date: Sun, 27 Oct 2024 16:51:58 +0100 Subject: [PATCH 17/31] checkout files --- src/DateTime/Period.php | 61 +++++++++++++++++++++++++++++++++++++++++ src/DateTime/Phase.php | 12 ++++++++ 2 files changed, 73 insertions(+) create mode 100644 src/DateTime/Period.php create mode 100644 src/DateTime/Phase.php diff --git a/src/DateTime/Period.php b/src/DateTime/Period.php new file mode 100644 index 0000000..edc9e0b --- /dev/null +++ b/src/DateTime/Period.php @@ -0,0 +1,61 @@ +begin > $this->end) { + throw new \LogicException(); + } + } + + public function isBefore(?\DateTimeInterface $dateTime = null): bool + { + return $this->begin > ($dateTime ?? new \DateTimeImmutable()); + } + + public function isAfter(?\DateTimeInterface $dateTime = null): bool + { + return $this->end < ($dateTime ?? new \DateTimeImmutable()); + } + + public function isOnGoing(?\DateTimeInterface $dateTime = null): bool + { + return $this->begin <= ($dateTime ?? new \DateTimeImmutable()) + && $this->end >= ($dateTime ?? new \DateTimeImmutable()); + } + + public function is(Phase $period, ?\DateTimeInterface $dateTime = null): bool + { + return match ($period) { + Phase::before => $this->isBefore($dateTime), + Phase::after => $this->isAfter($dateTime), + Phase::onGoing => $this->isOnGoing($dateTime) + }; + } + + public function getPhase(?\DateTimeInterface $dateTime = null): Phase + { + if ($this->isBefore($dateTime)) { + return Phase::before; + } + if ($this->isAfter($dateTime)) { + return Phase::after; + } + if ($this->isOnGoing($dateTime)) { + return Phase::onGoing; + } + throw new \LogicException(); + } + + public function duration(): \DateInterval + { + return $this->end->diff($this->begin); + } +} diff --git a/src/DateTime/Phase.php b/src/DateTime/Phase.php new file mode 100644 index 0000000..c6d43ab --- /dev/null +++ b/src/DateTime/Phase.php @@ -0,0 +1,12 @@ + Date: Sun, 27 Oct 2024 16:56:04 +0100 Subject: [PATCH 18/31] fix tests --- .github/workflows/php-psr.yml | 2 +- .github/workflows/php.yml | 2 +- composer.json | 2 +- src/Localization/GettextTranslator.php | 2 +- src/Localization/LangMap.php | 4 ++-- 5 files changed, 6 insertions(+), 6 deletions(-) diff --git a/.github/workflows/php-psr.yml b/.github/workflows/php-psr.yml index 6f2de99..abecdab 100644 --- a/.github/workflows/php-psr.yml +++ b/.github/workflows/php-psr.yml @@ -9,7 +9,7 @@ jobs: strategy: fail-fast: false matrix: - php: [ '7.4' , '8.0' , '8.1' ] + php: [ '8.1' ] steps: - uses: actions/checkout@v2 name: Checkout diff --git a/.github/workflows/php.yml b/.github/workflows/php.yml index d20f815..9990748 100644 --- a/.github/workflows/php.yml +++ b/.github/workflows/php.yml @@ -9,7 +9,7 @@ jobs: strategy: fail-fast: false matrix: - php: [ '7.4', '8.0', '8.1'] + php: ['8.1'] steps: - uses: actions/checkout@v2 name: Checkout diff --git a/composer.json b/composer.json index 44280f9..0e3c4ef 100644 --- a/composer.json +++ b/composer.json @@ -4,7 +4,7 @@ "type": "library", "license": "GPL-3.0-or-later", "require": { - "php": "7.4.* | 8.0.* | 8.1.* ", + "php": "8.1.* ", "nette/utils": "^v3.2.0", "nette/di": "^3.0.0", "nette/application": "^3.1.0", diff --git a/src/Localization/GettextTranslator.php b/src/Localization/GettextTranslator.php index b162f81..5ec8361 100644 --- a/src/Localization/GettextTranslator.php +++ b/src/Localization/GettextTranslator.php @@ -60,7 +60,7 @@ public function getSupportedLanguages(): array * @phpstan-param array|LangMap $map * @phpstan-return TValue */ - public function getVariant($map) + public function getVariant($map): mixed { if ($map instanceof LangMap) { return $map->get($this->lang); diff --git a/src/Localization/LangMap.php b/src/Localization/LangMap.php index c078507..8a3d4bc 100644 --- a/src/Localization/LangMap.php +++ b/src/Localization/LangMap.php @@ -27,7 +27,7 @@ public function __construct(array $variants) * @phpstan-param TLang $lang * @phpstan-return TValue */ - public function __get(string $lang) + public function __get(string $lang): mixed { return $this->get($lang); } @@ -36,7 +36,7 @@ public function __get(string $lang) * @phpstan-param TLang $lang * @phpstan-return TValue */ - public function get(string $lang) + public function get(string $lang): mixed { return $this->variants[$lang] ?? null; } From 6937482e3c6be8fa150df30b575b43e87600c8fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20Mi=C5=A1o=20=C4=8Cerve=C5=88=C3=A1k?= Date: Sun, 27 Oct 2024 17:22:23 +0100 Subject: [PATCH 19/31] PHP8.1 --- src/Logging/Message.php | 17 +++------- src/Price/Currency.php | 61 ++++------------------------------- src/Price/Price.php | 17 +++------- src/UI/Navigation/NavItem.php | 26 +++------------ src/UI/PageTitle.php | 15 ++++----- src/UI/Title.php | 17 ++++------ 6 files changed, 33 insertions(+), 120 deletions(-) diff --git a/src/Logging/Message.php b/src/Logging/Message.php index 1816464..16442a0 100644 --- a/src/Logging/Message.php +++ b/src/Logging/Message.php @@ -4,29 +4,20 @@ namespace Fykosak\Utils\Logging; -use Nette\SmartObject; use Nette\Utils\Html; class Message { - use SmartObject; - public const LVL_ERROR = 'danger'; public const LVL_WARNING = 'warning'; public const LVL_SUCCESS = 'success'; public const LVL_INFO = 'info'; public const LVL_PRIMARY = 'primary'; - public $text; - public string $level; - - /** - * @param string|Html $message - */ - public function __construct($message, string $level) - { - $this->text = $message; - $this->level = $level; + public function __construct( + public readonly string|Html $text, + public readonly string $level + ) { } /** diff --git a/src/Price/Currency.php b/src/Price/Currency.php index 65e3cbe..c6df72d 100644 --- a/src/Price/Currency.php +++ b/src/Price/Currency.php @@ -4,67 +4,20 @@ namespace Fykosak\Utils\Price; -use Nette\NotImplementedException; -use Nette\SmartObject; - /** * something like enum :) */ -final class Currency +enum Currency: string { - use SmartObject; - - public const EUR = 'EUR'; - public const CZK = 'CZK'; - - public string $value; - - /** - * @throws NotImplementedException - */ - public function __construct(string $currency) - { - $currency = strtoupper($currency); - if (!in_array($currency, [self::EUR, self::CZK])) { - throw new NotImplementedException(sprintf(_('Currency "%s" is not supported'), $currency)); - } - $this->value = $currency; - } - - /** - * @return self[] - */ - public static function cases(): array - { - return [new self(self::CZK), new self(self::EUR)]; - } - - public static function tryFrom(string $currency): ?self - { - try { - return new self($currency); - } catch (NotImplementedException $exception) { - return null; - } - } - - /** - * @throws NotImplementedException - */ - public static function from(string $currency): self - { - return new self($currency); - } + case EUR = 'EUR'; + case CZK = 'CZK'; public function getLabel(): string { - switch ($this->value) { - case self::EUR: - return '€'; - case self::CZK: - return 'Kč'; - } - return ''; + return match ($this) { + self::EUR => '€', + self::CZK => 'Kč', + }; } public function format(float $amount): string diff --git a/src/Price/Price.php b/src/Price/Price.php index 64fae1b..26554ed 100644 --- a/src/Price/Price.php +++ b/src/Price/Price.php @@ -4,22 +4,15 @@ namespace Fykosak\Utils\Price; -use Nette\SmartObject; - -/** - * @phpstan-type TSerializedPrice array{currency:string,amount:float} - */ final class Price { - use SmartObject; - - private Currency $currency; private float $amount; - public function __construct(Currency $currency, ?float $amount = null) - { + public function __construct( + private readonly Currency $currency, + ?float $amount = null + ) { $this->amount = $amount ?? 0; - $this->currency = $currency; } /** @@ -54,7 +47,7 @@ public function __toString(): string } /** - * @phpstan-return TSerializedPrice + * @phpstan-return array{currency:string,amount:float} */ public function __serialize(): array { diff --git a/src/UI/Navigation/NavItem.php b/src/UI/Navigation/NavItem.php index 3683fef..b873300 100644 --- a/src/UI/Navigation/NavItem.php +++ b/src/UI/Navigation/NavItem.php @@ -5,35 +5,19 @@ namespace Fykosak\Utils\UI\Navigation; use Fykosak\Utils\UI\Title; -use Nette\SmartObject; class NavItem { - use SmartObject; - - public string $destination; - /** @phpstan-var array */ - public array $linkParams; - public Title $title; - /** @var NavItem[] */ - public array $children; - public bool $active; - /** * @phpstan-param array $linkParams * @phpstan-param NavItem[] $children */ public function __construct( - Title $title, - string $destination = '#', - array $linkParams = [], - array $children = [], - bool $active = false + public readonly Title $title, + public readonly string $destination = '#', + public readonly array $linkParams = [], + public readonly array $children = [], + public readonly bool $active = false ) { - $this->active = $active; - $this->destination = $destination; - $this->linkParams = $linkParams; - $this->title = $title; - $this->children = $children; } } diff --git a/src/UI/PageTitle.php b/src/UI/PageTitle.php index 983dc7b..9eb9085 100644 --- a/src/UI/PageTitle.php +++ b/src/UI/PageTitle.php @@ -8,17 +8,14 @@ class PageTitle extends Title { - /** @var string|Html|null */ - public $subTitle; - - /** - * @param string|Html $title - * @param string|Html|null $subTitle - */ - public function __construct(?string $id, $title, ?string $icon = null, $subTitle = null) + public function __construct( + ?string $id, + string|Html $title, + ?string $icon = null, + public string|Html|null $subTitle = null + ) { parent::__construct($id, $title, $icon); - $this->subTitle = $subTitle; } public function toHtml(bool $includeSubTitle = false): Html diff --git a/src/UI/Title.php b/src/UI/Title.php index b5fbeb8..5cbaf7b 100644 --- a/src/UI/Title.php +++ b/src/UI/Title.php @@ -9,18 +9,13 @@ class Title { - /** @var string|Html */ - public $title; - public ?string $icon; - public string $id; + public readonly string $id; - /** - * @param string|Html $title - */ - public function __construct(?string $id, $title, ?string $icon = null) - { - $this->title = $title; - $this->icon = $icon; + public function __construct( + ?string $id, + public readonly string|Html $title, + public readonly ?string $icon = null + ) { $this->id = $id ?? Random::generate(10, 'a-z'); } From e4b370e92f8dddd58af8937c1e32fc3abb19e450 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20Mi=C5=A1o=20=C4=8Cerve=C5=88=C3=A1k?= Date: Sun, 27 Oct 2024 17:28:07 +0100 Subject: [PATCH 20/31] Messages PHP8.1 --- src/Logging/Message.php | 10 ++-------- src/Logging/MessageLevel.php | 14 ++++++++++++++ 2 files changed, 16 insertions(+), 8 deletions(-) create mode 100644 src/Logging/MessageLevel.php diff --git a/src/Logging/Message.php b/src/Logging/Message.php index 16442a0..5b06f36 100644 --- a/src/Logging/Message.php +++ b/src/Logging/Message.php @@ -8,15 +8,9 @@ class Message { - public const LVL_ERROR = 'danger'; - public const LVL_WARNING = 'warning'; - public const LVL_SUCCESS = 'success'; - public const LVL_INFO = 'info'; - public const LVL_PRIMARY = 'primary'; - public function __construct( public readonly string|Html $text, - public readonly string $level + public readonly MessageLevel $level ) { } @@ -27,7 +21,7 @@ public function __toArray(): array { return [ 'text' => ($this->text instanceof Html) ? $this->text->toHtml() : $this->text, - 'level' => $this->level, + 'level' => $this->level->value, ]; } } diff --git a/src/Logging/MessageLevel.php b/src/Logging/MessageLevel.php new file mode 100644 index 0000000..c5dacd7 --- /dev/null +++ b/src/Logging/MessageLevel.php @@ -0,0 +1,14 @@ + Date: Sun, 27 Oct 2024 17:33:13 +0100 Subject: [PATCH 21/31] fix tests --- tests/Tests/Price/TestCurrency.phpt | 15 +++++---------- tests/Tests/Price/TestMultiPrice.phpt | 20 ++++++++++---------- tests/Tests/Price/TestPrice.phpt | 10 +++++----- 3 files changed, 20 insertions(+), 25 deletions(-) diff --git a/tests/Tests/Price/TestCurrency.phpt b/tests/Tests/Price/TestCurrency.phpt index 3dfd0a6..5487c1f 100644 --- a/tests/Tests/Price/TestCurrency.phpt +++ b/tests/Tests/Price/TestCurrency.phpt @@ -13,11 +13,6 @@ require_once __DIR__ . '/../FakeBootstrap.php'; class TestCurrency extends BaseTest { - public function testCreateNonExist(): void - { - Assert::exception(fn() => new Currency('XBT'), \Exception::class); - } - public function testCases(): void { foreach (Currency::cases() as $case) { @@ -27,21 +22,21 @@ class TestCurrency extends BaseTest public function testFromExists(): void { - $currency = Currency::from(Currency::CZK); + $currency = Currency::CZK; Assert::type(Currency::class, $currency); - $currencyLower = Currency::from('czk'); + $currencyLower = Currency::from('CZK'); Assert::type(Currency::class, $currencyLower); } public function testTryFromExists(): void { - $currency = Currency::tryFrom(Currency::CZK); + $currency = Currency::CZK; Assert::type(Currency::class, $currency); } public function testFromNonExists(): void { - Assert::exception(fn() => Currency::from('XBT'), \Exception::class); + Assert::exception(fn() => Currency::from('XBT'), \ValueError::class); } public function testTryFromNonExists(): void @@ -52,7 +47,7 @@ class TestCurrency extends BaseTest public function testRender(): void { - $currency = new Currency(Currency::CZK); + $currency = Currency::CZK; Assert::same('2.00 Kč', $currency->format(2.0)); } } diff --git a/tests/Tests/Price/TestMultiPrice.phpt b/tests/Tests/Price/TestMultiPrice.phpt index 48bb789..e6ed837 100644 --- a/tests/Tests/Price/TestMultiPrice.phpt +++ b/tests/Tests/Price/TestMultiPrice.phpt @@ -24,14 +24,14 @@ class TestMultiPrice extends BaseTest public function testCreateFilled(): void { - $multiPrice = new MultiCurrencyPrice([new Price(Currency::from(Currency::CZK))]); + $multiPrice = new MultiCurrencyPrice([new Price(Currency::CZK)]); $price = $multiPrice->czk; Assert::type(Price::class, $price); } public function testGetAccess(): void { - $multiPrice = new MultiCurrencyPrice([new Price(Currency::from(Currency::CZK), 4),]); + $multiPrice = new MultiCurrencyPrice([new Price(Currency::CZK, 4),]); Assert::type(Price::class, $multiPrice->czk); Assert::exception(fn() => $multiPrice->eur, \OutOfRangeException::class); Assert::type(Price::class, $multiPrice->CZK); @@ -39,32 +39,32 @@ class TestMultiPrice extends BaseTest public function testSetAccess(): void { - $multiPrice = new MultiCurrencyPrice([new Price(Currency::from(Currency::CZK), 4),]); - $multiPrice->czk = new Price(Currency::from(Currency::CZK), 2); + $multiPrice = new MultiCurrencyPrice([new Price(Currency::CZK, 4),]); + $multiPrice->czk = new Price(Currency::CZK, 2); Assert::type(Price::class, $multiPrice->czk); Assert::same(2.0, $multiPrice->czk->getAmount()); Assert::exception( - fn() => $multiPrice->czk = new Price(Currency::from(Currency::EUR)), + fn() => $multiPrice->czk = new Price(Currency::EUR), \LogicException::class ); Assert::exception( - fn() => $multiPrice->CZK = new Price(Currency::from(Currency::EUR)), + fn() => $multiPrice->CZK = new Price(Currency::EUR), \LogicException::class ); Assert::exception( - fn() => $multiPrice->eur = new Price(Currency::from(Currency::EUR)), + fn() => $multiPrice->eur = new Price(Currency::EUR), \OutOfRangeException::class ); } public function testCreateSum(): void { - $multiPrice1 = new MultiCurrencyPrice([new Price(Currency::from(Currency::CZK), 2)]); + $multiPrice1 = new MultiCurrencyPrice([new Price(Currency::CZK, 2)]); $multiPrice2 = new MultiCurrencyPrice( [ - new Price(Currency::from(Currency::CZK), 1), - new Price(Currency::from(Currency::EUR), 4), + new Price(Currency::CZK, 1), + new Price(Currency::EUR, 4), ] ); diff --git a/tests/Tests/Price/TestPrice.phpt b/tests/Tests/Price/TestPrice.phpt index e73e58c..c4ad895 100644 --- a/tests/Tests/Price/TestPrice.phpt +++ b/tests/Tests/Price/TestPrice.phpt @@ -16,8 +16,8 @@ class TestPrice extends BaseTest { public function testAddSame(): void { - $price1 = new Price(new Currency(Currency::CZK), 2); - $price2 = new Price(new Currency(Currency::CZK), 4); + $price1 = new Price(Currency::CZK, 2); + $price2 = new Price(Currency::CZK, 4); $price1->add($price2); Assert::same(6.0, $price1->getAmount()); Assert::same(4.0, $price2->getAmount()); @@ -25,14 +25,14 @@ class TestPrice extends BaseTest public function testNotSame(): void { - $price1 = new Price(new Currency(Currency::CZK), 2); - $price2 = new Price(new Currency(Currency::EUR), 4); + $price1 = new Price(Currency::CZK, 2); + $price2 = new Price(Currency::EUR, 4); Assert::exception(fn() => $price1->add($price2), \LogicException::class); } public function testAdd(): void { - $price1 = new Price(new Currency(Currency::CZK), 2); + $price1 = new Price(Currency::CZK, 2); $price1->addAmount(3.5); Assert::same(5.5, $price1->getAmount()); } From c839878c9cc4608375624bfa23c0d20d22167551 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20Mi=C5=A1o=20=C4=8Cerve=C5=88=C3=A1k?= Date: Sun, 27 Oct 2024 17:58:25 +0100 Subject: [PATCH 22/31] try to fix tests --- src/Price/MultiCurrencyPrice.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Price/MultiCurrencyPrice.php b/src/Price/MultiCurrencyPrice.php index 22e25b5..d642d8f 100644 --- a/src/Price/MultiCurrencyPrice.php +++ b/src/Price/MultiCurrencyPrice.php @@ -76,7 +76,7 @@ public function add(self $multiPrice): void public function __get(string $name): ?Price { - return $this->getPrice(Currency::from($name)); + return $this->getPrice(Currency::from(strtoupper($name))); } public function __set(string $name, Price $value): void From 524e4c8c85e75540524008e1c4026c62b4f3b3ff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20Mi=C5=A1o=20=C4=8Cerve=C5=88=C3=A1k?= Date: Sun, 27 Oct 2024 20:23:08 +0100 Subject: [PATCH 23/31] PSR --- src/UI/PageTitle.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/UI/PageTitle.php b/src/UI/PageTitle.php index 9eb9085..eb451e4 100644 --- a/src/UI/PageTitle.php +++ b/src/UI/PageTitle.php @@ -13,8 +13,7 @@ public function __construct( string|Html $title, ?string $icon = null, public string|Html|null $subTitle = null - ) - { + ) { parent::__construct($id, $title, $icon); } From 841652d815093dedadd0a57652fe9eef7e735428 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20Mi=C5=A1o=20=C4=8Cerve=C5=88=C3=A1k?= Date: Sun, 27 Oct 2024 20:26:39 +0100 Subject: [PATCH 24/31] fix === --- src/Price/Price.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Price/Price.php b/src/Price/Price.php index 26554ed..354ae1e 100644 --- a/src/Price/Price.php +++ b/src/Price/Price.php @@ -20,7 +20,7 @@ public function __construct( */ public function add(Price $price): void { - if ($this->currency->value !== $price->getCurrency()->value) { + if ($this->currency !== $price->getCurrency()) { throw new \LogicException('Currencies are not a same'); } $this->amount += $price->getAmount(); From a4689b5be3f4a0620be676de77ccb6eca62e6d88 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20Mi=C5=A1o=20=C4=8Cerve=C5=88=C3=A1k?= Date: Sun, 27 Oct 2024 20:46:12 +0100 Subject: [PATCH 25/31] Rename BaseCompoent --- src/{BaseComponent => Components}/@layout.latte | 0 .../DIComponent.php} | 16 +++++----------- src/UI/Navigation/NavigationItemComponent.php | 4 ++-- tests/Tests/BaseComponent/DummyComponent.php | 4 ++-- 4 files changed, 9 insertions(+), 15 deletions(-) rename src/{BaseComponent => Components}/@layout.latte (100%) rename src/{BaseComponent/BaseComponent.php => Components/DIComponent.php} (71%) diff --git a/src/BaseComponent/@layout.latte b/src/Components/@layout.latte similarity index 100% rename from src/BaseComponent/@layout.latte rename to src/Components/@layout.latte diff --git a/src/BaseComponent/BaseComponent.php b/src/Components/DIComponent.php similarity index 71% rename from src/BaseComponent/BaseComponent.php rename to src/Components/DIComponent.php index 4f28d64..451412d 100644 --- a/src/BaseComponent/BaseComponent.php +++ b/src/Components/DIComponent.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace Fykosak\Utils\BaseComponent; +namespace Fykosak\Utils\Components; use Fykosak\Utils\Localization\GettextTranslator; use Nette\Application\UI\Control; @@ -12,22 +12,16 @@ /** * @property \Nette\Bridges\ApplicationLatte\Template $template */ -abstract class BaseComponent extends Control +abstract class DIComponent extends Control { - protected Container $container; protected ?GettextTranslator $translator; - public function __construct(Container $container) - { - $this->container = $container; + public function __construct( + protected readonly Container $container + ) { $container->callInjects($this); } - protected function getContext(): Container - { - return $this->container; - } - public function injectTranslator(?GettextTranslator $translator): void { $this->translator = $translator; diff --git a/src/UI/Navigation/NavigationItemComponent.php b/src/UI/Navigation/NavigationItemComponent.php index 5db1ad2..97a33a9 100644 --- a/src/UI/Navigation/NavigationItemComponent.php +++ b/src/UI/Navigation/NavigationItemComponent.php @@ -4,13 +4,13 @@ namespace Fykosak\Utils\UI\Navigation; -use Fykosak\Utils\BaseComponent\BaseComponent; +use Fykosak\Utils\BaseComponent\DIComponent; /** * Compatible with bootstrap >=5.0 * tested version (5.0,5.1) */ -class NavigationItemComponent extends BaseComponent +class NavigationItemComponent extends DIComponent { public function render(NavItem $item): void { diff --git a/tests/Tests/BaseComponent/DummyComponent.php b/tests/Tests/BaseComponent/DummyComponent.php index 95a59a6..517bc03 100644 --- a/tests/Tests/BaseComponent/DummyComponent.php +++ b/tests/Tests/BaseComponent/DummyComponent.php @@ -4,10 +4,10 @@ namespace Fykosak\Utils\Tests\BaseComponent; -use Fykosak\Utils\BaseComponent\BaseComponent; +use Fykosak\Utils\BaseComponent\DIComponent; use Fykosak\Utils\Tests\DummyService; -class DummyComponent extends BaseComponent +class DummyComponent extends DIComponent { public DummyService $dummyService; From 27d42b145557553ecef5f8171b041a2493def386 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20Mi=C5=A1o=20=C4=8Cerve=C5=88=C3=A1k?= Date: Sun, 27 Oct 2024 21:11:19 +0100 Subject: [PATCH 26/31] fix tests --- tests/Tests/BaseComponent/DummyComponent.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Tests/BaseComponent/DummyComponent.php b/tests/Tests/BaseComponent/DummyComponent.php index 517bc03..fa1ba2b 100644 --- a/tests/Tests/BaseComponent/DummyComponent.php +++ b/tests/Tests/BaseComponent/DummyComponent.php @@ -4,7 +4,7 @@ namespace Fykosak\Utils\Tests\BaseComponent; -use Fykosak\Utils\BaseComponent\DIComponent; +use Fykosak\Utils\Components\DIComponent; use Fykosak\Utils\Tests\DummyService; class DummyComponent extends DIComponent From 679de49ac3a57c187d0da79b414b14492fc6c6a6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20Mi=C5=A1o=20=C4=8Cerve=C5=88=C3=A1k?= Date: Sun, 27 Oct 2024 21:26:47 +0100 Subject: [PATCH 27/31] keep back changes --- src/Localization/GettextTranslator.php | 51 ++++++++++++------- src/Localization/LangMap.php | 6 +-- .../UnsupportedLanguageException.php | 8 +-- 3 files changed, 37 insertions(+), 28 deletions(-) diff --git a/src/Localization/GettextTranslator.php b/src/Localization/GettextTranslator.php index 51a0942..b162f81 100644 --- a/src/Localization/GettextTranslator.php +++ b/src/Localization/GettextTranslator.php @@ -11,19 +11,19 @@ */ class GettextTranslator implements Translator { - /** @phpstan-var \BackedEnum&LangEnum */ - public LangEnum & \BackedEnum $lang; - - /** @phpstan-var array */ public array $locales; + private string $localeDir; + /** @phpstan-var TLang */ + public string $lang; /** - * @phpstan-param class-string|string $langEnumClass + * @phpstan-param array $locales */ - public function __construct( - private readonly string $langEnumClass, - ) { + public function __construct(array $locales, string $localeDir) + { + $this->locales = $locales; + $this->localeDir = $localeDir; } /** @@ -31,30 +31,43 @@ public function __construct( * @param TLang $lang ISO 639-1 * @throws UnsupportedLanguageException */ - public function setLang(LangEnum & \BackedEnum $lang): void + public function setLang(string $lang): void { - if (!$lang instanceof $this->langEnumClass) { + $this->lang = $lang; + if (!isset($this->locales[$lang])) { throw new UnsupportedLanguageException($lang); } - $this->lang = $lang; - + $locale = $this->locales[$lang]; - putenv('LANGUAGE=' . $lang->getLocale()); // for the sake of CLI tests - setlocale(LC_MESSAGES, $lang->getLocale()); - setlocale(LC_TIME, $lang->getLocale()); - bindtextdomain('messages', $lang->getLocaleDir()); + putenv('LANGUAGE=' . $locale); // for the sake of CLI tests + setlocale(LC_MESSAGES, $locale); + setlocale(LC_TIME, $locale); + bindtextdomain('messages', $this->localeDir); bind_textdomain_codeset('messages', 'utf-8'); textdomain('messages'); } + /** + * @return TLang[] + */ + public function getSupportedLanguages(): array + { + return array_keys($this->locales); + } + /** * @phpstan-template TValue * @phpstan-param array|LangMap $map * @phpstan-return TValue */ - public function getVariant(array|LangMap $map) + public function getVariant($map) { - $this->lang->getVariant($map); + if ($map instanceof LangMap) { + return $map->get($this->lang); + } elseif (is_array($map)) { + return $map[$this->lang]; + } + throw new \InvalidArgumentException();//@phpstan-ignore-line } /** @@ -71,7 +84,7 @@ public function translate($message, ...$parameters): string return (string)$this->getVariant($message); } if ($message instanceof LangMap) { - return (string)$this->getVariant($message); + return (string)$message->get($this->lang); } if (isset($parameters[0])) { return ngettext($message, $message, (int)$parameters[0]); diff --git a/src/Localization/LangMap.php b/src/Localization/LangMap.php index 22d5514..c078507 100644 --- a/src/Localization/LangMap.php +++ b/src/Localization/LangMap.php @@ -24,10 +24,10 @@ public function __construct(array $variants) } /** - * @phpstan-param TLang $key + * @phpstan-param TLang $lang * @phpstan-return TValue */ - public function __get(string $lang): mixed + public function __get(string $lang) { return $this->get($lang); } @@ -36,7 +36,7 @@ public function __get(string $lang): mixed * @phpstan-param TLang $lang * @phpstan-return TValue */ - public function get(string $lang): mixed + public function get(string $lang) { return $this->variants[$lang] ?? null; } diff --git a/src/Localization/UnsupportedLanguageException.php b/src/Localization/UnsupportedLanguageException.php index 6ffff7e..98def66 100644 --- a/src/Localization/UnsupportedLanguageException.php +++ b/src/Localization/UnsupportedLanguageException.php @@ -9,12 +9,8 @@ class UnsupportedLanguageException extends \Exception { - public function __construct(LangEnum & \BackedEnum $lang, ?Throwable $previous = null) + public function __construct(string $lang, ?Throwable $previous = null) { - parent::__construct( - sprintf(_('Language %s is not supported'), $lang->value), - IResponse::S400_BAD_REQUEST, - $previous - ); + parent::__construct(sprintf(_('Language %s is not supported'), $lang), IResponse::S400_BAD_REQUEST, $previous); } } From a09a9b26653ec9e7724e9aa0ac75ef9286671c36 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20Mi=C5=A1o=20=C4=8Cerve=C5=88=C3=A1k?= Date: Sun, 27 Oct 2024 21:38:27 +0100 Subject: [PATCH 28/31] fix tests --- src/Localization/GettextTranslator.php | 16 ++++++---------- src/Localization/LangEnum.php | 20 -------------------- src/Localization/LangMap.php | 4 ++-- src/Localization/LangTrait.php | 25 ------------------------- src/Price/Price.php | 2 +- tests/config.neon | 2 +- 6 files changed, 10 insertions(+), 59 deletions(-) delete mode 100644 src/Localization/LangEnum.php delete mode 100644 src/Localization/LangTrait.php diff --git a/src/Localization/GettextTranslator.php b/src/Localization/GettextTranslator.php index b162f81..a55268c 100644 --- a/src/Localization/GettextTranslator.php +++ b/src/Localization/GettextTranslator.php @@ -11,19 +11,16 @@ */ class GettextTranslator implements Translator { - /** @phpstan-var array */ - public array $locales; - private string $localeDir; /** @phpstan-var TLang */ public string $lang; /** * @phpstan-param array $locales */ - public function __construct(array $locales, string $localeDir) - { - $this->locales = $locales; - $this->localeDir = $localeDir; + public function __construct( + public readonly array $locales, + private readonly string $localeDir + ) { } /** @@ -60,14 +57,13 @@ public function getSupportedLanguages(): array * @phpstan-param array|LangMap $map * @phpstan-return TValue */ - public function getVariant($map) + public function getVariant(array|LangMap $map): mixed { if ($map instanceof LangMap) { return $map->get($this->lang); - } elseif (is_array($map)) { + } else { return $map[$this->lang]; } - throw new \InvalidArgumentException();//@phpstan-ignore-line } /** diff --git a/src/Localization/LangEnum.php b/src/Localization/LangEnum.php deleted file mode 100644 index 26292d0..0000000 --- a/src/Localization/LangEnum.php +++ /dev/null @@ -1,20 +0,0 @@ - - */ - public static function getLocales(): array; - - public function getLocaleDir(): string; - - public function getLocale(): string; -} diff --git a/src/Localization/LangMap.php b/src/Localization/LangMap.php index c078507..8a3d4bc 100644 --- a/src/Localization/LangMap.php +++ b/src/Localization/LangMap.php @@ -27,7 +27,7 @@ public function __construct(array $variants) * @phpstan-param TLang $lang * @phpstan-return TValue */ - public function __get(string $lang) + public function __get(string $lang): mixed { return $this->get($lang); } @@ -36,7 +36,7 @@ public function __get(string $lang) * @phpstan-param TLang $lang * @phpstan-return TValue */ - public function get(string $lang) + public function get(string $lang): mixed { return $this->variants[$lang] ?? null; } diff --git a/src/Localization/LangTrait.php b/src/Localization/LangTrait.php deleted file mode 100644 index aa036ce..0000000 --- a/src/Localization/LangTrait.php +++ /dev/null @@ -1,25 +0,0 @@ -|LangMap $map - * @phpstan-return TValue - */ - final public function getVariant(array|LangMap $map): mixed - { - if ($map instanceof LangMap) { - return $map->get($this->value); - } else { - return $map[$this->value]; - } - } -} diff --git a/src/Price/Price.php b/src/Price/Price.php index 675c764..1650f27 100644 --- a/src/Price/Price.php +++ b/src/Price/Price.php @@ -9,7 +9,7 @@ final class Price private float $amount; public function __construct( - private readonly Currency $currency, + public readonly Currency $currency, ?float $amount = null ) { $this->amount = $amount ?? 0; diff --git a/tests/config.neon b/tests/config.neon index 7f8e2b1..42b0ee0 100644 --- a/tests/config.neon +++ b/tests/config.neon @@ -1,4 +1,4 @@ services: - Fykosak\Utils\Tests\DummyService - - Fykosak\Utils\Localization\GettextTranslator(Fykosak\Utils\Tests\DummyLang) + - Fykosak\Utils\Localization\GettextTranslator([],'') From bb1847e3e38a1db9c0c430fbce4d0ba1e6b09c90 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20Mi=C5=A1o=20=C4=8Cerve=C5=88=C3=A1k?= Date: Sun, 27 Oct 2024 21:44:01 +0100 Subject: [PATCH 29/31] cleanup --- src/Price/Price.php | 2 +- tests/Tests/DummyLang.php | 26 -------------------------- 2 files changed, 1 insertion(+), 27 deletions(-) delete mode 100644 tests/Tests/DummyLang.php diff --git a/src/Price/Price.php b/src/Price/Price.php index 1650f27..9355495 100644 --- a/src/Price/Price.php +++ b/src/Price/Price.php @@ -20,7 +20,7 @@ public function __construct( */ public function add(Price $price): void { - if ($this->currency !== $price->getCurrency()) { + if ($this->currency !== $price->currency) { throw new \LogicException('Currencies are not a same'); } $this->amount += $price->getAmount(); diff --git a/tests/Tests/DummyLang.php b/tests/Tests/DummyLang.php deleted file mode 100644 index e94f2c6..0000000 --- a/tests/Tests/DummyLang.php +++ /dev/null @@ -1,26 +0,0 @@ - Date: Sun, 27 Oct 2024 21:46:50 +0100 Subject: [PATCH 30/31] fix tests --- tests/Tests/Price/TestCurrency.phpt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Tests/Price/TestCurrency.phpt b/tests/Tests/Price/TestCurrency.phpt index dea91e8..5eb6dac 100644 --- a/tests/Tests/Price/TestCurrency.phpt +++ b/tests/Tests/Price/TestCurrency.phpt @@ -43,7 +43,7 @@ class TestCurrency extends BaseTest public function testRender(): void { $currency = Currency::CZK; - Assert::same('2.00 Kč', $currency->format(2.0)); + Assert::same('2.00Kč', $currency->format(2.0)); } } From f2cfc6632507e5325311c1d53e0a4000db70b28f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20Mi=C5=A1o=20=C4=8Cerve=C5=88=C3=A1k?= Date: Sun, 27 Oct 2024 21:49:05 +0100 Subject: [PATCH 31/31] Fix math space --- tests/Tests/Price/TestCurrency.phpt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Tests/Price/TestCurrency.phpt b/tests/Tests/Price/TestCurrency.phpt index 5eb6dac..fb8cf3b 100644 --- a/tests/Tests/Price/TestCurrency.phpt +++ b/tests/Tests/Price/TestCurrency.phpt @@ -43,7 +43,7 @@ class TestCurrency extends BaseTest public function testRender(): void { $currency = Currency::CZK; - Assert::same('2.00Kč', $currency->format(2.0)); + Assert::same('2.00 Kč', $currency->format(2.0)); } }