Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 11 |
|
0.00% |
0 / 5 |
CRAP | |
0.00% |
0 / 1 |
| CounterType | |
0.00% |
0 / 11 |
|
0.00% |
0 / 5 |
72 | |
0.00% |
0 / 1 |
| counterToBase64 | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
2 | |||
| counterToId | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
| idToCounter | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
6 | |||
| getRE | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
6 | |||
| matches | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | declare( strict_types = 1 ); |
| 3 | |
| 4 | namespace Wikimedia\Parsoid\Utils; |
| 5 | |
| 6 | enum CounterType: string { |
| 7 | case NODE_DATA_ID = 'mw'; |
| 8 | case ANNOTATION_ABOUT = 'mwa'; |
| 9 | case TRANSCLUSION_ABOUT = '#mwt'; |
| 10 | |
| 11 | /** |
| 12 | * Convert a counter to a Base64 encoded string. |
| 13 | * Padding is stripped. /,+ are replaced with _,- respectively. |
| 14 | * Warning: Max integer is 2^31 - 1 for bitwise operations. |
| 15 | * @param int $n |
| 16 | * @return string |
| 17 | */ |
| 18 | private static function counterToBase64( int $n ): string { |
| 19 | $str = ''; |
| 20 | do { |
| 21 | $str = chr( $n & 0xff ) . $str; |
| 22 | $n >>= 8; |
| 23 | } while ( $n > 0 ); |
| 24 | return rtrim( strtr( base64_encode( $str ), '+/', '-_' ), '=' ); |
| 25 | } |
| 26 | |
| 27 | public function counterToId( int $counter ): string { |
| 28 | if ( $this === self::NODE_DATA_ID ) { |
| 29 | return $this->value . self::counterToBase64( $counter ); |
| 30 | } else { |
| 31 | return $this->value . $counter; |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | public function idToCounter( string $id ): ?string { |
| 36 | return $this->matches( $id ) ? substr( $id, strlen( $this->value ) ) : null; |
| 37 | } |
| 38 | |
| 39 | public function getRE(): string { |
| 40 | return $this === self::NODE_DATA_ID ? "mw[\w-]{2,}" : "{$this->value}\d+"; |
| 41 | } |
| 42 | |
| 43 | public function matches( string $id ): bool { |
| 44 | return (bool)preg_match( "/^" . $this->getRE() . "$/D", $id ); |
| 45 | } |
| 46 | } |