/
RadioGroup.php
169 lines (147 loc) · 3.79 KB
/
RadioGroup.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
<?php
/**
* This file is part of the QA-Tools library.
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*
* @copyright Alexander Obuhovich <aik.bold@gmail.com>
* @link https://github.com/qa-tools/qa-tools
*/
namespace QATools\QATools\HtmlElements\Element;
use QATools\QATools\HtmlElements\Exception\RadioGroupException;
/**
* Represents a group of radio buttons.
*/
class RadioGroup extends AbstractTypifiedElementCollection implements ISimpleSetter
{
/**
* Initializes collection with a list of elements.
*
* @param array|RadioButton[] $elements RadioButton elements.
*/
public function __construct(array $elements = array())
{
if ( !$this->elementClass ) {
$this->elementClass = '\\QATools\\QATools\\HtmlElements\\Element\\RadioButton';
}
parent::__construct($elements);
}
/**
* Determines if an element can be added to a collection.
*
* @param mixed $element Element.
*
* @return boolean
*/
protected function acceptElement($element)
{
return $element->getTagName() == 'input' && strtolower($element->getAttribute('type')) == 'radio';
}
/**
* Indicates if radio has selected button.
*
* @return boolean
*/
public function hasSelectedButton()
{
/** @var $button RadioButton */
foreach ( $this as $button ) {
if ( $button->isSelected() ) {
return true;
}
}
return false;
}
/**
* Returns selected radio button.
*
* @return RadioButton Element, that represents selected radio button or {@code null} if no radio buttons are selected.
* @throws RadioGroupException When no radio button is selected.
*/
public function getSelectedButton()
{
/** @var $button RadioButton */
foreach ( $this as $button ) {
if ( $button->isSelected() ) {
return $button;
}
}
throw new RadioGroupException('No selected button', RadioGroupException::TYPE_NOT_SELECTED);
}
/**
* Selects radio button, that contains given text.
*
* @param string $text Text.
*
* @return static
* @throws RadioGroupException When radio button with given label text wasn't found.
*/
public function selectButtonByLabelText($text)
{
/** @var $button RadioButton */
foreach ( $this as $button ) {
if ( strpos($button->getLabelText(), $text) !== false ) {
$button->select();
return $this;
}
}
throw new RadioGroupException(
'Cannot locate radio button with label text containing: ' . $text,
RadioGroupException::TYPE_NOT_FOUND
);
}
/**
* Selects radio button that have a value matching the specified argument.
*
* @param string $value The value to match against.
*
* @return static
* @throws RadioGroupException When radio button with given value wasn't found.
*/
public function selectButtonByValue($value)
{
/** @var $button RadioButton */
foreach ( $this as $button ) {
if ( (string)$button->getValue() === (string)$value ) {
$button->select();
return $this;
}
}
throw new RadioGroupException(
'Cannot locate radio button with value: ' . $value,
RadioGroupException::TYPE_NOT_FOUND
);
}
/**
* Selects radio button by the given index.
*
* @param integer $index Index of a radio button to be selected.
*
* @return static
* @throws RadioGroupException When non-existing index was given.
*/
public function selectButtonByIndex($index)
{
if ( isset($this[$index]) ) {
/** @var RadioButton $button */
$button = $this[$index];
$button->select();
return $this;
}
throw new RadioGroupException(
'Cannot locate radio button with index: ' . $index,
RadioGroupException::TYPE_NOT_FOUND
);
}
/**
* Sets value to the element.
*
* @param mixed $value New value.
*
* @return static
*/
public function setValue($value)
{
return $this->selectButtonByValue((string)$value);
}
}