MediaWiki master
Pattern.php
Go to the documentation of this file.
1<?php
2
4
5use UnexpectedValueException;
8
15class Pattern {
17 private $debugName;
19 private $pattern;
21 private $prefix;
23 private $suffix;
24
29 public function __construct( string $debugName, string $pattern ) {
30 $this->debugName = $debugName;
31 $this->pattern = $pattern;
32 }
33
39 public function isMatch( string $name ) {
40 $this->init();
41 $match = true;
42 if ( $this->prefix !== '' ) {
43 $match = str_starts_with( $name, $this->prefix );
44 }
45 if ( $match && $this->suffix !== '' ) {
46 $match = str_ends_with( $name, $this->suffix )
47 && strlen( $name ) >= strlen( $this->prefix ) + strlen( $this->suffix );
48 }
49 return $match;
50 }
51
59 public function generate( $mappedSerial, ?string $year = null ) {
60 $this->init();
61 return $this->prefix .
62 ( $year ? $year . '-' : '' ) .
63 $mappedSerial .
64 $this->suffix;
65 }
66
74 public function buildLike( ISQLPlatform $db ) {
75 wfDeprecated( __METHOD__, '1.42' );
76 $this->init();
77 return $db->buildLike(
78 $this->prefix,
79 $db->anyString(),
80 $this->suffix
81 );
82 }
83
90 public function toLikeValue( ISQLPlatform $db ): LikeValue {
91 $this->init();
92 return new LikeValue(
93 $this->prefix,
94 $db->anyString(),
95 $this->suffix
96 );
97 }
98
106 public function extract( string $name ) {
107 if ( $this->isMatch( $name ) ) {
108 return substr( $name,
109 strlen( $this->prefix ),
110 strlen( $name ) - strlen( $this->prefix ) - strlen( $this->suffix ) );
111 }
112 return null;
113 }
114
118 private function init() {
119 if ( $this->prefix === null ) {
120 $varPos = strpos( $this->pattern, '$1' );
121 if ( $varPos === false ) {
122 throw new UnexpectedValueException( __CLASS__ .
123 "pattern {$this->debugName} must be of the form \"prefix \$1 suffix\"" );
124 }
125 $this->prefix = substr( $this->pattern, 0, $varPos );
126 $this->suffix = substr( $this->pattern, $varPos + strlen( '$1' ) );
127 }
128 }
129}
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:15
toLikeValue(ISQLPlatform $db)
Convert the pattern to an SQL builder "LIKE" value that matches it.
Definition Pattern.php:90
generate( $mappedSerial, ?string $year=null)
Substitute the serial number into the pattern.
Definition Pattern.php:59
buildLike(ISQLPlatform $db)
Convert the pattern to an SQL like clause.
Definition Pattern.php:74
__construct(string $debugName, string $pattern)
Definition Pattern.php:29
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:106
isMatch(string $name)
Does the pattern match the given name?
Definition Pattern.php:39
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.