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 Traversable;
22
 
23
/**
24
 * @template T
25
 *
26
 * @extends Option<T>
27
 */
28
final class LazyOption extends Option
29
{
30
    /** @var callable(mixed...):(Option<T>) */
31
    private $callback;
32
 
33
    /** @var array<int, mixed> */
34
    private $arguments;
35
 
36
    /** @var Option<T>|null */
37
    private $option;
38
 
39
    /**
40
     * @template S
41
     * @param callable(mixed...):(Option<S>) $callback
42
     * @param array<int, mixed>              $arguments
43
     *
44
     * @return LazyOption<S>
45
     */
46
    public static function create($callback, array $arguments = []): self
47
    {
48
        return new self($callback, $arguments);
49
    }
50
 
51
    /**
52
     * @param callable(mixed...):(Option<T>) $callback
53
     * @param array<int, mixed>              $arguments
54
     */
55
    public function __construct($callback, array $arguments = [])
56
    {
57
        if (!is_callable($callback)) {
58
            throw new \InvalidArgumentException('Invalid callback given');
59
        }
60
 
61
        $this->callback = $callback;
62
        $this->arguments = $arguments;
63
    }
64
 
65
    public function isDefined(): bool
66
    {
67
        return $this->option()->isDefined();
68
    }
69
 
70
    public function isEmpty(): bool
71
    {
72
        return $this->option()->isEmpty();
73
    }
74
 
75
    public function get()
76
    {
77
        return $this->option()->get();
78
    }
79
 
80
    public function getOrElse($default)
81
    {
82
        return $this->option()->getOrElse($default);
83
    }
84
 
85
    public function getOrCall($callable)
86
    {
87
        return $this->option()->getOrCall($callable);
88
    }
89
 
90
    public function getOrThrow(\Exception $ex)
91
    {
92
        return $this->option()->getOrThrow($ex);
93
    }
94
 
95
    public function orElse(Option $else)
96
    {
97
        return $this->option()->orElse($else);
98
    }
99
 
100
    public function ifDefined($callable)
101
    {
102
        $this->option()->forAll($callable);
103
    }
104
 
105
    public function forAll($callable)
106
    {
107
        return $this->option()->forAll($callable);
108
    }
109
 
110
    public function map($callable)
111
    {
112
        return $this->option()->map($callable);
113
    }
114
 
115
    public function flatMap($callable)
116
    {
117
        return $this->option()->flatMap($callable);
118
    }
119
 
120
    public function filter($callable)
121
    {
122
        return $this->option()->filter($callable);
123
    }
124
 
125
    public function filterNot($callable)
126
    {
127
        return $this->option()->filterNot($callable);
128
    }
129
 
130
    public function select($value)
131
    {
132
        return $this->option()->select($value);
133
    }
134
 
135
    public function reject($value)
136
    {
137
        return $this->option()->reject($value);
138
    }
139
 
140
    /**
141
     * @return Traversable<T>
142
     */
143
    public function getIterator(): Traversable
144
    {
145
        return $this->option()->getIterator();
146
    }
147
 
148
    public function foldLeft($initialValue, $callable)
149
    {
150
        return $this->option()->foldLeft($initialValue, $callable);
151
    }
152
 
153
    public function foldRight($initialValue, $callable)
154
    {
155
        return $this->option()->foldRight($initialValue, $callable);
156
    }
157
 
158
    /**
159
     * @return Option<T>
160
     */
161
    private function option(): Option
162
    {
163
        if (null === $this->option) {
164
            /** @var mixed */
165
            $option = call_user_func_array($this->callback, $this->arguments);
166
            if ($option instanceof Option) {
167
                $this->option = $option;
168
            } else {
169
                throw new \RuntimeException(sprintf('Expected instance of %s', Option::class));
170
            }
171
        }
172
 
173
        return $this->option;
174
    }
175
}