Completed
Push — master ( d27f99...f4e97d )
by Jean-Christophe
03:35
created

Javascript   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
wmc 8
lcom 0
cbo 0
dl 0
loc 38
rs 10
c 0
b 0
f 0

3 Methods

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