1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21:
22:
23: namespace picon;
24:
25: 26: 27: 28: 29: 30: 31:
32: class Radio extends LabeledMarkupContainer
33: {
34: private $group;
35:
36: private function getGroup()
37: {
38: if($this->group!=null)
39: {
40: return $this->group;
41: }
42:
43: $group = null;
44:
45: $callback = function(Component &$component) use (&$group)
46: {
47: $group = $component;
48: return Component::VISITOR_STOP_TRAVERSAL;
49: };
50: $this->visitParents(RadioGroup::getIdentifier(), $callback);
51:
52: $this->group = $group;
53:
54: if($group==null)
55: {
56: throw new \RuntimeException('A radio must be a child of RadioGroup');
57: }
58:
59: return $group;
60: }
61:
62: public function getValue()
63: {
64: return $this->getComponentPath();
65: }
66:
67: private function isChecked()
68: {
69: $group = $this->getGroup();
70:
71: if($group->getRawInput()!=null)
72: {
73: return $group->getRawInput()==$this->getValue();
74: }
75: else
76: {
77: return $group->getModelObject()==$this->getModelObject();
78: }
79:
80: }
81:
82: protected function onComponentTag(ComponentTag $tag)
83: {
84: parent::onComponentTag($tag);
85: $group = $this->getGroup();
86: $this->checkComponentTag($tag, 'input');
87: $this->checkComponentTagAttribute($tag, 'type', 'radio');
88:
89: $tag->put('name', $group->getName());
90: $tag->put('value', $this->getValue());
91:
92: $tag->remove('checked');
93: if($this->isChecked())
94: {
95: $tag->put('checked', 'checked');
96: }
97: }
98:
99: public function getLabel()
100: {
101: return $this->getModelObjectAsString();
102: }
103: }
104:
105: ?>
106: