Skip to content

Commit

Permalink
Remove dependency on Nette\Http\Request
Browse files Browse the repository at this point in the history
  • Loading branch information
Rixafy committed Oct 15, 2024
1 parent f76d3e9 commit d417370
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 149 deletions.
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

0 comments on commit d417370

Please sign in to comment.