ar_nlsDictionary::setLanguage()   A
last analyzed

Complexity

Conditions 6
Paths 12

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
cc 6
nc 12
nop 1
dl 0
loc 18
rs 9.0444
c 0
b 0
f 0
ccs 0
cts 17
cp 0
crap 42
1
<?php
2
3
ar_pinp::allow( 'ar_nls' );
4
ar_pinp::allow( 'ar_nlsDictionary', array("load", "setLanguage", "getLanguage") );
5
6
class ar_nls extends arBase {
7
8
	public static function dict($defaultLanguage, $currentLanguage = null, $defaultVarName = "ARnls", $baseDir = null ) {
9
		if ( !$baseDir ) {
10
			global $store; // FIXME: remove dependency on $store, use arbasedir in some way.
11
			$baseDir = $store->get_config("code")."nls/";
12
		}
13
		return new ar_nlsDictionary($baseDir, $defaultLanguage, $currentLanguage, $defaultVarName );
14
	}
15
16
}
17
18
class ar_nlsDictionary extends arBase implements ArrayAccess, Iterator {
19
20
	private $currentList = null;
21
	private $defaultList = null;
22
	private $baseDir = null;
23
	private $languages = array();
24
	private $loaded = array();
25
	public $currentLanguage = null;
26
	public $defaultLanguage = null;
27
28
	public function __construct( $baseDir, $defaultLanguage, $currentLanguage = null, $defaultVarName = "ARnls" ) {
29
30
		$this->baseDir = $baseDir;
31
		$this->defaultVarName = $defaultVarName;
0 ignored issues
show
Bug introduced by
The property defaultVarName does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
32
33
		if( !isset($currentLanguage) ) {
34
			$currentLanguage = $defaultLanguage;
35
		}
36
37
		$this->languages[$defaultLanguage] = array();
38
		$this->languages[$currentLanguage] = array();
39
40
		$this->defaultList = &$this->languages[$defaultLanguage];
41
		$this->currentList = &$this->languages[$currentLanguage];
42
43
		$this->currentLanguage = $currentLanguage;
44
		$this->defaultLanguage = $defaultLanguage;
45
46
		$this->load();
47
	}
48
49
	public function setLanguage( $language ) {
50
		if( !isset($language) || $language == "" ) {
51
			$language = $this->defaultLanguage;
52
		}
53
		if( !isset( $this->languages[$language] ) ) {
54
			$this->languages[$language] = array();
55
		}
56
		$this->currentList = &$this->languages[$language];
57
		$this->currentLanguage = $language;
58
		$this->load();
59
60
		foreach( $this->loaded as $section => $nlslist ) {
61
			if( !isset($nlslist[$language]) ) {
62
				$this->load($section);
63
			}
64
		}
65
		return $this;
66
	}
67
68
	public function getLanguage( $language = null ) {
69
		return isset($language) ? $this->languages[$language] : $this->currentList;
70
	}
71
72
	public function load( $section = "", $nls = null, $varName = null ) {
73
		if( !isset($nls) ) {
74
			$nls = $this->currentLanguage;
75
		}
76
		if( !isset($varName) ) {
77
			$varName = $this->defaultVarName;
78
		}
79
80
		$nls = preg_replace('|[^a-z]*|i','',$nls);
81
		$re = '|[^a-z-_.:0-9/]*|i';				// only allow 'normal' characters
82
		$section = str_replace('//', '/', 			// protects against urls in the form of //google.com
83
				str_replace('..', '', 			// protects against ../../../../../etc/passwd
84
				preg_replace($re, '', $section))); // add .js if not set, remove .. and other dirty characters
85
86
		if (strpos($section, 'current:')!==false) {
87
				$context = pobject::getContext();
88
				$arLibraryPath = $context['arLibraryPath'];
89
				$sectionCacheName = str_replace( 'current:', $arLibraryPath.':', $section);
90
		} else {
91
				$sectionCacheName = $section;
92
		}
93
94
		if( !$section ) {
95
			if( !$this->loaded[$sectionCacheName][$nls] && ($fullFile = $this->baseDir.$nls) && file_exists($fullFile) ) {
96
				include($fullFile);
97
				$this->loaded[$sectionCacheName][$nls] = true;
98
				$this->languages[$nls] = array_merge((array)$this->languages[$nls], (array)$$varName);
99
			}
100
		} elseif( !$this->loaded[$sectionCacheName][$nls] ) {
101
			$this->loaded[$sectionCacheName][$nls] = true;
102
			$fullFile = $this->baseDir.$section.".".$nls;
103
			if( file_exists($fullFile) ) {
104
				include($fullFile);
105
				$this->languages[$nls] = array_merge((array)$this->languages[$nls], (array)$$varName);
106
			} else {
107
				// FIXME: UGLY hack stolen from pobject::loadtext()
108
				global $ARCurrent;
109
				$context = pobject::getContext();
110
				$me = $context["arCurrentObject"];
111
				if ( !$me || !is_object($me) ) {
112
					//FIXME: this should not happen, but ldObjectNotFound() sometimes triggers this
113
					// the problem is that there is no arCurrentObject pushed on the stack
114
					// generally we can just return and nothing serious will happen
115
					debug('No current object found on the context stack, skipping loadtext', 'all');
116
					return $this;
117
				}
118
				$arResult = $ARCurrent->arResult;
119
				$me->pushContext(array());
120
					$oldnls = $me->reqnls;
121
					$me->reqnls = $nls;
122
					$oldAllnls = $ARCurrent->allnls;
123
					$ARCurrent->allnls = true;
124
					$me->CheckConfig($section, array("nls" => $nls));
125
					$ARCurrent->allnls = $oldAllnls;
126
					$me->reqnls = $oldnls;
127
				$me->popContext();
128
129
				$nlsarray = array();
130
				if( is_array($ARCurrent->arResult) ) {
131
					$nlsarray = $ARCurrent->arResult;
132
				} elseif( is_array($me->{$varName}) ) {
133
					$nlsarray = $me->{$varName};
134
				} elseif( is_array($ARCurrent->{$varName}) ) {
135
					$nlsarray = $ARCurrent->{$varName};
136
				}
137
				$ARCurrent->arResult = $arResult;
138
				$this->languages[$nls] = array_merge((array)$this->languages[$nls], (array)$nlsarray);
139
			}
140
		}
141
		return $this;
142
	}
143
144
	/* ArrayAccess */
145
146 1
	public function offsetSet( $offset, $value ) {
147 1
		if ($offset == "") {
148
			$this->currentList[] = $value;
149
		} else {
150 1
			$this->currentList[$offset] = $value;
151
		}
152 1
	}
153
154
	public function offsetExists( $offset ) {
155
		return ( $this->getEntry( $offset ) !== null );
156
	}
157
158
	public function offsetUnset( $offset ) {
159
		unset($this->currentList[$offset]);
160
	}
161
162 7
	private function getEntry( $offset ) {
163 7 View Code Duplication
		if( isset( $this->currentList[$offset] ) ) {
164 7
			return $this->currentList[$offset];
165
		} elseif( strpos($offset, ":") !== false ) { // $ARnls["ariadne:foo"] => try and autoload "ariadne.$currentLanguage"
166
			list($section, $rest) = explode(":", $offset, 2);
0 ignored issues
show
Unused Code introduced by
The assignment to $rest is unused. Consider omitting it like so list($first,,$third).

This checks looks for assignemnts to variables using the list(...) function, where not all assigned variables are subsequently used.

Consider the following code example.

<?php

function returnThreeValues() {
    return array('a', 'b', 'c');
}

list($a, $b, $c) = returnThreeValues();

print $a . " - " . $c;

Only the variables $a and $c are used. There was no need to assign $b.

Instead, the list call could have been.

list($a,, $c) = returnThreeValues();
Loading history...
167
			$this->load($section, $this->currentLanguage);
168
			if( isset( $this->currentList[$offset] ) ) {
169
				return $this->currentList[$offset];
170
			}
171
		}
172 View Code Duplication
		if( isset( $this->defaultList[$offset] ) ) {
173
			return $this->defaultList[$offset];
174
		} elseif( strpos($offset, ":") !== false ) { // $ARnls["ariadne:foo"] => try and autoload "ariadne.$defaultLanguage"
175
			list($section, $rest) = explode(":", $offset, 2);
0 ignored issues
show
Unused Code introduced by
The assignment to $rest is unused. Consider omitting it like so list($first,,$third).

This checks looks for assignemnts to variables using the list(...) function, where not all assigned variables are subsequently used.

Consider the following code example.

<?php

function returnThreeValues() {
    return array('a', 'b', 'c');
}

list($a, $b, $c) = returnThreeValues();

print $a . " - " . $c;

Only the variables $a and $c are used. There was no need to assign $b.

Instead, the list call could have been.

list($a,, $c) = returnThreeValues();
Loading history...
176
			$this->load($section, $this->defaultLanguage);
177
			if( isset( $this->defaultList[$offset] ) ) {
178
				return $this->defaultList[$offset];
179
			}
180
		}
181
		return null;
182
	}
183
184
185 7
	public function offsetGet( $offset ) {
186 7
		$value = $this->getEntry( $offset );
187 7
		return ( isset( $value ) ? $value : "{".$offset."}" );
188
	}
189
190
	/* Iterator */
191
	public function current() {
192
		return current($this->currentList);
193
	}
194
195
	public function key() {
196
		return key($this->currentList);
197
	}
198
199
	public function next() {
200
		return next($this->currentList);
201
	}
202
203
	public function rewind() {
204
		return reset($this->currentList);
205
	}
206
207
	public function valid() {
208
		$value = key($this->currentList);
209
		return isset($value);
210
	}
211
212
}
213