1<?php 2 3namespace Wikimedia\Parsoid\Mocks; 4 5use Wikimedia\Parsoid\Config\PageContent; 6 7class MockPageContent implements PageContent { 8 9 /** 10 * Alas, this is public because parserTests is reaching in and altering 11 * the main content when various modes are run. 12 * 13 * @var array 14 */ 15 public $data = []; 16 17 /** 18 * @param array $data Page content data. Keys are roles, values are arrays or strings. 19 * A string value is considered as an array [ 'content' => $value ]. Array keys are: 20 * - content: (string) The slot's content. 21 * - contentmodel: (string, default 'wikitext') The slot's content model. 22 * - contentformat: (string, default 'text/x-wiki') The slot's content format. 23 * - redirect: (string, optional) The redirect target (same format as PageConfig::getTitle), 24 * if this content is a redirect. 25 */ 26 public function __construct( array $data ) { 27 foreach ( $data as $role => $v ) { 28 $this->data[$role] = is_string( $v ) ? [ 'content' => $v ] : $v; 29 } 30 } 31 32 /** @inheritDoc */ 33 public function getRoles(): array { 34 return array_keys( $this->data ); 35 } 36 37 /** @inheritDoc */ 38 public function hasRole( string $role ): bool { 39 return isset( $this->data[$role] ); 40 } 41 42 /** 43 * @param string $role 44 */ 45 private function checkRole( string $role ): void { 46 if ( !isset( $this->data[$role] ) ) { 47 throw new \InvalidArgumentException( "Unknown role \"$role\"" ); 48 } 49 } 50 51 /** @inheritDoc */ 52 public function getModel( string $role ): string { 53 $this->checkRole( $role ); 54 return $this->data[$role]['contentmodel'] ?? 'wikitext'; 55 } 56 57 /** @inheritDoc */ 58 public function getFormat( string $role ): string { 59 $this->checkRole( $role ); 60 return $this->data[$role]['contentformat'] ?? 'text/x-wiki'; 61 } 62 63 /** @inheritDoc */ 64 public function getContent( string $role ): string { 65 $this->checkRole( $role ); 66 if ( !isset( $this->data[$role]['content'] ) ) { 67 throw new \InvalidArgumentException( 'Unknown role or missing content failure' ); 68 } 69 return $this->data[$role]['content']; 70 } 71 72 /** @inheritDoc */ 73 public function getRedirectTarget(): ?string { 74 return $this->data['main']['redirect'] ?? null; 75 } 76 77} 78