| 1 |
<?php |
| 2 |
namespace MediaWiki\Tests\Json; |
| 3 |
|
| 4 |
use Wikimedia\JsonCodec\JsonClassCodec; |
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
class ManagedObjectFactory implements JsonClassCodec { |
| 13 |
|
| 14 |
private $storage = []; |
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
public function create( string $name, int $value ) { |
| 23 |
if ( isset( $this->storage[$name] ) ) { |
| 24 |
throw new \Error( "duplicate name" ); |
| 25 |
} |
| 26 |
$this->storage[$name] = $o = new ManagedObject( $name, $value ); |
| 27 |
return $o; |
| 28 |
} |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
public function lookup( string $name ): ManagedObject { |
| 36 |
if ( !isset( $this->storage[$name] ) ) { |
| 37 |
throw new \Error( "not found" ); |
| 38 |
} |
| 39 |
return $this->storage[$name]; |
| 40 |
} |
| 41 |
|
| 42 |
|
| 43 |
public function toJsonArray( $obj ): array { |
| 44 |
'@phan-var ManagedObject $obj'; |
| 45 |
|
| 46 |
|
| 47 |
return [ 'name' => $obj->name ]; |
| 48 |
} |
| 49 |
|
| 50 |
|
| 51 |
public function newFromJsonArray( string $className, array $json ): ManagedObject { |
| 52 |
|
| 53 |
return $this->lookup( $json['name'] ); |
| 54 |
} |
| 55 |
|
| 56 |
|
| 57 |
public function jsonClassHintFor( string $className, string $key ): ?string { |
| 58 |
|
| 59 |
return null; |
| 60 |
} |
| 61 |
} |