summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/SemanticMediaWiki/src/Schema/SchemaFactory.php
blob: 0dd13dad1fd71d2e8272f8056e460f8ab589ff5b (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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
<?php

namespace SMW\Schema;

use RuntimeException;
use SMW\ApplicationFactory;
use SMW\Schema\Exception\SchemaTypeNotFoundException;
use SMW\Schema\Exception\SchemaConstructionFailedException;

/**
 * @license GNU GPL v2+
 * @since 3.0
 *
 * @author mwjames
 */
class SchemaFactory {

	/**
	 * @var []
	 */
	private $schemaTypes = [];

	/**
	 * @since 3.0
	 *
	 * @param array $schemaTypes
	 */
	public function __construct( array $schemaTypes = [] ) {
		$this->schemaTypes = $schemaTypes;

		if ( $this->schemaTypes === [] ) {
			$this->schemaTypes = $GLOBALS['smwgSchemaTypes'];
		}
	}

	/**
	 * @since 3.0
	 *
	 * @param string $type
	 *
	 * @return []
	 */
	public function getType( $type ) {
		return isset( $this->schemaTypes[$type] ) ? $this->schemaTypes[$type] : [];
	}

	/**
	 * @since 3.0
	 *
	 * @param string $type
	 *
	 * @return boolean
	 */
	public function isRegisteredType( $type ) {
		return isset( $this->schemaTypes[$type] );
	}

	/**
	 * @since 3.0
	 *
	 * @return []
	 */
	public function getRegisteredTypes() {
		return array_keys( $this->schemaTypes );
	}

	/**
	 * @since 3.0
	 *
	 * @param string|array $group
	 *
	 * @return []
	 */
	public function getRegisteredTypesByGroup( $group ) {

		$registeredTypes = [];
		$groups = (array)$group;

		foreach ( $this->schemaTypes as $type => $val ) {
			if ( isset( $val['group'] ) && in_array( $val['group'], $groups ) ) {
				$registeredTypes[] = $type;
			}
		}

		return $registeredTypes;
	}

	/**
	 * @since 3.0
	 *
	 * @param string $name
	 * @param array|string $data
	 *
	 * @return Schema
	 * @throws RuntimeException
	 */
	public function newSchema( $name, $data ) {

		if ( is_string( $data ) ) {
			if ( ( $data = json_decode( $data, true ) ) === null || json_last_error() !== JSON_ERROR_NONE ) {
				throw new RuntimeException( "Invalid JSON format." );
			}
		}

		$type = null;
		$validation_schema = null;

		if ( isset( $data['type'] ) ) {
			$type = $data['type'];
		}

		if ( !isset( $this->schemaTypes[$type] ) ) {
			throw new SchemaTypeNotFoundException( $type );
		}

		if ( isset( $this->schemaTypes[$type]['validation_schema'] ) ) {
			$validation_schema = $this->schemaTypes[$type]['validation_schema'];
		}

		if ( isset( $this->schemaTypes[$type]['__factory'] ) && is_callable( $this->schemaTypes[$type]['__factory'] ) ) {
			$schema = $this->schemaTypes[$type]['__factory']( $name, $data );
		} else {
			$schema = new SchemaDefinition( $name, $data, $validation_schema );
		}

		if ( !$schema instanceof Schema ) {
			throw new SchemaConstructionFailedException( $type );
		}

		return $schema;
	}

	public static function newTest( $name, $data ) {
		return '';
	}

	/**
	 * @since 3.0
	 *
	 * @return SchemaValidator
	 */
	public function newSchemaValidator() {
		return new SchemaValidator(
			ApplicationFactory::getInstance()->create( 'JsonSchemaValidator' )
		);
	}

}