1 <?php
2
3 namespace Brickrouge\Renderer;
4
5 use Brickrouge\Element;
6 use Brickrouge\Form;
7 use Brickrouge\Group;
8
9 class Simple extends Element
10 {
11 const GROUP_CLASS = '#group-class';
12
13 protected $form;
14
15 16 17
18 public function __construct(array $attributes=array())
19 {
20 parent::__construct('div', $attributes);
21 }
22
23 public function __invoke(Form $form)
24 {
25 $this->form = $form;
26 $this->children = $form->get_ordered_children();
27 $this->contents = null;
28
29 return $this->render_inner_html();
30 }
31
32 protected function render_inner_html()
33 {
34 $html = '';
35 $groups = $this->group_children();
36
37 foreach ($groups as $group_id => $group)
38 {
39 if (empty($group[self::CHILDREN]))
40 {
41 continue;
42 }
43
44 $html .= PHP_EOL . $this->render_group($group, $group_id) . PHP_EOL;
45 }
46
47 return $html;
48 }
49
50 protected function group_children()
51 {
52 $groups = $this->form[self::GROUPS] ?: array();
53
54 \Brickrouge\stable_sort($groups, function($v) { return isset($v['weight']) ? $v['weight'] : 0; });
55
56
57
58
59
60 foreach ($this->children as $name => $element)
61 {
62 if (!$element) continue;
63
64 $group = is_object($element) ? ($element[self::GROUP] ?: 'primary') : 'primary';
65
66 $groups[$group][self::CHILDREN][$name] = $element;
67 }
68
69 return $groups;
70 }
71
72 protected function render_group(array $group, $key)
73 {
74 $class = isset($group['class']) ? $group['class'] : null;
75
76 if ($key && !is_numeric($key))
77 {
78 $class .= ' group--' . \Brickrouge\normalize($key);
79 }
80
81 $constructor = $this[self::GROUP_CLASS] ?: 'Brickrouge\Group';
82
83 $group = new $constructor
84 (
85 array
86 (
87 self::CHILDREN => $group[self::CHILDREN],
88 self::DESCRIPTION => isset($group['description']) ? $group['description'] : null,
89 self::LEGEND => isset($group['title']) ? $group['title'] : null,
90
91 'class' => $class,
92 'id' => isset($group['id']) ? $group['id'] : null
93 )
94 );
95
96 return (string) $group;
97 }
98 }