Test Failed
Pull Request — master (#51)
by Domenico
02:39
created

ObjectiveCNSString   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 27
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 10
c 1
b 0
f 0
dl 0
loc 27
ccs 12
cts 12
cp 1
rs 10
wmc 2

1 Method

Rating   Name   Duplication   Size   Complexity  
A transform() 0 21 2
1
<?php
2
3
/**
4
 * The ObjectiveCNSString class is a filter handler that processes Objective-C NSString
5
 * format specifiers within a segment to convert them into placeholder XML tags
6
 * for translation and reassembly purposes.
7
 *
8
 * It uses the `SprintfLocker` to temporarily lock and unlock sprintf placeholders
9
 * to prevent misinterpretation during transformation.
10
 *
11
 * The transformation includes detecting and converting NSString format specifiers
12
 * (e.g., `%@` or `%2$@`) into a `<ph>` tag with attributes providing contextual information.
13
 * These tags are given unique identifiers and base64-encoded representations of the original specifiers.
14
 */
15
16
namespace Matecat\SubFiltering\Filters;
17
18
use Matecat\SubFiltering\Commons\AbstractHandler;
19
use Matecat\SubFiltering\Enum\CTypeEnum;
20
use Matecat\SubFiltering\Filters\Sprintf\SprintfLocker;
21
22
/**
23
 * Handles Objective-C NSString placeholders within segments for processing.
24
 *
25
 * This class processes Objective-C NSString placeholders (such as `%@` or `%1$@`)
26
 * by locking, transforming, and unlocking them in a segment of text. The transformation
27
 * replaces placeholders with XML-compliant placeholder tags containing additional
28
 * metadata like ctype and base64-encoded original content.
29
 *
30
 * Extends the AbstractHandler to use its pipeline and processing capabilities.
31
 */
32
class ObjectiveCNSString extends AbstractHandler {
33
    /**
34
     * @see https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/Strings/Articles/formatSpecifiers.html#//apple_ref/doc/uid/TP40004265-SW2
35
     *
36
     * @inheritDoc
37
     */
38 86
    public function transform( string $segment ): string {
39
40 86
        $sprintfLocker = new SprintfLocker( $this->pipeline->getSource(), $this->pipeline->getTarget() );
41
42
        // placeholding
43 86
        $segment = $sprintfLocker->lock( $segment );
44
45
46 86
        preg_match_all( '/%\d+\$@|%@/', $segment, $html, PREG_SET_ORDER );
47 86
        foreach ( $html as $percentNumberSnailVariable ) {
48
49 5
            $segment = preg_replace(
50 5
                    '/' . preg_quote( $percentNumberSnailVariable[ 0 ], '/' ) . '/',
51 5
                    '<ph id="' . $this->getPipeline()->getNextId() . '" ctype="' . CTypeEnum::OBJECTIVE_C_NSSTRING . '" equiv-text="base64:' . base64_encode( $percentNumberSnailVariable[ 0 ] ) . '"/>',
52 5
                    $segment,
53 5
                    1
54 5
            );
55
        }
56
57
        //revert placeholding
58 86
        return $sprintfLocker->unlock( $segment );
59
    }
60
}