|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Matecat\SubFiltering; |
|
4
|
|
|
|
|
5
|
|
|
use Matecat\SubFiltering\Commons\Pipeline; |
|
6
|
|
|
use Matecat\SubFiltering\Filters\DollarCurlyBrackets; |
|
7
|
|
|
use Matecat\SubFiltering\Filters\SingleCurlyBracketsToPh; |
|
8
|
|
|
use Matecat\SubFiltering\Filters\SmartCounts; |
|
9
|
|
|
use Matecat\SubFiltering\Filters\TwigToPh; |
|
10
|
|
|
use Matecat\SubFiltering\Filters\PercentDoubleCurlyBrackets; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* A specific filter implementation tailored for MyMemory services. |
|
14
|
|
|
* |
|
15
|
|
|
* This class extends the base `AbstractFilter` to provide custom, client-specific |
|
16
|
|
|
* filtering logic by dynamically modifying the transformation pipeline. It is designed |
|
17
|
|
|
* to handle variations in placeholder syntax and filtering requirements from different |
|
18
|
|
|
* clients (such as Airbnb, Roblox, etc.). |
|
19
|
|
|
* |
|
20
|
|
|
* It uses a Client ID (`cid`) passed during the transformation to adjust the |
|
21
|
|
|
* pipeline on the fly, ensuring the correct set of handlers is used for each case. |
|
22
|
|
|
* |
|
23
|
|
|
* @package Matecat\SubFiltering |
|
24
|
|
|
*/ |
|
25
|
|
|
class MyMemoryFilter extends AbstractFilter { |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Converts a segment from Layer 0 format to Layer 1 format, applying an optional client-specific pipeline configuration. |
|
29
|
|
|
* |
|
30
|
|
|
* This method processes the given segment through a transformation pipeline, which may be customized |
|
31
|
|
|
* based on the provided Client ID (`cid`) to handle specific placeholder or syntax rules. |
|
32
|
|
|
* |
|
33
|
|
|
* @param string $segment The segment in Layer 0 format to be transformed. |
|
34
|
|
|
* @param string|null $cid An optional Client ID for customizing the transformation pipeline. If provided. |
|
35
|
|
|
* this adjusts the pipeline to account for client-specific rules. |
|
36
|
|
|
* |
|
37
|
|
|
* @return string The transformed segment in Layer 1 format. |
|
38
|
|
|
*/ |
|
39
|
12 |
|
public function fromLayer0ToLayer1( string $segment, ?string $cid = null ): string { |
|
40
|
|
|
|
|
41
|
12 |
|
$channel = new Pipeline( $this->source, $this->target, $this->dataRefMap ); |
|
42
|
|
|
|
|
43
|
12 |
|
$this->configureFromLayer0ToLayer1Pipeline( $channel, $cid ); |
|
44
|
|
|
|
|
45
|
|
|
// Process the segment and return the result. |
|
46
|
12 |
|
return $channel->transform( $segment ); |
|
47
|
|
|
|
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Transforms a segment from Layer 0 to Layer 1, applying client-specific rules. |
|
52
|
|
|
* |
|
53
|
|
|
* This method overrides the parent implementation to dynamically adjust the |
|
54
|
|
|
* `fromLayer0ToLayer1Pipeline` based on the provided Client ID (`cid`). This allows for |
|
55
|
|
|
* tailored placeholder handling for different clients before the main transformation occurs. |
|
56
|
|
|
* |
|
57
|
|
|
* @param Pipeline $channel |
|
58
|
|
|
* @param string|null $cid An optional Client ID to trigger specific filtering rules: |
|
59
|
|
|
* - 'airbnb': Adds `SmartCounts` handler for advanced variable processing. |
|
60
|
|
|
* - 'roblox': Adds `SingleCurlyBracketsToPh` handler for `{placeholder}` style variables. |
|
61
|
|
|
* - 'familysearch': Removes `TwigToPh` and adds `SingleCurlyBracketsToPh`. |
|
62
|
|
|
* |
|
63
|
|
|
*/ |
|
64
|
20 |
|
protected function configureFromLayer0ToLayer1Pipeline( Pipeline $channel, ?string $cid = null ): void { |
|
65
|
|
|
|
|
66
|
20 |
|
parent::configureFromLayer0ToLayer1Pipeline( $channel ); |
|
67
|
|
|
|
|
68
|
|
|
switch ( $cid ) { |
|
69
|
20 |
|
case 'airbnb': |
|
70
|
|
|
// For Airbnb, add the SmartCounts handler to process specific variable syntax |
|
71
|
|
|
// that looks like `%{smart_count}`. |
|
72
|
4 |
|
if ( !$channel->contains( SmartCounts::class ) ) { |
|
73
|
4 |
|
$channel->addAfter( PercentDoubleCurlyBrackets::class, SmartCounts::class ); |
|
74
|
|
|
} |
|
75
|
4 |
|
break; |
|
76
|
|
|
|
|
77
|
16 |
|
case 'roblox': |
|
78
|
|
|
// For Roblox, add a handler to convert single curly bracket placeholders |
|
79
|
|
|
// (e.g., `{placeholder}`) into standard <ph> tags. |
|
80
|
3 |
|
if ( !$channel->contains( SingleCurlyBracketsToPh::class ) ) { |
|
81
|
2 |
|
$channel->addAfter( DollarCurlyBrackets::class, SingleCurlyBracketsToPh::class ); |
|
82
|
|
|
} |
|
83
|
3 |
|
break; |
|
84
|
|
|
|
|
85
|
13 |
|
case 'familysearch': |
|
86
|
|
|
// For FamilySearch, customize the pipeline by removing Twig support and adding |
|
87
|
|
|
// support for single curly bracket placeholders. |
|
88
|
2 |
|
if ( !$channel->contains( SingleCurlyBracketsToPh::class ) ) { |
|
89
|
1 |
|
$channel->remove( TwigToPh::class ); |
|
90
|
1 |
|
$channel->addAfter( DollarCurlyBrackets::class, SingleCurlyBracketsToPh::class ); |
|
91
|
|
|
} |
|
92
|
2 |
|
break; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
|
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
} |