Hoa central
Select.php
Go to the documentation of this file.
1 <?php
2 
37 namespace Hoa\Xyl\Interpreter\Html;
38 
47 class Select extends Generic
48 {
54  protected static $_attributes = [
55  'autofocus' => parent::ATTRIBUTE_TYPE_NORMAL,
56  'disabled' => parent::ATTRIBUTE_TYPE_NORMAL,
57  'form' => parent::ATTRIBUTE_TYPE_NORMAL,
58  'multiple' => parent::ATTRIBUTE_TYPE_NORMAL,
59  'name' => parent::ATTRIBUTE_TYPE_NORMAL,
60  'required' => parent::ATTRIBUTE_TYPE_NORMAL,
61  'size' => parent::ATTRIBUTE_TYPE_NORMAL
62  ];
63 
69  protected static $_attributesMapping = …;
70 
76  protected $_validity = null;
77 
78 
79 
85  public function getOptions()
86  {
87  return $this->xpath('.//__current_ns:option');
88  }
89 
95  public function getSelectedOptions()
96  {
97  return $this->xpath('.//__current_ns:option[@selected]');
98  }
99 
105  public function getForm()
106  {
107  return Form::getMe($this);
108  }
109 
117  public function isValid($revalid = false, $value)
118  {
119  if (false === $revalid && null !== $this->_validity) {
120  return $this->_validity;
121  }
122 
123  $this->_validity = true;
124 
125  if ((null === $value || '' === $value || [] === $value) &&
126  true === $this->attributeExists('required')) {
127  $this->_validity = false;
128 
129  return Form::postValidation($this->_validity, $value, $this);
130  }
131 
132  if (empty($value)) {
133  $this->_validity = true;
134 
135  return Form::postValidation($this->_validity, $value, $this);
136  }
137 
138  if (is_array($value)) {
139  if (false === $this->attributeExists('multiple')) {
140  $this->_validity = false;
141 
142  return Form::postValidation($this->_validity, $value, $this);
143  }
144  } else {
145  $value = [$value];
146  }
147 
148  foreach ($this->getOptions() as $option) {
149  $option = $this->getConcreteElement($option);
150 
151  if (false === $option->attributeExists('value')) {
152  continue;
153  }
154 
155  $handle = $option->readAttribute('value');
156 
157  if (false !== $i = array_search($handle, $value)) {
158  unset($value[$i]);
159  }
160  }
161 
162  $this->_validity = empty($value);
163 
164  return Form::postValidation($this->_validity, $value, $this);
165  }
166 
173  public function setValue($value)
174  {
175  foreach ($this->getOptions() as $option) {
176  $option = $this->getConcreteElement($option);
177 
178  if (false === $option->attributeExists('value')) {
179  continue;
180  }
181 
182  if ($value !== $option->readAttribute('value')) {
183  continue;
184  }
185 
186  $option->writeAttribute('selected', 'selected');
187  }
188 
189  return;
190  }
191 }
static getMe(Concrete $element)
Definition: Form.php:592
isValid($revalid=false, $value)
Definition: Select.php:117
static postValidation($verdict, &$value, Concrete $element, $postVerification=true)
Definition: Form.php:447