CharsetHelper::registerTranscoder

(PHP 7 >= 7.4.0, PHP 8)

CharsetHelper::registerTranscoder — Register custom transcoding strategy

Description

public static CharsetHelper::registerTranscoder(
    TranscoderInterface|callable $transcoder,
    ?int $priority = null
): void

Registers a custom transcoding provider in the Chain of Responsibility. Allows extending CharsetHelper with custom encoding conversion strategies without modifying the core.

Parameters

transcoder:

TranscoderInterface instance or callable with signature:

function(string $data, string $to, string $from, ?array $options): ?string

The transcoder must return the converted string on success, or null to pass to the next transcoder in the chain.

priority:

Optional priority override (null = use transcoder's default priority). Higher values execute first. Default priorities: UConverter (100), iconv (50), mbstring (10).

Return Values

No value is returned.

Errors/Exceptions

Throws InvalidArgumentException if transcoder is not a TranscoderInterface instance or callable.

Examples

Example #1 Register custom transcoder with TranscoderInterface

<?php
use Ducks\Component\EncodingRepair\CharsetHelper;
use Ducks\Component\EncodingRepair\Transcoder\TranscoderInterface;

class MyCustomTranscoder implements TranscoderInterface
{
    public function transcode(string $data, string $to, string $from, array $options): ?string
    {
        if ($from === 'MY-CUSTOM-ENCODING') {
            return myCustomConversion($data, $to);
        }
        return null;
    }

    public function getPriority(): int
    {
        return 75;
    }

    public function isAvailable(): bool
    {
        return true;
    }
}

CharsetHelper::registerTranscoder(new MyCustomTranscoder());
CharsetHelper::registerTranscoder(new MyCustomTranscoder(), 150); // Override priority

Example #2 Register callable transcoder

<?php
use Ducks\Component\EncodingRepair\CharsetHelper;

CharsetHelper::registerTranscoder(
    function (string $data, string $to, string $from, ?array $options): ?string {
        if ($from === 'MY-CUSTOM-ENCODING') {
            return myCustomConversion($data, $to);
        }
        return null;
    },
    150  // Priority
);

$result = CharsetHelper::toCharset($data, 'UTF-8', 'MY-CUSTOM-ENCODING');

Notes

Transcoders are executed in priority order (highest first):

  • Priority 100+: Custom high-priority transcoders
  • Priority 100: UConverter (ext-intl)
  • Priority 50: iconv
  • Priority 10: mbstring
  • Priority 0-9: Custom low-priority transcoders

See Also