Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

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