Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion Tests/Unit/Translator/EditInPlaceTranslatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,32 @@
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Translation\Loader\ArrayLoader;
use Symfony\Component\Translation\TranslatorInterface;
use Symfony\Contracts\Translation\TranslatorInterface as NewTranslatorInterface;
use Translation\Bundle\EditInPlace\ActivatorInterface;
use Translation\Bundle\Translator\EditInPlaceTranslator;
use Translation\Bundle\Translator\TranslatorInterface;

/**
* @author Damien Alexandre <dalexandre@jolicode.com>
*/
final class EditInPlaceTranslatorTest extends TestCase
{
public function testWithNotLocaleAwareTranslator()
{
if (!\interface_exists(NewTranslatorInterface::class)) {
$this->markTestSkipped('Relevant only when NewTranslatorInterface is available.');
}

$symfonyTranslator = $this->getMockBuilder(NewTranslatorInterface::class)->getMock();
$activator = new FakeActivator(true);
$requestStack = new RequestStack();

$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('The given translator must implements LocaleAwareInterface.');

new EditInPlaceTranslator($symfonyTranslator, $activator, $requestStack);
}

public function testEnabled(): void
{
$symfonyTranslator = $this->getMockBuilder(TranslatorInterface::class)->getMock();
Expand Down
18 changes: 17 additions & 1 deletion Tests/Unit/Translator/FallbackTranslatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@

use Nyholm\NSA;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Translation\TranslatorInterface;
use Symfony\Contracts\Translation\TranslatorInterface as NewTranslatorInterface;
use Translation\Bundle\Translator\FallbackTranslator;
use Translation\Bundle\Translator\TranslatorInterface;
use Translation\Translator\Translator;
use Translation\Translator\TranslatorService;

Expand All @@ -23,6 +24,21 @@
*/
final class FallbackTranslatorTest extends TestCase
{
public function testWithNotLocaleAwareTranslator()
{
if (!\interface_exists(NewTranslatorInterface::class)) {
$this->markTestSkipped('Relevant only when NewTranslatorInterface is available.');
}

$symfonyTranslator = $this->getMockBuilder(NewTranslatorInterface::class)->getMock();
$translator = new Translator();

$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('The given translator must implements LocaleAwareInterface.');

new FallbackTranslator('en', $symfonyTranslator, $translator);
}

public function testTranslateWithSubstitutedParameters(): void
{
$symfonyTranslator = $this->getMockBuilder(TranslatorInterface::class)->getMock();
Expand Down
23 changes: 19 additions & 4 deletions Translator/EditInPlaceTranslator.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,20 @@
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Translation\MessageCatalogueInterface;
use Symfony\Component\Translation\TranslatorBagInterface;
use Symfony\Component\Translation\TranslatorInterface;
use Symfony\Component\Translation\TranslatorInterface as LegacyTranslatorInterface;
use Symfony\Contracts\Translation\LocaleAwareInterface;
use Symfony\Contracts\Translation\TranslatorInterface as NewTranslatorInterface;
use Translation\Bundle\EditInPlace\ActivatorInterface;

/**
* Custom Translator for HTML rendering only (output `<x-trans>` tags).
*
* @author Damien Alexandre <dalexandre@jolicode.com>
*/
final class EditInPlaceTranslator implements TranslatorInterface, TranslatorBagInterface
final class EditInPlaceTranslator implements TranslatorInterface
{
/**
* @var TranslatorInterface|TranslatorBagInterface
* @var LegacyTranslatorInterface|NewTranslatorInterface
*/
private $translator;

Expand All @@ -39,8 +41,21 @@ final class EditInPlaceTranslator implements TranslatorInterface, TranslatorBagI
*/
private $requestStack;

public function __construct(TranslatorInterface $translator, ActivatorInterface $activator, RequestStack $requestStack)
/**
* $translator param can't be type hinted as we have to deal with both LegacyTranslatorInterface & NewTranslatorInterface.
* Once we won't support sf ^3.4 anymore, we will be able to type hint $translator with NewTranslatorInterface.
*
* @param LegacyTranslatorInterface|NewTranslatorInterface $translator
*/
public function __construct($translator, ActivatorInterface $activator, RequestStack $requestStack)
{
if (!$translator instanceof LegacyTranslatorInterface && !$translator instanceof LocaleAwareInterface) {
throw new \InvalidArgumentException('The given translator must implements LocaleAwareInterface.');
}
if (!$translator instanceof TranslatorBagInterface) {
throw new \InvalidArgumentException('The given translator must implements TranslatorBagInterface.');
}

$this->translator = $translator;
$this->activator = $activator;
$this->requestStack = $requestStack;
Expand Down
24 changes: 20 additions & 4 deletions Translator/FallbackTranslator.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,20 @@

use Symfony\Component\Translation\MessageCatalogueInterface;
use Symfony\Component\Translation\TranslatorBagInterface;
use Symfony\Component\Translation\TranslatorInterface;
use Symfony\Component\Translation\TranslatorInterface as LegacyTranslatorInterface;
use Symfony\Contracts\Translation\LocaleAwareInterface;
use Symfony\Contracts\Translation\TranslatorInterface as NewTranslatorInterface;
use Translation\Translator\Translator;

/**
* This is a bridge between Symfony's translator service and Translation\Translator\Translator.
*
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
*/
final class FallbackTranslator implements TranslatorInterface, TranslatorBagInterface
final class FallbackTranslator implements TranslatorInterface
{
/**
* @var TranslatorInterface|TranslatorBagInterface
* @var LegacyTranslatorInterface|NewTranslatorInterface
*/
private $symfonyTranslator;

Expand All @@ -38,8 +40,22 @@ final class FallbackTranslator implements TranslatorInterface, TranslatorBagInte
*/
private $defaultLocale;

public function __construct(string $defaultLocale, TranslatorInterface $symfonyTranslator, Translator $externalTranslator)
/**
* $symfonyTranslator param can't be type hinted as we have to deal with both LegacyTranslatorInterface & NewTranslatorInterface.
* Once we won't support sf ^3.4 anymore, we will be able to type hint $symfonyTranslator with NewTranslatorInterface.
*
* @param LegacyTranslatorInterface|NewTranslatorInterface $symfonyTranslator
*/
public function __construct(string $defaultLocale, $symfonyTranslator, Translator $externalTranslator)
{
if (!$symfonyTranslator instanceof LegacyTranslatorInterface && !$symfonyTranslator instanceof LocaleAwareInterface) {
throw new \InvalidArgumentException('The given translator must implements LocaleAwareInterface.');
}

if (!$symfonyTranslator instanceof TranslatorBagInterface) {
throw new \InvalidArgumentException('The given translator must implements TranslatorBagInterface.');
}

$this->symfonyTranslator = $symfonyTranslator;
$this->externalTranslator = $externalTranslator;
$this->defaultLocale = $defaultLocale;
Expand Down
35 changes: 35 additions & 0 deletions Translator/TranslatorInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

/*
* This file is part of the PHP Translation package.
*
* (c) PHP Translation team <tobias.nyholm@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Translation\Bundle\Translator;

use Symfony\Component\Translation\TranslatorBagInterface;
use Symfony\Component\Translation\TranslatorInterface as LegacyTranslatorInterface;
use Symfony\Contracts\Translation\LocaleAwareInterface;
use Symfony\Contracts\Translation\TranslatorInterface as NewTranslatorInterface;

/*
* This interface is here to allow us to support both sf 3.x with
* LegacyTranslatorInterface & sf 5.x where this interface have been replaced
* by NewLocalAwareInterface.
*
* When sf 3.4 won't be supported anymore, this interface will become useless.
*/

if (\interface_exists(NewTranslatorInterface::class)) {
interface TranslatorInterface extends NewTranslatorInterface, LocaleAwareInterface, TranslatorBagInterface
{
}
} else {
interface TranslatorInterface extends LegacyTranslatorInterface, TranslatorBagInterface
{
}
}
4 changes: 4 additions & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,7 @@ parameters:
-
path: %currentWorkingDirectory%/DependencyInjection/Configuration.php
message: '#Call to an undefined method Symfony\\Component\\Config\\Definition\\Builder\\TreeBuilder::root()#'

-
path: %currentWorkingDirectory%/Translator
message: '#Call to an undefined method Symfony\\Component\\Translation\\TranslatorInterface|Symfony\\Contracts\\Translation\\TranslatorInterface::getCatalogue().#'