namespace MediaWiki\Block;
+use MediaWiki\Block\Restriction\NamespaceRestriction;
use MediaWiki\Block\Restriction\PageRestriction;
use MediaWiki\Block\Restriction\Restriction;
use Wikimedia\Rdbms\IResultWrapper;
class BlockRestriction {
+ /**
+ * Map of all of the restriction types.
+ */
+ private static $types = [
+ PageRestriction::TYPE_ID => PageRestriction::class,
+ NamespaceRestriction::TYPE_ID => NamespaceRestriction::class,
+ ];
+
/**
* Retrieves the restrictions from the database by block id.
*
+ * @since 1.33
* @param int|array $blockId
* @param IDatabase|null $db
* @return Restriction[]
*/
public static function loadByBlockId( $blockId, IDatabase $db = null ) {
- if ( is_null( $blockId ) || $blockId === [] ) {
+ if ( $blockId === null || $blockId === [] ) {
return [];
}
/**
* Inserts the restrictions into the database.
*
+ * @since 1.33
* @param Restriction[] $restrictions
* @return bool
*/
* Updates the list of restrictions. This method does not allow removing all
* of the restrictions. To do that, use ::deleteByBlockId().
*
+ * @since 1.33
* @param Restriction[] $restrictions
* @return bool
*/
/**
* Updates the list of restrictions by parent id.
*
+ * @since 1.33
* @param int $parentBlockId
* @param Restriction[] $restrictions
* @return bool
/**
* Delete the restrictions.
*
+ * @since 1.33
* @param Restriction[]|null $restrictions
* @throws MWException
* @return bool
/**
* Delete the restrictions by Block ID.
*
+ * @since 1.33
* @param int|array $blockId
* @throws MWException
* @return bool
/**
* Delete the restrictions by Parent Block ID.
*
+ * @since 1.33
* @param int|array $parentBlockId
* @throws MWException
* @return bool
* equality check as the restrictions do not have to contain the same block
* ids.
*
+ * @since 1.33
* @param Restriction[] $a
* @param Restriction[] $b
* @return bool
/**
* Set the blockId on a set of restrictions and return a new set.
*
+ * @since 1.33
* @param int $blockId
* @param Restriction[] $restrictions
* @return Restriction[]
* @return Restriction|null
*/
private static function rowToRestriction( \stdClass $row ) {
- switch ( $row->ir_type ) {
- case PageRestriction::TYPE_ID:
- return PageRestriction::newFromRow( $row );
- default:
- return null;
+ if ( array_key_exists( (int)$row->ir_type, self::$types ) ) {
+ $class = self::$types[ (int)$row->ir_type ];
+ return call_user_func( [ $class, 'newFromRow' ], $row );
}
+
+ return null;
}
}