diff --git a/src/Gateway/KlickTippGateway.php b/src/Gateway/KlickTippGateway.php index e603cf9..143bd43 100644 --- a/src/Gateway/KlickTippGateway.php +++ b/src/Gateway/KlickTippGateway.php @@ -4,13 +4,14 @@ namespace Fenepedia\ContaoKlickTippGateway\Gateway; +use Codefog\HasteBundle\StringParser; use Contao\CoreBundle\Framework\ContaoFramework; use Contao\StringUtil; use Contao\Validator; use Fenepedia\ContaoKlickTippGateway\Config\KlickTippConfig; use Fenepedia\ContaoKlickTippGateway\Exception\KlickTippGatewayException; use Fenepedia\ContaoKlickTippGateway\Parcel\Stamp\KlickTippConfigStamp; -use Fenepedia\ContaoKlickTippGateway\Util; +use Haste\Util\StringUtil as HasteStringUtil; use Kazin8\KlickTipp\Connector; use Psr\Log\LoggerInterface; use Terminal42\NotificationCenterBundle\Exception\Parcel\CouldNotDeliverParcelException; @@ -29,6 +30,7 @@ public function __construct( private readonly Connector $connector, private readonly LoggerInterface $contaoGeneralLogger, private readonly LoggerInterface $contaoErrorLogger, + private readonly StringParser|null $stringParser = null, ) { } @@ -75,7 +77,7 @@ private function createKlickTippConfigStamp(Parcel $parcel): KlickTippConfigStam $messageConfig = $parcel->getMessageConfig(); $tokens = $parcel->getStamp(TokenCollectionStamp::class)->tokenCollection->forSimpleTokenParser(); - $email = Util::recursiveReplaceTokensAndTags($messageConfig->getString('kt_email'), $tokens); + $email = $this->recursiveReplaceTokensAndTags($messageConfig->getString('kt_email'), $tokens); if (!$email || !Validator::isEmail($email)) { throw new KlickTippGatewayException('Invalid email address "'.$email.'" given.'); @@ -84,8 +86,8 @@ private function createKlickTippConfigStamp(Parcel $parcel): KlickTippConfigStam $parameters = []; foreach (StringUtil::deserialize($messageConfig->getString('kt_parameters'), true) as $param) { - $key = Util::recursiveReplaceTokensAndTags((string) $param['key'], $tokens); - $value = Util::recursiveReplaceTokensAndTags((string) $param['value'], $tokens); + $key = $this->recursiveReplaceTokensAndTags((string) $param['key'], $tokens); + $value = $this->recursiveReplaceTokensAndTags((string) $param['value'], $tokens); // Do some type casting if (is_numeric($value)) { @@ -203,4 +205,20 @@ private function getTagId(string $tag): string|null return $tagId; } + + /** + * Compatibility layer to support both codefog/contao-haste v4 and v5. + */ + private function recursiveReplaceTokensAndTags(string $text, array $tokens, int $textFlags = 0): string + { + if ($this->stringParser) { + return $this->stringParser->recursiveReplaceTokensAndTags($text, $tokens, $textFlags); + } + + if (class_exists(HasteStringUtil::class)) { + return HasteStringUtil::recursiveReplaceTokensAndTags($text, $tokens, $textFlags); + } + + throw new \RuntimeException('Cannot replace tokens and tags due to missing dependencies.'); + } } diff --git a/src/Util.php b/src/Util.php deleted file mode 100644 index eec1775..0000000 --- a/src/Util.php +++ /dev/null @@ -1,31 +0,0 @@ -recursiveReplaceTokensAndTags($text, $tokens, $textFlags); - } - - return StringUtil::recursiveReplaceTokensAndTags($text, $tokens, $textFlags); - } -}