isSelectionExists($selection)) { return false; } $this->selections[] = $selection; if (count($this->selections) === 1) { $this->default = $selection->getValue(); } return true; } /** * @param OptionSelection $selection * * @return boolean */ protected function isSelectionExists(OptionSelection $selection) { foreach ($this->selections as $thisSelection) { if ($thisSelection->getValue() === $selection->getValue()) { return true; } } return false; } /** * @param mixed $value * * @return bool */ protected function isValueExists($value) { foreach ($this->selections as $thisSelection) { if ($thisSelection->getValue() === $value) { return true; } } return false; } /** * @param mixed $value * * @return mixed * @throws OptionValueFilterFailed */ protected function filter($value) { if ( ! $this->hasSelections()) { throw new OptionValueFilterFailed(); } if ( ! $this->isValueExists($value)) { throw new OptionValueFilterFailed(); } return $value; } protected function hasSelections() { return count($this->selections) > 0; } /** * @return OptionSelection[] */ public function getSelections(): array { return $this->selections; } }