summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/SemanticMediaWiki/src/ParameterListDocBuilder.php
blob: 369d66c83536c1ed723ee6eb738b477f61817c62 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
<?php

namespace SMW;

use ParamProcessor\ParamDefinition;

/**
 * @since 2.4
 *
 * @license GNU GPL v2+
 * @author Jeroen De Dauw < jeroendedauw@gmail.com >
 */
class ParameterListDocBuilder {

	/**
	 * @var callable
	 */
	private $msg;

	/**
	 * @param callable $messageFunction
	 */
	public function __construct( callable $messageFunction ) {
		$this->msg = $messageFunction;
	}

	/**
	 * Returns the wikitext for a table listing the provided parameters.
	 *
	 * @param ParamDefinition[] $paramDefinitions
	 *
	 * @return string
	 */
	public function getParameterTable( array $paramDefinitions ) {
		$tableRows = [];
		$hasAliases = $this->containsAliases( $paramDefinitions );

		foreach ( $paramDefinitions as $parameter ) {
			if ( $parameter->getName() !== 'format' ) {
				$tableRows[] = $this->getDescriptionRow( $parameter, $hasAliases );
			}
		}

		if ( empty( $tableRows ) ) {
			return '';
		}

		$tableRows = array_merge( [
			'!' . $this->msg( 'validator-describe-header-parameter' ) ."\n" .
			( $hasAliases ? '!' . $this->msg( 'validator-describe-header-aliases' ) ."\n" : '' ) .
			'!' . $this->msg( 'validator-describe-header-type' ) ."\n" .
			'!' . $this->msg( 'validator-describe-header-default' ) ."\n" .
			'!' . $this->msg( 'validator-describe-header-description' )
		], $tableRows );

		return '{| class="wikitable sortable"' . "\n" .
			implode( "\n|-\n", $tableRows ) .
			"\n|}";
	}

	/**
	 * @param ParamDefinition[] $paramDefinitions
	 *
	 * @return boolean
	 */
	private function containsAliases( array $paramDefinitions ) {
		foreach ( $paramDefinitions as $parameter ) {
			if ( !empty( $parameter->getAliases() ) ) {
				return true;
			}
		}

		return false;
	}

	/**
	 * Returns the wikitext for a table row describing a single parameter.
	 *
	 * @param ParamDefinition $parameter
	 * @param boolean $hasAliases
	 *
	 * @return string
	 */
	private function getDescriptionRow( ParamDefinition $parameter, $hasAliases ) {
		if ( $hasAliases ) {
			$aliases = $parameter->getAliases();
			$aliases = count( $aliases ) > 0 ? implode( ', ', $aliases ) : ' -';
		}

		$description = $this->msg( $parameter->getMessage() );

		$type = $this->msg( $parameter->getTypeMessage() );

		$default = $parameter->isRequired() ? "''" . $this->msg( 'validator-describe-required' ) . "''" : $parameter->getDefault();
		if ( is_array( $default ) ) {
			$default = implode( ', ', $default );
		}
		elseif ( is_bool( $default ) ) {
			$default = $default ? 'yes' : 'no';
		}

		if ( $default === '' ) {
			$default = "''" . $this->msg( 'validator-describe-empty' ) . "''";
		}

		return "|{$parameter->getName()}\n"
		. ( $hasAliases ? '|' . $aliases . "\n" : '' ) .
		<<<EOT
|{$type}
|{$default}
|{$description}
EOT;
	}

	private function msg() {
		return call_user_func_array( $this->msg, func_get_args() );
	}

}