diff --git a/wire/modules/Fieldtype/FieldtypeOptions/SelectableOptionArray.php b/wire/modules/Fieldtype/FieldtypeOptions/SelectableOptionArray.php index bb869680..96fc8883 100644 --- a/wire/modules/Fieldtype/FieldtypeOptions/SelectableOptionArray.php +++ b/wire/modules/Fieldtype/FieldtypeOptions/SelectableOptionArray.php @@ -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); + } + }