Completed
Push — master ( 409900...f20dfb )
by Jean-Christophe
03:24
created

BaseComponent::getParam()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 5

Duplication

Lines 6
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 6
loc 6
rs 9.4285
cc 2
eloc 5
nc 2
nop 1
1
<?php
2
3
namespace Ajax\common\components;
4
5
use Ajax\JsUtils;
6
7
/**
8
 * Base component for JQuery UI visuals components
9
 * @author jc
10
 * @version 1.001
11
 */
12 View Code Duplication
abstract class BaseComponent {
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
13
	public $jquery_code_for_compile=array ();
14
	protected $params=array ();
15
16
	/**
17
	 *
18
	 * @var JsUtils
19
	 */
20
	protected $js;
21
22
	public function __construct(JsUtils $js=NULL) {
23
		$this->js=$js;
24
	}
25
26
	protected function getParamsAsJSON($params) {
27
		$result="";
28
		if (sizeof($params)>0) {
29
			$result=json_encode($params, JSON_UNESCAPED_SLASHES);
30
			$result=str_ireplace("%quote%", "\"", $result);
31
		}
32
		return $result;
33
	}
34
35
	public function setParam($key, $value) {
36
		$this->params [$key]=$value;
37
		return $this;
38
	}
39
40
	public function getParam($key) {
41
		$value=null;
42
		if (array_key_exists($key, $this->params))
43
			$value=$this->params [$key];
44
		return $value;
45
	}
46
47
	public function getParams() {
48
		return $this->params;
49
	}
50
51
	public function compile(JsUtils $js=NULL) {
52
		if ($js==NULL)
53
			$js=$this->js;
54
		$script=$this->getScript();
55
		$js->addToCompile($script);
56
	}
57
58
	protected function setParamCtrl($key, $value, $typeCtrl) {
59
		if (is_array($typeCtrl)) {
60
			if (array_search($value, $typeCtrl)===false)
61
				throw new \Exception("La valeur passée a propriété `".$key."` ne fait pas partie des valeurs possibles : {".implode(",", $typeCtrl)."}");
62
		} else {
63
			if (!$typeCtrl($value)) {
64
				throw new \Exception("La fonction ".$typeCtrl." a retourné faux pour l'affectation de la propriété ".$key);
65
			}
66
		}
67
		$this->setParam($key, $value);
68
	}
69
70
	public function setParams($params) {
71
		foreach ( $params as $k => $v ) {
72
			$method="set".ucfirst($k);
73
			if (method_exists($this, $method))
74
				$this->$method($v);
75
			else {
76
				$this->setParam($k, $v);
77
				trigger_error("`".$k."` property n'existe pas", E_USER_NOTICE);
78
			}
79
		}
80
81
		return $this;
82
	}
83
84
	abstract public function getScript();
85
}