From 6d3e42db4dbdc461879dbea6912925e1f8583b03 Mon Sep 17 00:00:00 2001 From: Rixafy <45132928+Rixafy@users.noreply.github.com> Date: Sat, 19 Oct 2024 16:48:29 +0200 Subject: [PATCH] Remove dependency on Nette\Http\Request (#131) * Fix QA * Fix tests (nette/utils version detection) * Remove dependency on Nette\Http\Request --- phpstan.neon | 4 +- src/LocalesResolvers/Header.php | 25 ++-- tests/Tests/LocalesResolvers/HeaderTest.phpt | 134 ------------------- tests/Tests/TestAbstract.php | 2 +- 4 files changed, 13 insertions(+), 152 deletions(-) diff --git a/phpstan.neon b/phpstan.neon index 4bb67f39..0402587f 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -38,7 +38,7 @@ parameters: # ------------------------------------------------------------------- - message: """ - #^Fetching class constant class of deprecated class Nette\\\\Bridges\\\\ApplicationLatte\\\\ILatteFactory\\: + #^Fetching class constant class of deprecated interface Nette\\\\Bridges\\\\ApplicationLatte\\\\ILatteFactory\\: use Nette\\\\Bridges\\\\ApplicationLatte\\\\LatteFactory$# """ count: 1 @@ -46,7 +46,7 @@ parameters: - message: """ - #^Fetching class constant class of deprecated class Nette\\\\Localization\\\\ITranslator\\: + #^Fetching class constant class of deprecated interface Nette\\\\Localization\\\\ITranslator\\: use Nette\\\\Localization\\\\Translator$# """ count: 2 diff --git a/src/LocalesResolvers/Header.php b/src/LocalesResolvers/Header.php index a0d65d84..264c2810 100644 --- a/src/LocalesResolvers/Header.php +++ b/src/LocalesResolvers/Header.php @@ -2,7 +2,6 @@ namespace Contributte\Translation\LocalesResolvers; -use Contributte\Translation\Exceptions\InvalidArgument; use Contributte\Translation\Translator; use Nette\Http\IRequest; use Nette\Http\Request; @@ -11,19 +10,12 @@ class Header implements ResolverInterface { - private Request $httpRequest; + private IRequest $httpRequest; - /** - * @throws \Contributte\Translation\Exceptions\InvalidArgument - */ public function __construct( IRequest $httpRequest ) { - if (!is_a($httpRequest, Request::class, true)) { - throw new InvalidArgument('Header locale resolver need "Nette\\Http\\Request" or his child for using "detectLanguage" method.'); - } - $this->httpRequest = $httpRequest; } @@ -31,24 +23,27 @@ public function resolve( Translator $translator ): ?string { - /** @var array $langs */ - $langs = []; + /** @var array $locales */ + $locales = []; foreach ($translator->getAvailableLocales() as $v1) { - $langs[] = $v1; + $locales[] = $v1; if (Strings::length($v1) < 3) { continue; } - $langs[] = Strings::substring($v1, 0, 2);// en_US => en + $locales[] = Strings::substring($v1, 0, 2);// en_US => en } - if (count($langs) === 0) { + if (count($locales) === 0) { return null; } - return $this->httpRequest->detectLanguage($langs); + return (new Request( + $this->httpRequest->getUrl(), + headers: $this->httpRequest->getHeaders() + ))->detectLanguage($locales); } } diff --git a/tests/Tests/LocalesResolvers/HeaderTest.phpt b/tests/Tests/LocalesResolvers/HeaderTest.phpt index 4a44e2b9..d9325399 100644 --- a/tests/Tests/LocalesResolvers/HeaderTest.phpt +++ b/tests/Tests/LocalesResolvers/HeaderTest.phpt @@ -2,13 +2,10 @@ namespace Tests\LocalesResolvers; -use Contributte\Translation\Exceptions\InvalidArgument; use Contributte\Translation\LocalesResolvers\Header; use Contributte\Translation\Translator; use Mockery; -use Nette\Http\IRequest; use Nette\Http\Request; -use Nette\Http\UrlImmutable; use Nette\Http\UrlScript; use Tester\Assert; use Tests\TestAbstract; @@ -31,137 +28,6 @@ final class HeaderTest extends TestAbstract Assert::same('en-us', $this->resolve('da, en_us', ['en', 'en-us'])); } - public function test02(): void - { - Assert::exception(static function (): void { - new Header(new class implements IRequest { - - public function getReferer(): ?UrlImmutable - { - return null; - } - - public function isSameSite(): bool - { - return true; - } - - public function getUrl(): UrlScript - { - return new UrlScript(); - } - - /** - * @return mixed - */ - public function getQuery( - ?string $key = null - ) - { - return null; - } - - /** - * @return mixed - */ - public function getPost( - ?string $key = null - ) - { - return null; - } - - /** - * @return mixed - */ - public function getFile( - string $key - ) - { - return null; - } - - /** - * @return array<\Nette\Http\FileUpload> - */ - public function getFiles(): array - { - return []; - } - - /** - * @return mixed - */ - public function getCookie( - string $key - ) - { - return null; - } - - /** - * @return array - */ - public function getCookies(): array - { - return []; - } - - public function getMethod(): string - { - return ''; - } - - public function isMethod( - string $method - ): bool - { - return true; - } - public function getHeader( - string $header - ): ?string - { - return null; - } - - /** - * @return array - */ - public function getHeaders(): array - { - return []; - } - - public function isSecured(): bool - { - return true; - } - - public function isAjax(): bool - { - return true; - } - - public function getRemoteAddress(): ?string - { - return null; - } - - public function getRemoteHost(): ?string - { - return null; - } - - public function getRawBody(): ?string - { - return null; - } - - }); - }, InvalidArgument::class, 'Header locale resolver need "' . Request::class . '" or his child for using "detectLanguage" method.'); - } - /** * @param array $availableLocales */ diff --git a/tests/Tests/TestAbstract.php b/tests/Tests/TestAbstract.php index 4c620cf3..b46a69ba 100644 --- a/tests/Tests/TestAbstract.php +++ b/tests/Tests/TestAbstract.php @@ -20,7 +20,7 @@ public function __construct( ) { if (class_exists('\Composer\InstalledVersions')) { // Composer 2 - $netteUtilsVersion = InstalledVersions::getPrettyVersion('nette/utils'); + $netteUtilsVersion = ltrim(InstalledVersions::getPrettyVersion('nette/utils'), 'v'); } else { // Composer 1 $composerRaw = FileSystem::read(__DIR__ . '/../../composer.lock');