2 namespace MediaWiki\Tests\Json
;
4 use Wikimedia\JsonCodec\JsonClassCodec
;
7 * Managed object factory which also handles serialization/deserialization
8 * of the objects it manages.
10 * @implements JsonClassCodec<ManagedObject>
12 class ManagedObjectFactory
implements JsonClassCodec
{
13 /** @var array<string,ManagedObject> Fake database */
14 private $storage = [];
17 * Create and store an object with $name and $value in the database.
20 * @return ManagedObject
22 public function create( string $name, int $value ) {
23 if ( isset( $this->storage
[$name] ) ) {
24 throw new \
Error( "duplicate name" );
26 $this->storage
[$name] = $o = new ManagedObject( $name, $value );
31 * Lookup $name in the database.
33 * @return ManagedObject
35 public function lookup( string $name ): ManagedObject
{
36 if ( !isset( $this->storage
[$name] ) ) {
37 throw new \
Error( "not found" );
39 return $this->storage
[$name];
43 public function toJsonArray( $obj ): array {
44 '@phan-var ManagedObject $obj';
45 // Not necessary to serialize all the properties, since they
46 // will be reloaded from the "database" during deserialization
47 return [ 'name' => $obj->name
];
51 public function newFromJsonArray( string $className, array $json ): ManagedObject
{
52 // @phan-suppress-next-line PhanTypeMismatchReturn template limitations
53 return $this->lookup( $json['name'] );
57 public function jsonClassHintFor( string $className, string $key ): ?
string {