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

Javascript::containsCode()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 3
eloc 2
nc 3
nop 1
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
}