forked from padaVVan/yii2-placer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAbstractPlace.php
131 lines (111 loc) · 2.59 KB
/
AbstractPlace.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
<?php
namespace padavvan\placer;
use padavvan\placer\dependencies\Dependency;
use yii\base\InvalidConfigException;
abstract class AbstractPlace extends \yii\base\BaseObject
{
/**
* Place name
* @var string
*/
protected $name = null;
/**
* Wrap tag name
* @var null|string
*/
public $tag = null;
/**
* Wrap tag config
* @var array
*/
public $options = [];
/**
* @var \padavvan\placer\dependencies\Dependency[]
*/
private ?array $_dependencies = null;
public function init()
{
if ($this->name === null) {
throw new InvalidConfigException('Must name');
}
}
/**
* Add new place
*/
abstract public function push(AbstractPlace $place);
/**
* Remove place
*/
abstract public function remove(AbstractPlace $place);
/**
* Render place
* @return string|void
*/
abstract public function render();
/**
* Dependency setter
* @return $this
* @throws InvalidConfigException
*/
public function setDependency(mixed $values)
{
if ($values instanceof Dependency) {
$deps[] = $values;
} elseif (is_array($values)) {
$deps = $values;
} else {
return $this;
}
foreach ($deps as $value) {
if (!($value instanceof Dependency)) {
throw new InvalidConfigException('Param must be a Dependency object');
}
}
$this->_dependencies = $deps;
return $this;
}
/**
* Name setter
* @param string $value
*/
public function setName($value)
{
$this->name = $value;
}
/**
* Display place or not.
* For this pass on all the dependencies and evaluate the value.
* If at least one dependency is not satisfied then returns false.
* @return bool
*/
protected function isView()
{
if ($this->_dependencies === null) {
return true;
}
$evaluate = true;
foreach ($this->_dependencies as $dependency) {
$evaluate = $evaluate && $dependency->evaluateDependency();
}
return $evaluate;
}
/**
* @param $tagName
* @param $options
* @return $this
*/
public function wrap($tagName, $options)
{
$this->tag = $tagName;
$this->options = (array)$options;
return $this;
}
/**
* @param $config
* @return static
*/
public static function create($config)
{
return new static($config);
}
}