Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add eztext support #18

Merged
merged 4 commits into from
Oct 31, 2024
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
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);
}
}
Loading