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 1/8] 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 2/8] 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 3/8] 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 4/8] 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 5/8] 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 6/8] 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 7/8] 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 8/8] 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;