Skip to content

Commit

Permalink
Remove dependency on Nette\Http\Request (#131)
Browse files Browse the repository at this point in the history
* Fix QA

* Fix tests (nette/utils version detection)

* Remove dependency on Nette\Http\Request
  • Loading branch information
Rixafy authored Oct 19, 2024
1 parent 8dedfb0 commit 6d3e42d
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 152 deletions.
4 changes: 2 additions & 2 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,15 @@ 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
path: src/DI/TranslationExtension.php

-
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
Expand Down
25 changes: 10 additions & 15 deletions src/LocalesResolvers/Header.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -11,44 +10,40 @@
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;
}

public function resolve(
Translator $translator
): ?string
{
/** @var array<string> $langs */
$langs = [];
/** @var array<string> $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);
}

}
134 changes: 0 additions & 134 deletions tests/Tests/LocalesResolvers/HeaderTest.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<string>
*/
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<string>
*/
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<string> $availableLocales
*/
Expand Down
2 changes: 1 addition & 1 deletion tests/Tests/TestAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand Down

0 comments on commit 6d3e42d

Please sign in to comment.