Skip to content
Draft
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
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"require": {
"php": "^7.4 || ^8.0",
"ext-libxml": "*",
"ext-dom": "*",
"symfony/yaml": "^5.0",
"symfony/routing": "^5.0",
"symfony/console": "^5.0",
Expand Down
7 changes: 7 additions & 0 deletions phpstan-baseline-7.4.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
parameters:
ignoreErrors:
-
message: '#^Parameter \#2 \$callback of function array_filter expects \(callable\(mixed\)\: bool\)\|null, Closure\(mixed\)\: \(array\|null\) given\.$#'
identifier: argument.type
count: 1
path: src/bundle/DependencyInjection/IbexaAutomatedTranslationExtension.php
7 changes: 7 additions & 0 deletions phpstan-baseline-gte-8.0.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
parameters:
ignoreErrors:
-
message: '#^Parameter \#2 \$callback of function array_filter expects \(callable\(mixed\)\: bool\)\|null, Closure\(mixed\)\: array given\.$#'
identifier: argument.type
count: 1
path: src/bundle/DependencyInjection/IbexaAutomatedTranslationExtension.php
18 changes: 12 additions & 6 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
parameters:
ignoreErrors:
-
message: '#^Parameter \#2 \$callback of function array_filter expects \(callable\(mixed\)\: bool\)\|null, Closure\(mixed\)\: \(array\|null\) given\.$#'
identifier: argument.type
count: 1
path: src/bundle/DependencyInjection/IbexaAutomatedTranslationExtension.php

-
message: '#^Access to protected property Ibexa\\AdminUi\\Form\\Data\\ContentTranslationData\:\:\$content\.$#'
identifier: property.protected
Expand All @@ -30,6 +24,18 @@ parameters:
count: 1
path: src/bundle/Form/TranslationAddDataTransformer.php

-
message: '#^Call to an undefined method DOMNode\:\:getAttribute\(\)\.$#'
identifier: method.notFound
count: 1
path: src/lib/Encoder.php

-
message: '#^Call to an undefined method DOMNode\:\:getAttribute\(\)\.$#'
identifier: method.notFound
count: 1
path: src/lib/Encoder/Field/PageBuilderFieldEncoder.php

-
message: '#^Cannot call method setValue\(\) on Ibexa\\Contracts\\FieldTypePage\\FieldType\\LandingPage\\Model\\Attribute\|null\.$#'
identifier: method.nonObject
Expand Down
19 changes: 19 additions & 0 deletions phpstan-baseline.neon.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?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);

$includes = [];
if (PHP_VERSION_ID < 80000) {
$includes[] = __DIR__ . '/phpstan-baseline-7.4.neon';
} else {
$includes[] = __DIR__ . '/phpstan-baseline-gte-8.0.neon';
}

$config = [];
$config['includes'] = $includes;

return $config;
1 change: 1 addition & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
includes:
- vendor/phpstan/phpstan-phpunit/extension.neon
- vendor/phpstan/phpstan-symfony/extension.neon
- phpstan-baseline.neon.php
- phpstan-baseline.neon

parameters:
Expand Down
35 changes: 35 additions & 0 deletions src/lib/Encoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@

namespace Ibexa\AutomatedTranslation;

use DOMCdataSection;
use DOMDocument;
use DOMXPath;
use Ibexa\AutomatedTranslation\Encoder\Field\FieldEncoderManager;
use Ibexa\AutomatedTranslation\Exception\EmptyTranslatedFieldException;
use Ibexa\Bundle\AutomatedTranslation\Event\FieldDecodeEvent;
Expand All @@ -18,7 +21,9 @@
use Ibexa\Contracts\Core\Repository\Values\Content\Content;
use Ibexa\Contracts\Core\Repository\Values\Content\Field;
use Ibexa\Core\FieldType\Value;
use Ibexa\FieldTypeRichText\FieldType\RichText\Value as RichTextValue;
use InvalidArgumentException;
use RuntimeException;
use Symfony\Component\Serializer\Encoder\XmlEncoder;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;

Expand Down Expand Up @@ -118,6 +123,36 @@ public function encode(Content $content): string

$encoder = new XmlEncoder();
$payload = $encoder->encode($results, XmlEncoder::FORMAT);

$dom = new DOMDocument();
$dom->loadXML($payload);
$xpath = new DOMXPath($dom);
$textNodes = $xpath->query('//text()');
if ($textNodes !== false) {
foreach ($textNodes as $textNode) {
if ($textNode instanceof DOMCdataSection) {
$parent = $textNode->parentNode;
if ($parent === null) {
continue;
}

$type = $parent->getAttribute('type');

if ($type !== RichTextValue::class) {
$newText = $dom->createTextNode($textNode->data);
$parent->replaceChild($newText, $textNode);
}
}
}
$payload = $dom->saveXML();

if (!$payload) {
throw new RuntimeException(
sprintf('Saving XML failed after removing CDATA, error: %s', preg_last_error_msg())
);
}
}

// here Encoder has decorated with CDATA, we don't want the CDATA
return str_replace(
['<![CDATA[', ']]>'],
Expand Down
34 changes: 34 additions & 0 deletions src/lib/Encoder/Field/PageBuilderFieldEncoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,19 @@

namespace Ibexa\AutomatedTranslation\Encoder\Field;

use DOMCdataSection;
use DOMDocument;
use DOMXPath;
use Ibexa\AutomatedTranslation\Encoder\BlockAttribute\BlockAttributeEncoderManager;
use Ibexa\AutomatedTranslation\Exception\EmptyTranslatedAttributeException;
use Ibexa\Contracts\AutomatedTranslation\Encoder\Field\FieldEncoderInterface;
use Ibexa\Contracts\Core\Repository\Values\Content\Field;
use Ibexa\Core\FieldType\Value as APIValue;
use Ibexa\FieldTypePage\FieldType\LandingPage\Value;
use Ibexa\FieldTypePage\FieldType\Page\Block\Definition\BlockDefinitionFactoryInterface;
use Ibexa\FieldTypeRichText\FieldType\RichText\Value as RichTextValue;
use InvalidArgumentException;
use RuntimeException;
use Symfony\Component\Serializer\Encoder\XmlEncoder;

final class PageBuilderFieldEncoder implements FieldEncoderInterface
Expand Down Expand Up @@ -84,6 +89,35 @@ public function encode(Field $field): string
XmlEncoder::ROOT_NODE_NAME => 'blocks',
]);

$dom = new DOMDocument();
$dom->loadXML($payload);
$xpath = new DOMXPath($dom);
$textNodes = $xpath->query('//text()');
if ($textNodes !== false) {
foreach ($textNodes as $textNode) {
if ($textNode instanceof DOMCdataSection) {
$parent = $textNode->parentNode;
if ($parent === null) {
continue;
}

$type = $parent->getAttribute('type');

if ($type !== RichTextValue::class) {
$newText = $dom->createTextNode($textNode->data);
$parent->replaceChild($newText, $textNode);
}
}
}
$payload = $dom->saveXML();

if (!$payload) {
throw new RuntimeException(
sprintf('Saving XML failed after removing CDATA, error: %s', preg_last_error_msg())
);
}
}

$payload = str_replace('<?xml version="1.0"?>' . "\n", '', $payload);

$payload = str_replace(
Expand Down
Loading