-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathAbstractWidget.php
More file actions
92 lines (83 loc) · 2.06 KB
/
Copy pathAbstractWidget.php
File metadata and controls
92 lines (83 loc) · 2.06 KB
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
<?php
namespace DesignPatterns\Structural\Composite;
/**
* Base class for all html widgets in composition that:
* - defines common variables like `name`, `parent`,
* - implements default logic of children management methods,
* - defines the `render` method that renders html code of element (and its children if available).
*
* Corresponds to `Component` in the Composite pattern.
*
* @author Vlad Riabchenko <contact@vria.eu>
*/
abstract class AbstractWidget
{
/**
* The name of an element.
*
* @var string
*/
protected $name;
/**
* Reference to the parent.
*
* @var AbstractWidget
*/
protected $parent;
/**
* Constructor.
*
* @param string $name
*/
public function __construct($name)
{
$this->name = $name;
}
/**
* Get name.
*
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* Get parent.
*
* @return AbstractWidget
*/
public function getParent()
{
return $this->parent;
}
/**
* Add child. The name of the child allows to retrieve it afterwards.
* This is a default implementation that leaves should live unchanged.
* The composite subclasses must overwrite it to save a child.
*
* @param AbstractWidget $child
*/
public function add(AbstractWidget $child)
{
throw new \BadMethodCallException("Simple form element cannot have children");
}
/**
* Get child by its name.
* This is a default implementation that leaves should live unchanged.
* The composite subclasses must overwrite it to return a child.
*
* @param string $name
*
* @return AbstractWidget
*/
public function get($name)
{
throw new \BadMethodCallException("Simple form element cannot have children");
}
/**
* Render entire element.
* Leaves will simply output some html while composites will ask their children to render themselves.
*/
public abstract function render();
}