-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add eztext support * Add test for eztext support * Update tests/lib/Encoder/Field/TextBlockFieldEncoderTest.php Co-authored-by: Konrad Oboza <[email protected]> * Add service definition for TextBlockFieldEncoder --------- Co-authored-by: Konrad Oboza <[email protected]>
- Loading branch information
1 parent
0e1f1ef
commit 74250dc
Showing
3 changed files
with
92 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?php | ||
|
||
/** | ||
* @copyright Copyright (C) Ibexa AS. All rights reserved. | ||
* @license For full copyright and license information view LICENSE file distributed with this source code. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Ibexa\AutomatedTranslation\Encoder\Field; | ||
|
||
use Ibexa\AutomatedTranslation\Exception\EmptyTranslatedFieldException; | ||
use Ibexa\Contracts\AutomatedTranslation\Encoder\Field\FieldEncoderInterface; | ||
use Ibexa\Contracts\Core\Repository\Values\Content\Field; | ||
use Ibexa\Core\FieldType\TextBlock\Value as TextBlockValue; | ||
use Ibexa\Core\FieldType\Value; | ||
|
||
final class TextBlockFieldEncoder implements FieldEncoderInterface | ||
{ | ||
public function canEncode(Field $field): bool | ||
{ | ||
return $field->value instanceof TextBlockValue; | ||
} | ||
|
||
public function canDecode(string $type): bool | ||
{ | ||
return TextBlockValue::class === $type; | ||
} | ||
|
||
public function encode(Field $field): string | ||
{ | ||
return (string) $field->value; | ||
} | ||
|
||
public function decode(string $value, $previousFieldValue): Value | ||
{ | ||
$value = trim($value); | ||
|
||
if (strlen($value) === 0) { | ||
throw new EmptyTranslatedFieldException(); | ||
} | ||
|
||
return new TextBlockValue($value); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?php | ||
|
||
/** | ||
* @copyright Copyright (C) Ibexa AS. All rights reserved. | ||
* @license For full copyright and license information view LICENSE file distributed with this source code. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Ibexa\Tests\AutomatedTranslation\Encoder\Field; | ||
|
||
use Ibexa\AutomatedTranslation\Encoder\Field\TextBlockFieldEncoder; | ||
use Ibexa\Contracts\Core\Repository\Values\Content\Field; | ||
use Ibexa\Core\FieldType\TextBlock; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
final class TextBlockFieldEncoderTest extends TestCase | ||
{ | ||
private const TEXT_BLOCK_VALUE = "Some text.\nSome more text."; | ||
|
||
public function testEncode(): void | ||
{ | ||
$field = new Field([ | ||
'fieldDefIdentifier' => 'field_1_TextBlock', | ||
'value' => new TextBlock\Value(self::TEXT_BLOCK_VALUE), | ||
]); | ||
|
||
$subject = new TextBlockFieldEncoder(); | ||
$result = $subject->encode($field); | ||
|
||
$this->assertEquals(self::TEXT_BLOCK_VALUE, $result); | ||
} | ||
|
||
public function testDecode(): void | ||
{ | ||
$field = new Field([ | ||
'fieldDefIdentifier' => 'field_1_TextBlock', | ||
'value' => new TextBlock\Value(self::TEXT_BLOCK_VALUE), | ||
]); | ||
|
||
$subject = new TextBlockFieldEncoder(); | ||
$result = $subject->decode(self::TEXT_BLOCK_VALUE, $field->value); | ||
|
||
$this->assertInstanceOf(TextBlock\Value::class, $result); | ||
$this->assertEquals(new TextBlock\Value(self::TEXT_BLOCK_VALUE), $result); | ||
} | ||
} |