mirror of
https://github.com/processwire/processwire.git
synced 2025-08-10 16:54:44 +02:00
Add hasValue(), hasTitle() and hasID() methods to SelectableOptionArray class
This commit is contained in:
@@ -197,4 +197,56 @@ class SelectableOptionArray extends WireArray {
|
||||
return $isIdentical;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get SelectableOption by $property matching $value, or boolean false if not found
|
||||
*
|
||||
* @param string $property May be "title", "value" or "id"
|
||||
* @param string|int $value
|
||||
* @return bool|SelectableOption
|
||||
*
|
||||
*/
|
||||
protected function getOptionByProperty($property, $value) {
|
||||
$match = false;
|
||||
foreach($this as $option) {
|
||||
$v = $option->getProperty($property);
|
||||
if($v !== $value) continue;
|
||||
$match = $option;
|
||||
break;
|
||||
}
|
||||
return $match;
|
||||
}
|
||||
|
||||
/**
|
||||
* Is the given value present in these selectable options?
|
||||
*
|
||||
* @param string $value
|
||||
* @return SelectableOption|bool Returns SelectableOption if found, or boolean false if not
|
||||
*
|
||||
*/
|
||||
public function hasValue($value) {
|
||||
return $this->getOptionByProperty('value', $value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Is the given title present in these selectable options?
|
||||
*
|
||||
* @param string $title
|
||||
* @return SelectableOption|bool Returns SelectableOption if found, or boolean false if not
|
||||
*
|
||||
*/
|
||||
public function hasTitle($title) {
|
||||
return $this->getOptionByProperty('title', $title);
|
||||
}
|
||||
|
||||
/**
|
||||
* Is the given id present in these selectable options?
|
||||
*
|
||||
* @param int $id
|
||||
* @return SelectableOption|bool Returns SelectableOption if found, or boolean false if not
|
||||
*
|
||||
*/
|
||||
public function hasID($id) {
|
||||
return $this->getOptionByProperty('id', (int) $id);
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user