MediaWiki master
Pattern.php
Go to the documentation of this file.
1<?php
2
4
5use Stringable;
6use UnexpectedValueException;
9
16class Pattern implements Stringable {
18 private $prefix;
20 private $suffix;
21
26 public function __construct(
27 private readonly string $debugName,
28 private readonly string $pattern,
29 ) {
30 }
31
37 public function isMatch( string $name ) {
38 $this->init();
39 $match = true;
40 if ( $this->prefix !== '' ) {
41 $match = str_starts_with( $name, $this->prefix );
42 }
43 if ( $match && $this->suffix !== '' ) {
44 $match = str_ends_with( $name, $this->suffix )
45 && strlen( $name ) >= strlen( $this->prefix ) + strlen( $this->suffix );
46 }
47 return $match;
48 }
49
57 public function generate( $mappedSerial, ?string $year = null ) {
58 $this->init();
59 return $this->prefix .
60 ( $year ? $year . '-' : '' ) .
61 $mappedSerial .
62 $this->suffix;
63 }
64
72 public function buildLike( ISQLPlatform $db ) {
73 wfDeprecated( __METHOD__, '1.42' );
74 $this->init();
75 return $db->buildLike(
76 $this->prefix,
77 $db->anyString(),
78 $this->suffix
79 );
80 }
81
88 public function toLikeValue( ISQLPlatform $db ): LikeValue {
89 $this->init();
90 return new LikeValue(
91 $this->prefix,
92 $db->anyString(),
93 $this->suffix
94 );
95 }
96
104 public function extract( string $name ) {
105 if ( $this->isMatch( $name ) ) {
106 return substr( $name,
107 strlen( $this->prefix ),
108 strlen( $name ) - strlen( $this->prefix ) - strlen( $this->suffix ) );
109 }
110 return null;
111 }
112
116 private function init() {
117 if ( $this->prefix === null ) {
118 $varPos = strpos( $this->pattern, '$1' );
119 if ( $varPos === false ) {
120 throw new UnexpectedValueException( __CLASS__ .
121 "pattern {$this->debugName} must be of the form \"prefix \$1 suffix\"" );
122 }
123 $this->prefix = substr( $this->pattern, 0, $varPos );
124 $this->suffix = substr( $this->pattern, $varPos + strlen( '$1' ) );
125 }
126 }
127
128 public function __toString() {
129 return $this->pattern;
130 }
131}
wfDeprecated( $function, $version=false, $component=false, $callerOffset=2)
Logs a warning that a deprecated feature was used.
Helper for TempUserConfig representing string patterns with "$1" indicating variable substitution.
Definition Pattern.php:16
toLikeValue(ISQLPlatform $db)
Convert the pattern to an SQL builder "LIKE" value that matches it.
Definition Pattern.php:88
generate( $mappedSerial, ?string $year=null)
Substitute the serial number into the pattern.
Definition Pattern.php:57
buildLike(ISQLPlatform $db)
Convert the pattern to an SQL like clause.
Definition Pattern.php:72
__construct(private readonly string $debugName, private readonly string $pattern,)
Definition Pattern.php:26
extract(string $name)
Extract the variable part of the string (matching $1 or YYYY-$1), or null if there is no match.
Definition Pattern.php:104
isMatch(string $name)
Does the pattern match the given name?
Definition Pattern.php:37
Content of like value.
Definition LikeValue.php:14
Interface for query language.
buildLike( $param,... $params)
LIKE statement wrapper.
anyString()
Returns a token for buildLike() that denotes a '' to be used in a LIKE query.