@@ 12-85 (lines=74) @@ | ||
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 | 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 | } |
@@ 12-85 (lines=74) @@ | ||
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 | 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 | } |