-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReservedKeyCodec.php
More file actions
88 lines (80 loc) · 2.47 KB
/
Copy pathReservedKeyCodec.php
File metadata and controls
88 lines (80 loc) · 2.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
<?php
declare( strict_types=1 );
namespace Wikimedia\JsonCodec\Tests;
use stdClass;
use Wikimedia\JsonCodec\JsonCodec;
/**
* An example of extending JsonCodec to remap/abbreviate type names,
* change the key used to encode types, and reserve certain key names.
*/
class ReservedKeyCodec extends JsonCodec {
protected array $classMap = [];
protected array $reverseClassMap = [];
public function __construct() {
parent::__construct();
$this->addSchemaClass( 'array' );
$this->addSchemaClass( stdClass::class );
$this->addSchemaClass( SampleObject::class );
$this->addSchemaClass( SampleContainerObject::class );
}
/**
* Add a new class to the schema; this class will be encoded with a short
* numeric value.
* @param string $className
*/
public function addSchemaClass( string $className ): void {
// $idx zero is reserved for 'matches class hint'
$idx = count( $this->classMap ) + 1;
$this->classMap[$className] = $idx;
$this->reverseClassMap[$idx] = $className;
}
/** @inheritDoc */
protected function isArrayMarked( array $value ): bool {
// Use our own mark!
return array_key_exists( '@', $value );
}
protected function markArray( array &$arr, string $className, ?string $classHint ): void {
// We're going to record marks using '@' and rename any existing
// key starting with '@' to include one additional '@'
$remapped = [];
foreach ( $arr as $key => $value ) {
if ( str_starts_with( $key, '@' ) ) {
$remapped['@' . $key] = $value;
unset( $arr[$key] );
}
}
foreach ( $remapped as $key => $value ) {
$arr[$key] = $value;
}
// Use our own class name mapping for compactness!
if ( array_key_exists( $className, $this->classMap ) ) {
$arr['@'] = $this->classMap[$className];
} elseif ( $className === $classHint ) {
$arr['@'] = 0;
} else {
$arr['@'] = $className;
}
}
protected function unmarkArray( array &$arr, ?string $classHint ): string {
$className = $classHint;
if ( array_key_exists( '@', $arr ) ) {
$className = $arr['@'];
if ( array_key_exists( $className, $this->reverseClassMap ) ) {
$className = $this->reverseClassMap[$className];
}
unset( $arr['@'] );
}
// Undo the mapping which added extra '@' to key names
$remapped = [];
foreach ( $arr as $key => $value ) {
if ( str_starts_with( $key, '@' ) ) {
$remapped[substr( $key, 1 )] = $value;
unset( $arr[$key] );
}
}
foreach ( $remapped as $key => $value ) {
$arr[$key] = $value;
}
return $className;
}
}