Passed
Push — master ( cbfe11...bfcee2 )
by Jean-Christophe
03:44
created

BaseGui::clearComponents()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
namespace Ajax\common;
3
4
use Ajax\common\components\SimpleComponent;
5
use Ajax\JsUtils;
6
use Ajax\common\html\BaseHtml;
7
8
/**
9
 * BaseGui Phalcon library
10
 *
11
 * @author jcheron
12
 * @version 1.02
13
 */
14
15
/**
16
 * BaseGui
17
 */
18
class BaseGui {
19
20
	protected $autoCompile;
21
22
	protected $components;
23
24
	protected $htmlComponents;
25
26
	/**
27
	 *
28
	 * @var \Ajax\JsUtils
29
	 */
30
	protected $js;
31
32
	public function __construct($autoCompile = true) {
33
		$this->autoCompile = $autoCompile;
34
		$this->components = array();
35
		$this->htmlComponents = array();
36
	}
37
38
	public function isAutoCompile() {
39
		return $this->autoCompile;
40
	}
41
42
	public function setAutoCompile($autoCompile) {
43
		$this->autoCompile = $autoCompile;
44
		return $this;
45
	}
46
47
	public function compile($internal = false) {
48
		if ($internal === false && $this->autoCompile === true)
49
			throw new \Exception("Impossible to compile if autoCompile is set to 'true'");
50
		foreach ($this->components as $component) {
51
			$component->compile();
52
		}
53
	}
54
55
	public function setJs(JsUtils $js) {
56
		$this->js = $js;
57
	}
58
59
	public function addComponent(SimpleComponent $component, $attachTo, $params) {
60
		if ($this->autoCompile) {
61
			if ($attachTo != null) {
62
				if (! isset($this->components[$attachTo])) {
63
					$this->components[$attachTo] = $component;
64
				} else {
65
					$this->components[] = $component;
66
				}
67
				$component->attach($attachTo);
68
			} else {
69
				$this->components[] = $component;
70
			}
71
		}
72
		if (isset($params))
73
			if (\is_array($params))
74
				$component->setParams($params);
75
		return $component;
76
	}
77
78
	public function addHtmlComponent(BaseHtml $htmlComponent) {
79
		$this->htmlComponents[$htmlComponent->getIdentifier()] = $htmlComponent;
80
		return $htmlComponent;
81
	}
82
83
	public function compileHtml(JsUtils $js = NULL, &$view = NULL) {
84
		foreach ($this->htmlComponents as $htmlComponent) {
85
			$htmlComponent->compile($js, $view);
86
		}
87
	}
88
89
	public function matchHtmlComponents($callback) {
90
		return array_filter($this->htmlComponents, $callback);
91
	}
92
	
93
	public function getHtmlComponent($identifier){
94
		return $this->htmlComponents[$identifier]??'';
95
	}
96
    public function clearComponents(){
97
        $this->components=[];
98
        $this->htmlComponents=[];
99
    }
100
}
101