22 use InvalidArgumentException;
50 private $equivalentString;
58 if ( $database !==
null && ( !is_string( $database ) || $database ===
'' ) ) {
59 throw new InvalidArgumentException(
'Database must be null or a non-empty string.' );
61 $this->database = $database;
62 if ( $schema !==
null && ( !is_string( $schema ) || $schema ===
'' ) ) {
63 throw new InvalidArgumentException(
'Schema must be null or a non-empty string.' );
64 } elseif ( $database ===
null && $schema !==
null ) {
65 throw new InvalidArgumentException(
'Schema must be null if database is null.' );
67 $this->schema = $schema;
68 if ( !is_string( $prefix ) ) {
69 throw new InvalidArgumentException(
"Prefix must be a string." );
71 $this->prefix = $prefix;
79 if ( $domain instanceof self ) {
83 if ( !is_string( $domain ) ) {
84 throw new InvalidArgumentException(
"Domain must be a string or " . __CLASS__ );
87 $parts = array_map( [ __CLASS__,
'decode' ], explode(
'-', $domain ) );
92 if ( count( $parts ) == 1 ) {
93 $database = $parts[0];
94 } elseif ( count( $parts ) == 2 ) {
95 [ $database, $prefix ] = $parts;
96 } elseif ( count( $parts ) == 3 ) {
97 [ $database, $schema, $prefix ] = $parts;
99 throw new InvalidArgumentException(
"Domain '$domain' has too few or too many parts." );
102 if ( $database ===
'' ) {
106 if ( $schema ===
'' ) {
110 $instance =
new self( $database, $schema, $prefix );
111 $instance->equivalentString = $domain;
120 return new self(
null,
null,
'' );
128 if ( $other instanceof
self ) {
130 $this->database === $other->database &&
131 $this->schema === $other->schema &&
132 $this->prefix === $other->prefix
136 return ( $this->getId() === $other );
154 if ( $this->isUnspecified() ) {
158 $other = self::newFromId( $other );
161 ( $this->database === $other->database || $this->database ===
null ) &&
162 ( $this->schema === $other->schema || $this->schema ===
null ) &&
163 $this->prefix === $other->prefix
173 $this->database ===
null && $this->schema ===
null && $this->prefix ===
''
181 return $this->database;
188 return $this->schema;
195 return $this->prefix;
202 $this->equivalentString ??= $this->convertToString();
204 return $this->equivalentString;
210 private function convertToString(): string {
211 $parts = [ (string)$this->database ];
212 if ( $this->schema !==
null ) {
213 $parts[] = $this->schema;
215 if ( $this->prefix !=
'' || $this->schema !==
null ) {
221 $parts[] = $this->prefix;
224 return implode(
'-', array_map( [ __CLASS__,
'encode' ], $parts ) );
227 private static function encode( $decoded ) {
230 $length = strlen( $decoded );
231 for ( $i = 0; $i < $length; ++$i ) {
232 $char = $decoded[$i];
233 if ( $char ===
'-' ) {
235 } elseif ( $char ===
'?' ) {
245 private static function decode( $encoded ) {
248 $length = strlen( $encoded );
249 for ( $i = 0; $i < $length; ++$i ) {
250 $char = $encoded[$i];
251 if ( $char ===
'?' ) {
252 $nextChar = $encoded[$i + 1] ??
null;
253 if ( $nextChar ===
'h' ) {
256 } elseif ( $nextChar ===
'?' ) {
274 return $this->getId();
if(!defined('MW_SETUP_CALLBACK'))
Class to handle database/schema/prefix specifications for IDatabase.
isCompatible( $other)
Check whether the domain $other meets the specifications of this domain.
static newFromId( $domain)
__construct( $database, $schema, $prefix)