feat: add url string transformer

This commit is contained in:
Keith Solomon
2026-04-26 14:39:00 -05:00
parent 4880613b67
commit ddb0e12a9b
2 changed files with 116 additions and 0 deletions
+32
View File
@@ -0,0 +1,32 @@
<?php
namespace WPContentSync\Url;
final class UrlTransformer {
public function transformString( string $value, UrlMappingCollection $mappings ): string {
$transformed = $value;
foreach ( $mappings->all() as $mapping ) {
$transformed = $this->replaceMapping( $transformed, $mapping );
}
return $transformed;
}
private function replaceMapping( string $value, UrlMapping $mapping ): string {
$source = $mapping->sourceUrl();
$destination = $mapping->destinationUrl();
$replacements = array(
$source => $destination,
str_replace( '&', '&amp;', $source ) => str_replace( '&', '&amp;', $destination ),
$this->toProtocolRelative( $source ) => $this->toProtocolRelative( $destination ),
);
return strtr( $value, $replacements );
}
private function toProtocolRelative( string $url ): string {
return preg_replace( '#^https?:#', '', $url ) ?? $url;
}
}