Merge "Use $string === '' instead of strlen( $string ) === 0"
[mediawiki.git] / includes / parser / Parsoid / Config / PageContent.php
blobfca23ae2fc77a635662a40fecdc6ddf5bb902f86
1 <?php
2 /**
3 * Copyright (C) 2011-2022 Wikimedia Foundation and others.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 namespace MediaWiki\Parser\Parsoid\Config;
22 use InvalidArgumentException;
23 use MediaWiki\Revision\RevisionRecord;
24 use Wikimedia\Parsoid\Config\PageContent as IPageContent;
26 /**
27 * PageContent implementation for MediaWiki
29 * @since 1.39
31 class PageContent extends IPageContent {
32 private RevisionRecord $rev;
34 public function __construct( RevisionRecord $rev ) {
35 $this->rev = $rev;
38 /** @inheritDoc */
39 public function getRoles(): array {
40 return $this->rev->getSlotRoles();
43 /** @inheritDoc */
44 public function hasRole( string $role ): bool {
45 return $this->rev->hasSlot( $role );
48 /**
49 * Throw if the revision doesn't have the named role
50 * @param string $role
51 * @throws InvalidArgumentException
53 private function checkRole( string $role ): void {
54 if ( !$this->rev->hasSlot( $role ) ) {
55 throw new InvalidArgumentException( "PageContent does not have role '$role'" );
59 /** @inheritDoc */
60 public function getModel( string $role ): string {
61 $this->checkRole( $role );
62 return $this->rev->getContent( $role )->getModel();
65 /** @inheritDoc */
66 public function getFormat( string $role ): string {
67 $this->checkRole( $role );
68 return $this->rev->getContent( $role )->getDefaultFormat();
71 /** @inheritDoc */
72 public function getContent( string $role ): string {
73 $this->checkRole( $role );
74 return $this->rev->getContentOrThrow( $role )->serialize();