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 $debugName;
20 private $pattern;
22 private $prefix;
24 private $suffix;
25
30 public function __construct( string $debugName, string $pattern ) {
31 $this->debugName = $debugName;
32 $this->pattern = $pattern;
33 }
34
40 public function isMatch( string $name ) {
41 $this->init();
42 $match = true;
43 if ( $this->prefix !== '' ) {
44 $match = str_starts_with( $name, $this->prefix );
45 }
46 if ( $match && $this->suffix !== '' ) {
47 $match = str_ends_with( $name, $this->suffix )
48 && strlen( $name ) >= strlen( $this->prefix ) + strlen( $this->suffix );
49 }
50 return $match;
51 }
52
60 public function generate( $mappedSerial, ?string $year = null ) {
61 $this->init();
62 return $this->prefix .
63 ( $year ? $year . '-' : '' ) .
64 $mappedSerial .
65 $this->suffix;
66 }
67
75 public function buildLike( ISQLPlatform $db ) {
76 wfDeprecated( __METHOD__, '1.42' );
77 $this->init();
78 return $db->buildLike(
79 $this->prefix,
80 $db->anyString(),
81 $this->suffix
82 );
83 }
84
91 public function toLikeValue( ISQLPlatform $db ): LikeValue {
92 $this->init();
93 return new LikeValue(
94 $this->prefix,
95 $db->anyString(),
96 $this->suffix
97 );
98 }
99
107 public function extract( string $name ) {
108 if ( $this->isMatch( $name ) ) {
109 return substr( $name,
110 strlen( $this->prefix ),
111 strlen( $name ) - strlen( $this->prefix ) - strlen( $this->suffix ) );
112 }
113 return null;
114 }
115
119 private function init() {
120 if ( $this->prefix === null ) {
121 $varPos = strpos( $this->pattern, '$1' );
122 if ( $varPos === false ) {
123 throw new UnexpectedValueException( __CLASS__ .
124 "pattern {$this->debugName} must be of the form \"prefix \$1 suffix\"" );
125 }
126 $this->prefix = substr( $this->pattern, 0, $varPos );
127 $this->suffix = substr( $this->pattern, $varPos + strlen( '$1' ) );
128 }
129 }
130
131 public function __toString() {
132 return $this->pattern;
133 }
134}
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:91
generate( $mappedSerial, ?string $year=null)
Substitute the serial number into the pattern.
Definition Pattern.php:60
buildLike(ISQLPlatform $db)
Convert the pattern to an SQL like clause.
Definition Pattern.php:75
__construct(string $debugName, string $pattern)
Definition Pattern.php:30
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:107
isMatch(string $name)
Does the pattern match the given name?
Definition Pattern.php:40
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.