Completed
Push — master ( 59c431...219a45 )
by mw
36:38
created

isKnownByLabelAndLanguage()   B

Complexity

Conditions 7
Paths 16

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 9
nc 16
nop 2
dl 0
loc 18
rs 8.2222
c 0
b 0
f 0
1
<?php
2
3
namespace SMW\DataValues\ValueValidators;
4
5
use SMWDataValue as DataValue;
6
use SMW\ApplicationFactory;
7
use SMW\DIProperty;
8
9
/**
10
 * @private
11
 *
12
 * @license GNU GPL v2+
13
 * @since 2.5
14
 *
15
 * @author mwjames
16
 */
17
class PropertySpecificationConstraintValueValidator implements ConstraintValueValidator {
18
19
	/**
20
	 * @var boolean
21
	 */
22
	private $hasConstraintViolation = false;
23
24
	/**
25
	 * @var array
26
	 */
27
	private static $inMemoryLabelToLanguageTracer = array();
28
29
	/**
30
	 * @since 2.5
31
	 *
32
	 * {@inheritDoc}
33
	 */
34
	public function hasConstraintViolation() {
35
		return $this->hasConstraintViolation;
36
	}
37
38
	/**
39
	 * @since 2.5
40
	 *
41
	 * {@inheritDoc}
42
	 */
43
	public function validate( $dataValue ) {
44
45
		$this->hasConstraintViolation = false;
46
47
		if (
48
			!$dataValue instanceof DataValue ||
49
			$dataValue->getProperty() === null ||
50
			$dataValue->getContextPage() === null ||
51
			$dataValue->getContextPage()->getNamespace() !== SMW_NS_PROPERTY ) {
52
			return $this->hasConstraintViolation;
53
		}
54
55
		if ( $dataValue->getProperty()->getKey() === '_PPLB' ) {
56
			return $this->doValidateCodifiedPreferredPropertyLabelConstraints( $dataValue );
57
		}
58
	}
59
60
	private function doValidateCodifiedPreferredPropertyLabelConstraints( $dataValue ) {
61
62
		// Annotated but not enabled
63
		if ( !$dataValue->isEnabledFeature( SMW_DV_PPLB ) ) {
64
			return $dataValue->addErrorMsg(
65
				array(
66
					'smw-datavalue-feature-not-supported',
67
					'SMW_DV_PPLB'
68
				)
69
			);
70
		}
71
72
		$value = $dataValue->toArray();
73
		$dbKey = $dataValue->getContextPage()->getDBKey();
74
75
		// Language has been already assigned!
76
		if ( ( $isKnownBy = $this->isKnownByLabelAndLanguage( $value, $dbKey ) ) !== false ) {
77
			$dataValue->addErrorMsg(
78
				array(
79
					'smw-property-preferred-label-language-combination-exists',
80
					$value['_TEXT'],
81
					$value['_LCODE'],
82
					$isKnownBy
83
				)
84
			);
85
		}
86
	}
87
88
	private function isKnownByLabelAndLanguage( $value, $dbkey ) {
89
90
		$lang = isset( $value['_LCODE'] ) ? $value['_LCODE'] : false;
91
92
		if ( !isset( self::$inMemoryLabelToLanguageTracer[$dbkey] ) ) {
93
			self::$inMemoryLabelToLanguageTracer[$dbkey] = array();
94
		}
95
96
		if ( $lang && !isset( self::$inMemoryLabelToLanguageTracer[$dbkey][$lang] ) ) {
97
			self::$inMemoryLabelToLanguageTracer[$dbkey][$lang] = $value['_TEXT'];
98
		}
99
100
		if ( $lang && self::$inMemoryLabelToLanguageTracer[$dbkey][$lang] !== $value['_TEXT'] ) {
101
			return self::$inMemoryLabelToLanguageTracer[$dbkey][$lang];
102
		}
103
104
		return false;
105
	}
106
107
}
108