1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Ajax\common\components; |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
use Ajax\JsUtils; |
7
|
|
|
/** |
8
|
|
|
* Base component for JQuery UI visuals components |
9
|
|
|
* @author jc |
10
|
|
|
* @version 1.001 |
11
|
|
|
*/ |
12
|
|
|
abstract class BaseComponent { |
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
|
|
View Code Duplication |
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
|
|
|
public function addParams($params){ |
85
|
|
|
foreach ($params as $k=>$v){ |
86
|
|
|
$this->setParam($k, $v); |
87
|
|
|
} |
88
|
|
|
return $this; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
abstract public function getScript(); |
92
|
|
|
|
93
|
|
|
public function setDebug($value){ |
94
|
|
|
return $this->setParam("debug", $value); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
public function setVerbose($value){ |
98
|
|
|
return $this->setParam("verbose", $value); |
99
|
|
|
} |
100
|
|
|
} |
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.