38 lines
796 B
PHP
38 lines
796 B
PHP
<?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 );
|
|
|
|
usort(
|
|
$this->mappings,
|
|
static function ( UrlMapping $left, UrlMapping $right ): int {
|
|
return strlen( $right->sourceUrl() ) <=> strlen( $left->sourceUrl() );
|
|
}
|
|
);
|
|
}
|
|
|
|
/**
|
|
* @return array<int, UrlMapping>
|
|
*/
|
|
public function all(): array {
|
|
return $this->mappings;
|
|
}
|
|
}
|