MediaWiki REL1_39
Pattern.php
Go to the documentation of this file.
1<?php
2
4
11class Pattern {
13 private $debugName;
15 private $pattern;
17 private $prefix;
19 private $suffix;
20
25 public function __construct( string $debugName, string $pattern ) {
26 $this->debugName = $debugName;
27 $this->pattern = $pattern;
28 }
29
35 public function isMatch( string $name ) {
36 $this->init();
37 $match = true;
38 if ( $this->prefix !== '' ) {
39 $match = str_starts_with( $name, $this->prefix );
40 }
41 if ( $match && $this->suffix !== '' ) {
42 $match = str_ends_with( $name, $this->suffix )
43 && strlen( $name ) >= strlen( $this->prefix ) + strlen( $this->suffix );
44 }
45 return $match;
46 }
47
54 public function generate( $mappedSerial ) {
55 $this->init();
56 return $this->prefix . $mappedSerial . $this->suffix;
57 }
58
62 private function init() {
63 if ( $this->prefix === null ) {
64 $varPos = strpos( $this->pattern, '$1' );
65 if ( $varPos === false ) {
66 throw new \MWException( __CLASS__ .
67 "pattern {$this->debugName} must be of the form \"prefix \$1 suffix\"" );
68 }
69 $this->prefix = substr( $this->pattern, 0, $varPos );
70 $this->suffix = substr( $this->pattern, $varPos + strlen( '$1' ) );
71 }
72 }
73}
Helper for TempUserConfig representing string patterns with "$1" indicating variable substitution.
Definition Pattern.php:11
generate( $mappedSerial)
Substitute the serial number into the pattern.
Definition Pattern.php:54
__construct(string $debugName, string $pattern)
Definition Pattern.php:25
isMatch(string $name)
Does the pattern match the given name?
Definition Pattern.php:35