Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
94.59% |
35 / 37 |
|
80.00% |
4 / 5 |
CRAP | |
0.00% |
0 / 1 |
| ZString | |
94.59% |
35 / 37 |
|
80.00% |
4 / 5 |
16.04 | |
0.00% |
0 / 1 |
| __construct | |
88.24% |
15 / 17 |
|
0.00% |
0 / 1 |
9.13 | |||
| getDefinition | |
100.00% |
12 / 12 |
|
100.00% |
1 / 1 |
1 | |||
| isValid | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
| getSerialized | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
3 | |||
| getZValue | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * WikiLambda ZString |
| 4 | * |
| 5 | * @file |
| 6 | * @ingroup Extensions |
| 7 | * @copyright 2020– Abstract Wikipedia team; see AUTHORS.txt |
| 8 | * @license MIT |
| 9 | */ |
| 10 | |
| 11 | namespace MediaWiki\Extension\WikiLambda\ZObjects; |
| 12 | |
| 13 | use MediaWiki\Extension\WikiLambda\Registry\ZTypeRegistry; |
| 14 | use MediaWiki\Extension\WikiLambda\ZObjectUtils; |
| 15 | |
| 16 | class ZString extends ZObject { |
| 17 | |
| 18 | /** |
| 19 | * @inheritDoc |
| 20 | */ |
| 21 | public function __construct( $value = '' ) { |
| 22 | if ( is_string( $value ) || $value === null ) { |
| 23 | $this->data[ ZTypeRegistry::Z_STRING_VALUE ] = $value; |
| 24 | return; |
| 25 | } |
| 26 | |
| 27 | if ( is_array( $value ) ) { |
| 28 | $this->data[ ZTypeRegistry::Z_STRING_VALUE ] = $value[0] ?? ''; |
| 29 | return; |
| 30 | } |
| 31 | |
| 32 | if ( $value instanceof ZString ) { |
| 33 | $this->data[ZTypeRegistry::Z_STRING_VALUE] = $value->getZValue(); |
| 34 | return; |
| 35 | } |
| 36 | |
| 37 | // Possibly a serialised version of a ZString; pull out the value and use it if so, otherwise we'll default. |
| 38 | if ( $value instanceof \stdclass ) { |
| 39 | $arrayObject = get_object_vars( $value ); |
| 40 | if ( |
| 41 | array_key_exists( 'data', $arrayObject ) |
| 42 | && array_key_exists( ZTypeRegistry::Z_STRING_VALUE, $arrayObject['data'] ) |
| 43 | && is_string( $arrayObject['data'][ZTypeRegistry::Z_STRING_VALUE] ) |
| 44 | ) { |
| 45 | $this->data[ZTypeRegistry::Z_STRING_VALUE] = $arrayObject['data'][ZTypeRegistry::Z_STRING_VALUE]; |
| 46 | return; |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | // Otherwise give up and have it as null |
| 51 | $this->data[ ZTypeRegistry::Z_STRING_VALUE ] = null; |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * @inheritDoc |
| 56 | */ |
| 57 | public static function getDefinition(): array { |
| 58 | return [ |
| 59 | 'type' => [ |
| 60 | 'type' => ZTypeRegistry::Z_REFERENCE, |
| 61 | 'value' => ZTypeRegistry::Z_STRING, |
| 62 | ], |
| 63 | 'keys' => [ |
| 64 | ZTypeRegistry::Z_STRING_VALUE => [ |
| 65 | 'type' => ZTypeRegistry::BUILTIN_STRING, |
| 66 | 'default' => '' |
| 67 | ], |
| 68 | ], |
| 69 | ]; |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * @inheritDoc |
| 74 | */ |
| 75 | public function isValid(): bool { |
| 76 | // For ZString, all strings of any value are by definition valid (including null, |
| 77 | // which is read as empty). |
| 78 | if ( !is_string( $this->data[ ZTypeRegistry::Z_STRING_VALUE ] ) ) { |
| 79 | return false; |
| 80 | } |
| 81 | return true; |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * @inheritDoc |
| 86 | */ |
| 87 | public function getSerialized( $form = self::FORM_CANONICAL ) { |
| 88 | if ( $form === self::FORM_CANONICAL ) { |
| 89 | if ( !ZObjectUtils::isValidZObjectReference( $this->getZValue() ?? '' ) ) { |
| 90 | return $this->getZValue(); |
| 91 | } |
| 92 | } |
| 93 | return parent::getSerialized(); |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Get the string value wrapped in this ZString instance |
| 98 | * |
| 99 | * @return string|null |
| 100 | */ |
| 101 | public function getZValue() { |
| 102 | return $this->data[ ZTypeRegistry::Z_STRING_VALUE ]; |
| 103 | } |
| 104 | } |