Completed
Push — master ( ef9455...966ba4 )
by Jean-Christophe
02:39
created

Javascript   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 10
lcom 0
cbo 1
dl 0
loc 48
rs 10
c 2
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A containsCode() 0 3 3
A prep_element() 0 6 2
A prep_value() 0 9 3
A prep_jquery_selector() 0 6 2
1
<?php
2
3
namespace Ajax\service;
4
5
class Javascript {
6
	public static $preventDefault="\nif(event && event.preventDefault) event.preventDefault();\n";
7
	public static $stopPropagation="\nif(event && event.stopPropagation) event.stopPropagation();\n";
8
9
	public static function containsCode($expression){
10
		return strrpos($expression, 'this')!==false||strrpos($expression, 'event')!==false||strrpos($expression, 'self')!==false;
11
	}
12
13
	/**
14
	 * Puts HTML element in quotes for use in jQuery code
15
	 * unless the supplied element is the Javascript 'this'
16
	 * object, in which case no quotes are added
17
	 *
18
	 * @param string $element
19
	 * @return string
20
	 */
21
	public static function prep_element($element) {
22
		if (self::containsCode($element)===false) {
23
			$element='"'.addslashes($element).'"';
24
		}
25
		return $element;
26
	}
27
28
	/**
29
	 * Puts HTML values in quotes for use in jQuery code
30
	 * unless the supplied value contains the Javascript 'this' or 'event'
31
	 * object, in which case no quotes are added
32
	 *
33
	 * @param string $value
34
	 * @return string
35
	 */
36
	public static function prep_value($value) {
37
		if (\is_array($value)) {
38
			$value=implode(",", $value);
39
		}
40
		if (self::containsCode($value)===false) {
41
			$value='"'.$value.'"';
42
		}
43
		return $value;
44
	}
45
46
	public static function prep_jquery_selector($value){
47
		if(JString::startswith($value, '$(')===false){
48
			return '$('.$value.')';
49
		}
50
		return $value;
51
	}
52
}
53