vendor/phpoption/phpoption/src/PhpOption/Some.php line 100

Open in your IDE?
  1. <?php
  2. /*
  3.  * Copyright 2012 Johannes M. Schmitt <schmittjoh@gmail.com>
  4.  *
  5.  * Licensed under the Apache License, Version 2.0 (the "License");
  6.  * you may not use this file except in compliance with the License.
  7.  * You may obtain a copy of the License at
  8.  *
  9.  * http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. namespace PhpOption;
  18. use ArrayIterator;
  19. /**
  20.  * @template T
  21.  *
  22.  * @extends Option<T>
  23.  */
  24. final class Some extends Option
  25. {
  26.     /** @var T */
  27.     private $value;
  28.     /**
  29.      * @param T $value
  30.      */
  31.     public function __construct($value)
  32.     {
  33.         $this->value $value;
  34.     }
  35.     /**
  36.      * @template U
  37.      *
  38.      * @param U $value
  39.      *
  40.      * @return Some<U>
  41.      */
  42.     public static function create($value)
  43.     {
  44.         return new self($value);
  45.     }
  46.     public function isDefined()
  47.     {
  48.         return true;
  49.     }
  50.     public function isEmpty()
  51.     {
  52.         return false;
  53.     }
  54.     public function get()
  55.     {
  56.         return $this->value;
  57.     }
  58.     public function getOrElse($default)
  59.     {
  60.         return $this->value;
  61.     }
  62.     public function getOrCall($callable)
  63.     {
  64.         return $this->value;
  65.     }
  66.     public function getOrThrow(\Exception $ex)
  67.     {
  68.         return $this->value;
  69.     }
  70.     public function orElse(Option $else)
  71.     {
  72.         return $this;
  73.     }
  74.     public function ifDefined($callable)
  75.     {
  76.         $this->forAll($callable);
  77.     }
  78.     public function forAll($callable)
  79.     {
  80.         $callable($this->value);
  81.         return $this;
  82.     }
  83.     public function map($callable)
  84.     {
  85.         return new self($callable($this->value));
  86.     }
  87.     public function flatMap($callable)
  88.     {
  89.         /** @var mixed */
  90.         $rs $callable($this->value);
  91.         if (!$rs instanceof Option) {
  92.             throw new \RuntimeException('Callables passed to flatMap() must return an Option. Maybe you should use map() instead?');
  93.         }
  94.         return $rs;
  95.     }
  96.     public function filter($callable)
  97.     {
  98.         if (true === $callable($this->value)) {
  99.             return $this;
  100.         }
  101.         return None::create();
  102.     }
  103.     public function filterNot($callable)
  104.     {
  105.         if (false === $callable($this->value)) {
  106.             return $this;
  107.         }
  108.         return None::create();
  109.     }
  110.     public function select($value)
  111.     {
  112.         if ($this->value === $value) {
  113.             return $this;
  114.         }
  115.         return None::create();
  116.     }
  117.     public function reject($value)
  118.     {
  119.         if ($this->value === $value) {
  120.             return None::create();
  121.         }
  122.         return $this;
  123.     }
  124.     public function getIterator()
  125.     {
  126.         return new ArrayIterator([$this->value]);
  127.     }
  128.     public function foldLeft($initialValue$callable)
  129.     {
  130.         return $callable($initialValue$this->value);
  131.     }
  132.     public function foldRight($initialValue$callable)
  133.     {
  134.         return $callable($this->value$initialValue);
  135.     }
  136. }