ObjectiveCNSString   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

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

1 Method

Rating   Name   Duplication   Size   Complexity  
A transform() 0 23 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 80
    public function transform( string $segment ): string {
39
40
//        $sprintfLocker = new SprintfLocker( $this->pipeline->getSource(), $this->pipeline->getTarget() );
41
42
//         placeholding
43
//        $segment = $sprintfLocker->lock( $segment );
44
45
46 80
        preg_match_all( '/%\d+\$@|%@/', $segment, $html, PREG_SET_ORDER );
47 80
        foreach ( $html as $percentNumberSnailVariable ) {
48
49 4
            $segment = preg_replace(
50 4
                    '/' . preg_quote( $percentNumberSnailVariable[ 0 ], '/' ) . '/',
51 4
                    '<ph id="' . $this->getPipeline()->getNextId() . '" ctype="' . CTypeEnum::OBJECTIVE_C_NSSTRING . '" equiv-text="base64:' . base64_encode( $percentNumberSnailVariable[ 0 ] ) . '"/>',
52 4
                    $segment,
53 4
                    1
54 4
            );
55
        }
56
57
        //revert placeholding
58
//        return $sprintfLocker->unlock( $segment );
59
60 80
        return $segment;
61
62
    }
63
}