summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/SemanticMediaWiki/src/SQLStore/TableBuilder/Table.php
blob: 5565bda9cb2aaf722e91b4f2b8a1b00c5dba3a2b (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
120
121
122
123
124
125
126
127
128
129
130
<?php

namespace SMW\SQLStore\TableBuilder;

use RuntimeException;

/**
 * @private
 *
 * @license GNU GPL v2+
 * @since 2.5
 *
 * @author mwjames
 */
class Table {

	/**
	 * @var string
	 */
	private $name;

	/**
	 * @var array
	 */
	private $attributes = [];

	/**
	 * @since 2.5
	 *
	 * @param string $name
	 */
	public function __construct( $name ) {
		$this->name = $name;
	}

	/**
	 * @since 2.5
	 *
	 * @param string
	 */
	public function getName() {
		return $this->name;
	}

	/**
	 * @since 2.5
	 *
	 * @param string
	 */
	public function getHash() {
		return json_encode( $this->attributes );
	}

	/**
	 * @since 2.5
	 *
	 * @param array
	 */
	public function getAttributes() {
		return $this->attributes;
	}

	/**
	 * @since 3.0
	 *
	 * @param string $key
	 *
	 * @param mixed
	 */
	public function get( $key ) {

		if ( !isset( $this->attributes[$key] ) ) {
			throw new RuntimeException( "$key is a reserved option key." );
		}

		return $this->attributes[$key];
	}

	/**
	 * @since 2.5
	 *
	 * @param string $fieldName
	 * @param string|array $fieldType
	 */
	public function addColumn( $fieldName, $fieldType ) {
		$this->attributes['fields'][$fieldName] = $fieldType;
	}

	/**
	 * @since 2.5
	 *
	 * @param string|array $index
	 * @param string|null $key
	 */
	public function addIndex( $index, $key = null ) {
		if ( $key !== null ) {
			$this->attributes['indices'][$key] = $index;
		} else {
			$this->attributes['indices'][] = $index;
		}
	}

	/**
	 * @since 3.0
	 *
	 * @param string $fieldName
	 * @param string|int $default
	 */
	public function addDefault( $fieldName, $default ) {
		$this->attributes['defaults'][$fieldName] = $default;
	}

	/**
	 * @since 2.5
	 *
	 * @param string $key
	 * @param string|array $option
	 *
	 * @throws RuntimeException
	 */
	public function addOption( $key, $option ) {

		if ( $key === 'fields' || $key === 'indices' || $key === 'defaults' ) {
			throw new RuntimeException( "$key is a reserved option key." );
		}

		$this->attributes[$key] = $option;
	}

}