Completed
Push — move ( 875a6f )
by Jeroen De
04:17
created

CoordinateDescription::getSQLCondition()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 36

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
dl 0
loc 36
ccs 0
cts 28
cp 0
rs 8.7217
c 0
b 0
f 0
cc 6
nc 6
nop 3
crap 42
1
<?php
2
3
namespace Maps\SemanticMW\ValueDescriptions;
4
5
use DatabaseBase;
6
use SMW\DataValueFactory;
7
use SMW\Query\Language\ValueDescription;
8
use SMWDIGeoCoord;
9
10
/**
11
 * Description of one data value of type Geographical Coordinates.
12
 *
13
 * @author Jeroen De Dauw
14
 */
15
class CoordinateDescription extends ValueDescription {
16
17
	/**
18
	 * @see SMWDescription::getQueryString
19
	 *
20
	 * @since 0.6
21
	 *
22
	 * @param boolean $asValue
23
	 *
24
	 * @return string
25
	 */
26
	public function getQueryString( $asValue = false ) {
27
		$queryString = DataValueFactory::newDataItemValue( $this->getDataItem(), $this->getProperty() )->getWikiValue();
28
		return $asValue ? $queryString : "[[$queryString]]";
29
	}
30
31
	/**
32
	 * @see SMWDescription::getSQLCondition
33
	 *
34
	 * FIXME: store specific code should be in the store component
35
	 *
36
	 * @since 0.6
37
	 *
38
	 * @param string $tableName
39
	 * @param array $fieldNames
40
	 * @param DatabaseBase $dbs
41
	 *
42
	 * @return string|false
43
	 */
44
	public function getSQLCondition( $tableName, array $fieldNames, DatabaseBase $dbs ) {
45
		$dataItem = $this->getDataItem();
46
47
		// Only execute the query when the description's type is geographical coordinates,
48
		// the description is valid, and the near comparator is used.
49
		if ( $dataItem instanceof SMWDIGeoCoord ) {
0 ignored issues
show
Bug introduced by
The class SMWDIGeoCoord does not exist. Is this class maybe located in a folder that is not analyzed, or in a newer version of your dependencies than listed in your composer.lock/composer.json?
Loading history...
50
			switch ( $this->getComparator() ) {
51
				case SMW_CMP_EQ:
52
					$comparator = '=';
53
					break;
54
				case SMW_CMP_LEQ:
55
					$comparator = '<=';
56
					break;
57
				case SMW_CMP_GEQ:
58
					$comparator = '>=';
59
					break;
60
				case SMW_CMP_NEQ:
61
					$comparator = '!=';
62
					break;
63
				default:
64
					return false;
65
			}
66
67
			$lat = $dbs->addQuotes( $dataItem->getLatitude() );
68
			$lon = $dbs->addQuotes( $dataItem->getLongitude() );
69
70
			$conditions = [];
71
72
			$conditions[] = "{$tableName}.$fieldNames[1] $comparator $lat";
73
			$conditions[] = "{$tableName}.$fieldNames[2] $comparator $lon";
74
75
			return implode( ' AND ', $conditions );
76
		}
77
78
		return false;
79
	}
80
81
}