PolygonHandler::validateText()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 0
cts 10
cp 0
rs 9.9332
c 0
b 0
f 0
cc 3
nc 3
nop 0
crap 12
1
<?php
2
3
/**
4
 * Class to Handle Polygons in SM.
5
 * This class is used to convert the string representation
6
 * of Polygons to concrete structures.
7
 * Also acts as a factory class for polygons
8
 *
9
 * @since 2.1
10
 *
11
 * @file SM_PolygonHandler.php
12
 *
13
 * @author Nischay Nahata
14
 */
15
class PolygonHandler {
16
17
	/**
18
	 * The string used to store this value as a string in SMW.
19
	 *
20
	 * @since 2.1
21
	 *
22
	 * @var string
23
	 */
24
	protected $text;
25
26
	/**
27
	 * The string used to store this value as an object.
28
	 *
29
	 * @since 2.1
30
	 *
31
	 * @var object or null
32
	 */
33
	protected $value = null;
34
35
	/**
36
	 * The array of error messages occurred in parsing.
37
	 *
38
	 * @since 2.1
39
	 *
40
	 * @var array
41
	 */
42
	protected $errors = [];
43
44
	/**
45
	 * Array of classes used to validate different Geographic shapes.
46
	 *
47
	 * @since 2.1
48
	 *
49
	 * @var array
50
	 */
51
	protected $validatorClasses = [
52
		'locations' => 'LocationValidator',
53
		'lines' => 'LineValidator',
54
		'polygons' => 'PolygonValidator',
55
		'circles' => 'CircleValidator',
56
		'rectangles' => 'RectangleValidator'
57
	];
58
59
	/**
60
	 * Array of classes of different Geographic shapes.
61
	 *
62
	 * @since 2.1
63
	 *
64
	 * @var array
65
	 */
66
	protected $geoClasses = [
67
		'locations' => 'MapsLocation',
68
		'lines' => 'MapsLine',
69
		'polygons' => 'MapsPolygon',
70
		'circles' => 'MapsCircle',
71
		'rectangles' => 'MapsRectanlge'
72
	];
73
74
	/**
75
	 * NOTE: These need to be changed as Manipulations are depreceated.
76
	 * Array of classes for param handling of different Geographic shapes.
77
	 *
78
	 * @since 2.1
79
	 *
80
	 * @var array
81
	 */
82
	protected $paramClasses = [
83
		'locations' => 'MapsParamLocation',
84
		'lines' => 'MapsParamLine',
85
		'polygons' => 'MapsParamPolygon',
86
		'circles' => 'MapsParamCircle',
87
		'rectangles' => 'MapsParamRectangle'
88
	];
89
90
	/**
91
	 * Constructor.
92
	 *
93
	 * @since 2.1
94
	 *
95
	 * @param string $text
96
	 */
97
	public function __construct( $text ) {
98
		$this->text = $text;
99
	}
100
101
	public function getGeoType() {
102
		$parts = explode( '=', $this->text );
103
		return current( $parts );
104
	}
105
106
	public function getValidationErrors() {
107
		$this->validateText();
108
		return $this->errors;
109
	}
110
111
	protected function validateText() {
112
		$parts = explode( '=', $this->text );
113
		if( array_key_exists( $parts[0], $this->validatorClasses ) ) {
114
			$validatorClass = new $this->validatorClasses[ $parts[0] ]( '~' );
115
			if ( !$validatorClass->doValidation( $parts[1] ) )
116
				$this->errors[] = wfMessage( 'semanticmaps-shapes-improperformat', $this->text )->escaped();
117
		} else {
118
			$this->errors[] = wfMessage( 'semanticmaps-shapes-missingshape', $parts[0] )->escaped();
119
		}
120
	}
121
122
	/**
123
	 * Gets an object of the model class the string represents.
124
	 *
125
	 * @since 2.1
126
	 */
127
	public function shapeFromText() {
128
		$parts = explode( '~' , $this->text );
129
		$shape = explode( '=', array_shift( $parts ) );
130
		if( array_key_exists( $shape[0] , $this->geoClasses ) ) {
131
			$geoClass = new $this->geoClasses[ $shape[0] ]( explode( ':' , $shape[1] ) );
132
133
			return $geoClass;
134
		} else {
135
			return false;
136
		}
137
	}
138
}
139