Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
10 / 10 |
|
100.00% |
1 / 1 |
CRAP | |
100.00% |
1 / 1 |
| ZObjectFilterIsEnumValue | |
100.00% |
10 / 10 |
|
100.00% |
1 / 1 |
4 | |
100.00% |
1 / 1 |
| pass | |
100.00% |
10 / 10 |
|
100.00% |
1 / 1 |
4 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * WikiLambda ZObject Authorization Filter: Object is instance of an enum type |
| 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\Authorization; |
| 12 | |
| 13 | use MediaWiki\Extension\WikiLambda\Registry\ZTypeRegistry; |
| 14 | use MediaWiki\Extension\WikiLambda\WikiLambdaServices; |
| 15 | use MediaWiki\Extension\WikiLambda\ZObjectContent\ZObjectContent; |
| 16 | use MediaWiki\Extension\WikiLambda\ZObjects\ZType; |
| 17 | use MediaWiki\Title\Title; |
| 18 | |
| 19 | class ZObjectFilterIsEnumValue implements ZObjectFilter { |
| 20 | |
| 21 | /** |
| 22 | * ZObject filter that checks whether the edited object is an instance |
| 23 | * of an Enum type |
| 24 | * |
| 25 | * @param ZObjectContent $fromContent |
| 26 | * @param ZObjectContent $toContent |
| 27 | * @param Title $title |
| 28 | * @param array $args |
| 29 | * @return bool |
| 30 | */ |
| 31 | public static function pass( $fromContent, $toContent, $title, $args = [] ): bool { |
| 32 | $type = $fromContent->getZType(); |
| 33 | |
| 34 | if ( in_array( $type, ZTypeRegistry::EXCLUDE_TYPES_FROM_ENUMS ) ) { |
| 35 | // Type has identity key but excluded from enum; return false |
| 36 | return false; |
| 37 | } |
| 38 | |
| 39 | $typeTitle = Title::newFromText( $type, NS_MAIN ); |
| 40 | $zObjectStore = WikiLambdaServices::getZObjectStore(); |
| 41 | $typeObject = $zObjectStore->fetchZObjectByTitle( $typeTitle ); |
| 42 | |
| 43 | if ( $typeObject === false ) { |
| 44 | // Type not found; return false |
| 45 | return false; |
| 46 | } |
| 47 | |
| 48 | $typeInnerObject = $typeObject->getInnerZObject(); |
| 49 | return ( $typeInnerObject instanceof ZType ) && $typeInnerObject->isEnumType(); |
| 50 | } |
| 51 | } |