| 1 |
<?php |
| 2 |
namespace MediaWiki\Tests\Json; |
| 3 |
|
| 4 |
use Wikimedia\Assert\Assert; |
| 5 |
use Wikimedia\JsonCodec\JsonCodecable; |
| 6 |
use Wikimedia\JsonCodec\JsonCodecableTrait; |
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
class SampleObject implements JsonCodecable { |
| 13 |
use JsonCodecableTrait; |
| 14 |
|
| 15 |
|
| 16 |
public string $property; |
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
public function __construct( string $property ) { |
| 23 |
$this->property = $property; |
| 24 |
} |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
public function toJsonArray(): array { |
| 30 |
if ( $this->property !== 'use _type_' ) { |
| 31 |
|
| 32 |
return [ 'property' => $this->property ]; |
| 33 |
} |
| 34 |
return [ |
| 35 |
'property' => $this->property, |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
'_type_' => 'check123', |
| 40 |
]; |
| 41 |
} |
| 42 |
|
| 43 |
|
| 44 |
public static function newFromJsonArray( array $json ): SampleObject { |
| 45 |
if ( $json['property'] === 'use _type_' ) { |
| 46 |
Assert::invariant( $json['_type_'] === 'check123', 'protected field' ); |
| 47 |
} |
| 48 |
return new SampleObject( $json['property'] ); |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
class_alias( SampleObject::class, 'MediaWiki\\Tests\\Json\\SampleObjectAlias' ); |