From 0f9eb0aaf5219ad09eebe9b3e797575ea1ed51ad Mon Sep 17 00:00:00 2001 From: Ryan Cramer Date: Fri, 26 Oct 2018 12:21:10 -0400 Subject: [PATCH] Add hasValue(), hasTitle() and hasID() methods to SelectableOptionArray class --- .../SelectableOptionArray.php | 52 +++++++++++++++++++ 1 file changed, 52 insertions(+) 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); + } + }