MediaWiki  master
Pattern.php
Go to the documentation of this file.
1 <?php
2 
4 
6 
13 class Pattern {
15  private $debugName;
17  private $pattern;
19  private $prefix;
21  private $suffix;
22 
27  public function __construct( string $debugName, string $pattern ) {
28  $this->debugName = $debugName;
29  $this->pattern = $pattern;
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 
56  public function generate( $mappedSerial ) {
57  $this->init();
58  return $this->prefix . $mappedSerial . $this->suffix;
59  }
60 
67  public function buildLike( ISQLPlatform $db ) {
68  $this->init();
69  return $db->buildLike(
70  $this->prefix,
71  $db->anyString(),
72  $this->suffix
73  );
74  }
75 
82  public function extract( string $name ) {
83  if ( $this->isMatch( $name ) ) {
84  return substr( $name,
85  strlen( $this->prefix ),
86  strlen( $name ) - strlen( $this->prefix ) - strlen( $this->suffix ) );
87  }
88  return null;
89  }
90 
94  private function init() {
95  if ( $this->prefix === null ) {
96  $varPos = strpos( $this->pattern, '$1' );
97  if ( $varPos === false ) {
98  throw new \MWException( __CLASS__ .
99  "pattern {$this->debugName} must be of the form \"prefix \$1 suffix\"" );
100  }
101  $this->prefix = substr( $this->pattern, 0, $varPos );
102  $this->suffix = substr( $this->pattern, $varPos + strlen( '$1' ) );
103  }
104  }
105 }
Helper for TempUserConfig representing string patterns with "$1" indicating variable substitution.
Definition: Pattern.php:13
generate( $mappedSerial)
Substitute the serial number into the pattern.
Definition: Pattern.php:56
buildLike(ISQLPlatform $db)
Convert the pattern to an SQL like clause.
Definition: Pattern.php:67
__construct(string $debugName, string $pattern)
Definition: Pattern.php:27
extract(string $name)
Extract the part of the string matching $1, or null if there is no match.
Definition: Pattern.php:82
isMatch(string $name)
Does the pattern match the given name?
Definition: Pattern.php:37
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.