Option::ensure( PhpOptionOption|Closure|mixed $value, null $noneValue = null )

Option factory, which creates new option based on passed value.


If value is already an option, it simply returns If value is a \Closure, LazyOption with passed callback created and returned. If Option returned from callback, it returns directly (flatMap-like behaviour) On other case value passed to Option::fromValue() method



(PhpOptionOption|Closure|mixed) (Required)


(null) (Optional) used when $value is mixed or Closure, for None-check

Default value: null




File: vendor/phpoption/phpoption/src/PhpOption/Option.php

    public static function ensure($value, $noneValue = null)
        if ($value instanceof Option) {
            return $value;
        } elseif ($value instanceof \Closure) {
            return new LazyOption(function() use ($value, $noneValue) {
                $return = $value();

                if ($return instanceof Option) {
                    return $return;
                } else {
                    return Option::fromValue($return, $noneValue);
        } else {
            return Option::fromValue($value, $noneValue);