Skip to content

Commit

Permalink
Add eztext support (#18)
Browse files Browse the repository at this point in the history
* 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
reithor and konradoboza authored Oct 31, 2024
1 parent 0e1f1ef commit 74250dc
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/bundle/Resources/config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ services:
tags: [ ibexa.automated_translation.field_encoder ]

# field encoder
Ibexa\AutomatedTranslation\Encoder\Field\TextBlockFieldEncoder: ~

Ibexa\AutomatedTranslation\Encoder\Field\TextLineFieldEncoder: ~

Ibexa\AutomatedTranslation\Encoder\Field\RichTextFieldEncoder:
Expand Down
44 changes: 44 additions & 0 deletions src/lib/Encoder/Field/TextBlockFieldEncoder.php
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);
}
}
46 changes: 46 additions & 0 deletions tests/lib/Encoder/Field/TextBlockFieldEncoderTest.php
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);
}
}

0 comments on commit 74250dc

Please sign in to comment.