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