Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
55.00% |
22 / 40 |
|
62.50% |
5 / 8 |
CRAP | |
0.00% |
0 / 1 |
| Pattern | |
55.00% |
22 / 40 |
|
62.50% |
5 / 8 |
39.33 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| isMatch | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
5 | |||
| generate | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
2 | |||
| buildLike | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
2 | |||
| toLikeValue | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
2 | |||
| extract | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
6 | |||
| init | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
3 | |||
| __toString | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace MediaWiki\User\TempUser; |
| 4 | |
| 5 | use Stringable; |
| 6 | use UnexpectedValueException; |
| 7 | use Wikimedia\Rdbms\LikeValue; |
| 8 | use Wikimedia\Rdbms\Platform\ISQLPlatform; |
| 9 | |
| 10 | /** |
| 11 | * Helper for TempUserConfig representing string patterns with "$1" indicating |
| 12 | * variable substitution. |
| 13 | * |
| 14 | * @internal |
| 15 | */ |
| 16 | class Pattern implements Stringable { |
| 17 | /** @var string */ |
| 18 | private $prefix; |
| 19 | /** @var string */ |
| 20 | private $suffix; |
| 21 | |
| 22 | /** |
| 23 | * @param string $debugName The name of the pattern, for use in error messages |
| 24 | * @param string $pattern The pattern itself |
| 25 | */ |
| 26 | public function __construct( |
| 27 | private readonly string $debugName, |
| 28 | private readonly string $pattern, |
| 29 | ) { |
| 30 | } |
| 31 | |
| 32 | /** |
| 33 | * Does the pattern match the given name? |
| 34 | * @param string $name |
| 35 | * @return bool |
| 36 | */ |
| 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 | |
| 50 | /** |
| 51 | * Substitute the serial number into the pattern. |
| 52 | * |
| 53 | * @param string $mappedSerial |
| 54 | * @param ?string $year |
| 55 | * @return string |
| 56 | */ |
| 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 | |
| 65 | /** |
| 66 | * Convert the pattern to an SQL like clause |
| 67 | * |
| 68 | * @deprecated since 1.42. Use toLikeValue() instead |
| 69 | * @param ISQLPlatform $db |
| 70 | * @return string |
| 71 | */ |
| 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 | |
| 82 | /** |
| 83 | * Convert the pattern to an SQL builder "LIKE" value that matches it |
| 84 | * |
| 85 | * @param ISQLPlatform $db |
| 86 | * @return LikeValue |
| 87 | */ |
| 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 | |
| 97 | /** |
| 98 | * Extract the variable part of the string (matching $1 or YYYY-$1), |
| 99 | * or null if there is no match |
| 100 | * |
| 101 | * @param string $name |
| 102 | * @return ?string |
| 103 | */ |
| 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 | |
| 113 | /** |
| 114 | * Initialise the prefix and suffix |
| 115 | */ |
| 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 | } |