1: <?php
2:
3: /**
4: * Picon Framework
5: * http://code.google.com/p/picon-framework/
6: *
7: * Copyright (C) 2011-2012 Martin Cassidy <martin.cassidy@webquub.com>
8:
9: * Picon Framework is free software: you can redistribute it and/or modify
10: * it under the terms of the GNU General Public License as published by
11: * the Free Software Foundation, either version 3 of the License, or
12: * (at your option) any later version.
13:
14: * Picon Framework is distributed in the hope that it will be useful,
15: * but WITHOUT ANY WARRANTY; without even the implied warranty of
16: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17: * General Public License for more details.
18:
19: * You should have received a copy of the GNU General Public License
20: * along with Picon Framework. If not, see <http://www.gnu.org/licenses/>.
21: * */
22:
23: namespace picon;
24:
25: /**
26: * A form component with pre-defined choices of which 1 can be chosen
27: *
28: * @author Martin Cassidy
29: * @package web/markup/html/form
30: */
31: abstract class AbstractSingleChoice extends AbstractChoice
32: {
33: /**
34: * @todo this will incorrectly evaluate isSelected() as true if the model object is null
35: * and the choice render returns a 0
36: * @param type $choice
37: * @param type $index
38: * @return type
39: */
40: public function isSelected($choice, $index)
41: {
42: if($this->isEmptyInput())
43: {
44: return false;
45: }
46: else
47: {
48: $raw = $this->getRawInput();
49: if(!empty($raw))
50: {
51: return $this->getRawInput()==$this->getChoiceRenderer()->getValue($choice, $index);
52: }
53: else
54: {
55: return Objects::equals($this->getModelObject(), $choice);
56: }
57: }
58: }
59:
60: protected function convertInput()
61: {
62: $value = $this->getRawInput();
63: $i = 1;
64: foreach($this->getChoices() as $index => $choice)
65: {
66: if(is_array($choice))
67: {
68: foreach($choice as $innerIndex => $option)
69: {
70: if($this->valueForChoice($option, $value, $i))
71: {
72: $this->setConvertedInput($option);
73: return;
74: }
75: $i++;
76: }
77: }
78: else
79: {
80: if($this->valueForChoice($choice, $value, $i))
81: {
82: $this->setConvertedInput($choice);
83: return;
84: }
85: }
86: $i++;
87: }
88: }
89: }
90:
91: ?>
92: