Skip to content

Commit

Permalink
Handling ph tags with dataRef attribute without any correspondence on
Browse files Browse the repository at this point in the history
dataRef map
  • Loading branch information
Mauro Cassani committed Jun 9, 2021
1 parent e2c9dbe commit eea147a
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 0 deletions.
60 changes: 60 additions & 0 deletions src/Filters/DataRefReplace.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Matecat\SubFiltering\Filters;

use Matecat\SubFiltering\Commons\AbstractHandler;
use Matecat\XliffParser\Utils\HtmlParser;
use Matecat\XliffParser\XliffUtils\DataRefReplacer;

class DataRefReplace extends AbstractHandler {
Expand Down Expand Up @@ -30,15 +31,74 @@ public function transform( $segment )
{
// dataRefMap is present only in xliff 2.0 files
if ( empty( $this->dataRefMap ) ) {
$segment = $this->replaceXliffPhTagsWithNoDataRefToMatecatPhTags($segment);

return $this->replaceXliffPcTagsToMatecatPhTags($segment);
}

$dataRefReplacer = new DataRefReplacer( $this->dataRefMap );
$segment = $dataRefReplacer->replace( $segment );
$segment = $this->replaceXliffPhTagsWithNoDataRefToMatecatPhTags($segment);

return $this->replaceXliffPcTagsToMatecatPhTags($segment);
}

/**
* This function replace encoded ph tags (from Xliff 2.0) without any dataRef correspondence
* to regular Matecat <ph> tag for UI presentation
*
* Example:
*
* We can control who sees content when with <ph id="source1" dataRef="source1"/>Visibility Constraints.
*
* is transformed to:
*
* We can control who sees content when with &lt;ph id="mtc_ph_u_1" equiv-text="base64:Jmx0O3BoIGlkPSJzb3VyY2UxIiBkYXRhUmVmPSJzb3VyY2UxIi8mZ3Q7"/&gt;Visibility Constraints.
*
* @param $segment
*
* @return string|string[]
*/
private function replaceXliffPhTagsWithNoDataRefToMatecatPhTags($segment)
{
preg_match_all('/&lt;(ph .*?)&gt;/iu', $segment, $phTags);

if(count($phTags[0]) === 0) {
return $segment;
}

$phIndex = 1;

foreach ($phTags[0] as $phTag){
// check if phTag has not any correspondence on dataRef map
if($this->isAPhTagWithNoDataRefCorrespondence($phTag)){
$phMatecat = '&lt;ph id="mtc_ph_u_'.$phIndex.'" equiv-text="base64:'.base64_encode($phTag).'"/&gt;';
$segment = str_replace($phTag, $phMatecat, $segment);
$phIndex++;
}
}

return $segment;
}

/**
* This function checks if a ph tag with dataRef attribute a correspondence on dataRef map
*
* @param string $phTag
*
* @return bool
*/
private function isAPhTagWithNoDataRefCorrespondence($phTag)
{
$parsed = HtmlParser::parse($phTag);

return (
isset($parsed[0]) and
isset($parsed[0]->attributes['dataRef']) and
!array_key_exists($parsed[0]->attributes['dataRef'], $this->dataRefMap
));
}

/**
* This function replace encoded pc tags (from Xliff 2.0) without any dataRef correspondence
* to regular Matecat <ph> tag for UI presentation
Expand Down
19 changes: 19 additions & 0 deletions tests/MateCatSubFilteringTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,25 @@ public function testTwigUngreedy()
**************************
*/

public function testPhWithoutDataRef()
{
$db_segment = 'We can control who sees content when with <ph id="source1" dataRef="source1"/>Visibility Constraints.';
$Filter = MateCatFilter::getInstance( new FeatureSet(), 'en-EN','et-ET', [] );

$expected_l1_segment = 'We can control who sees content when with <ph id="source1" dataRef="source1"/>Visibility Constraints.';
$expected_l2_segment = 'We can control who sees content when with &lt;ph id="mtc_ph_u_1" equiv-text="base64:Jmx0O3BoIGlkPSJzb3VyY2UxIiBkYXRhUmVmPSJzb3VyY2UxIi8mZ3Q7"/&gt;Visibility Constraints.';

$l1_segment = $Filter->fromLayer0ToLayer1( $db_segment );
$l2_segment = $Filter->fromLayer1ToLayer2( $l1_segment );

$this->assertEquals($l1_segment, $expected_l1_segment);
$this->assertEquals($l2_segment, $expected_l2_segment);

$back_to_db_segment =$Filter->fromLayer1ToLayer0($l1_segment);

$this->assertEquals($back_to_db_segment, $db_segment);
}

/**
* @throws \Exception
*/
Expand Down

0 comments on commit eea147a

Please sign in to comment.