Code Duplication    Length = 69-69 lines in 2 locations

Ajax/common/components/SimpleComponent.php 1 location

@@ 12-80 (lines=69) @@
9
 * @author jc
10
 * @version 1.001
11
 */
12
abstract class SimpleComponent extends BaseComponent {
13
	protected $attachTo;
14
	protected $uiName;
15
	protected $events;
16
17
	public function __construct(JsUtils $js) {
18
		parent::__construct($js);
19
		$this->events=array ();
20
	}
21
22
	protected function compileEvents() {
23
		foreach ( $this->events as $event => $jsCode ) {
24
			$this->jquery_code_for_compile []="$( \"".$this->attachTo."\" ).on(\"".$event."\" , function( event, data ) {".$jsCode."});";
25
		}
26
	}
27
28
	protected function compileJQueryCode() {
29
		$result=implode("\n", $this->jquery_code_for_compile);
30
		$result=str_ireplace("\"%", "", $result);
31
		$result=str_ireplace("%\"", "", $result);
32
		$result=str_replace(array (
33
				"\\n",
34
				"\\r",
35
				"\\t"
36
		), '', $result);
37
		return $result;
38
	}
39
40
	public function getScript() {
41
		$allParams=$this->params;
42
		$this->jquery_code_for_compile=array ();
43
		$this->jquery_code_for_compile []="$( \"".$this->attachTo."\" ).{$this->uiName}(".$this->getParamsAsJSON($allParams).");";
44
		$this->compileEvents();
45
		return $this->compileJQueryCode();
46
	}
47
48
	/**
49
	 *
50
	 * @param String $identifier identifiant CSS
51
	 */
52
	public function attach($identifier) {
53
		$this->attachTo=$identifier;
54
	}
55
56
	public function addEvent($event, $jsCode) {
57
		return $this->setParam($event, "%function( event, ui ) {".$jsCode."}%");
58
	}
59
60
	public function on($event, $jsCode) {
61
		$this->events [$event]=$jsCode;
62
		return $this;
63
	}
64
65
	protected function setParamCtrl($key, $value, $typeCtrl) {
66
		if (is_array($typeCtrl)) {
67
			if (array_search($value, $typeCtrl)===false)
68
				throw new \Exception("La valeur passée a propriété `".$key."` pour le composant `".$this->uiName."` ne fait pas partie des valeurs possibles : {".implode(",", $typeCtrl)."}");
69
		} else {
70
			if (!$typeCtrl($value)) {
71
				throw new \Exception("La fonction ".$typeCtrl." a retourné faux pour l'affectation de la propriété ".$key." au composant ".$this->uiName);
72
			}
73
		}
74
		return $this->setParam($key, $value);
75
	}
76
77
	public function getAttachTo() {
78
		return $this->attachTo;
79
	}
80
}

Ajax/common/SimpleComponent.php 1 location

@@ 12-80 (lines=69) @@
9
 * @author jc
10
 * @version 1.001
11
 */
12
abstract class SimpleComponent extends BaseComponent {
13
	protected $attachTo;
14
	protected $uiName;
15
	protected $events;
16
17
	public function __construct(JsUtils $js) {
18
		parent::__construct($js);
19
		$this->events=array ();
20
	}
21
22
	protected function compileEvents() {
23
		foreach ( $this->events as $event => $jsCode ) {
24
			$this->jquery_code_for_compile []="$( \"".$this->attachTo."\" ).on(\"".$event."\" , function( event, data ) {".$jsCode."});";
25
		}
26
	}
27
28
	protected function compileJQueryCode() {
29
		$result=implode("\n", $this->jquery_code_for_compile);
30
		$result=str_ireplace("\"%", "", $result);
31
		$result=str_ireplace("%\"", "", $result);
32
		$result=str_replace(array (
33
				"\\n",
34
				"\\r",
35
				"\\t" 
36
		), '', $result);
37
		return $result;
38
	}
39
40
	public function getScript() {
41
		$allParams=$this->params;
42
		$this->jquery_code_for_compile=array ();
43
		$this->jquery_code_for_compile []="$( \"".$this->attachTo."\" ).{$this->uiName}(".$this->getParamsAsJSON($allParams).");";
44
		$this->compileEvents();
45
		return $this->compileJQueryCode();
46
	}
47
48
	/**
49
	 *
50
	 * @param String $identifier identifiant CSS
51
	 */
52
	public function attach($identifier) {
53
		$this->attachTo=$identifier;
54
	}
55
56
	public function addEvent($event, $jsCode) {
57
		return $this->setParam($event, "%function( event, ui ) {".$jsCode."}%");
58
	}
59
60
	public function on($event, $jsCode) {
61
		$this->events [$event]=$jsCode;
62
		return $this;
63
	}
64
65
	protected function setParamCtrl($key, $value, $typeCtrl) {
66
		if (is_array($typeCtrl)) {
67
			if (array_search($value, $typeCtrl)===false)
68
				throw new \Exception("La valeur passée a propriété `".$key."` pour le composant `".$this->uiName."` ne fait pas partie des valeurs possibles : {".implode(",", $typeCtrl)."}");
69
		} else {
70
			if (!$typeCtrl($value)) {
71
				throw new \Exception("La fonction ".$typeCtrl." a retourné faux pour l'affectation de la propriété ".$key." au composant ".$this->uiName);
72
			}
73
		}
74
		return $this->setParam($key, $value);
75
	}
76
77
	public function getAttachTo() {
78
		return $this->attachTo;
79
	}
80
}