feat: add url mapping value objects
This commit is contained in:
@@ -0,0 +1,32 @@
|
||||
<?php
|
||||
|
||||
namespace WPContentSync\Url;
|
||||
|
||||
final class UrlMapping {
|
||||
private string $source_url;
|
||||
private string $destination_url;
|
||||
|
||||
public function __construct( string $source_url, string $destination_url ) {
|
||||
$source_url = $this->normalizeUrl( $source_url );
|
||||
$destination_url = $this->normalizeUrl( $destination_url );
|
||||
|
||||
if ( '' === $source_url || '' === $destination_url ) {
|
||||
throw new \InvalidArgumentException( 'Source and destination URLs are required.' );
|
||||
}
|
||||
|
||||
$this->source_url = $source_url;
|
||||
$this->destination_url = $destination_url;
|
||||
}
|
||||
|
||||
public function sourceUrl(): string {
|
||||
return $this->source_url;
|
||||
}
|
||||
|
||||
public function destinationUrl(): string {
|
||||
return $this->destination_url;
|
||||
}
|
||||
|
||||
private function normalizeUrl( string $url ): string {
|
||||
return rtrim( trim( $url ), '/' );
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
namespace WPContentSync\Url;
|
||||
|
||||
final class UrlMappingCollection {
|
||||
/**
|
||||
* @var array<int, UrlMapping>
|
||||
*/
|
||||
private array $mappings;
|
||||
|
||||
/**
|
||||
* @param array<int, UrlMapping> $mappings URL mappings.
|
||||
*/
|
||||
public function __construct( array $mappings ) {
|
||||
foreach ( $mappings as $mapping ) {
|
||||
if ( ! $mapping instanceof UrlMapping ) {
|
||||
throw new \InvalidArgumentException( 'URL mapping collections only accept UrlMapping instances.' );
|
||||
}
|
||||
}
|
||||
|
||||
$this->mappings = array_values( $mappings );
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<int, UrlMapping>
|
||||
*/
|
||||
public function all(): array {
|
||||
return $this->mappings;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user