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 EmptyIterator;
22
 
23
/**
24
 * @extends Option<mixed>
25
 */
26
final class None extends Option
27
{
28
    /** @var None|null */
29
    private static $instance;
30
 
31
    /**
32
     * @return None
33
     */
34
    public static function create(): self
35
    {
36
        if (null === self::$instance) {
37
            self::$instance = new self();
38
        }
39
 
40
        return self::$instance;
41
    }
42
 
43
    public function get()
44
    {
45
        throw new \RuntimeException('None has no value.');
46
    }
47
 
48
    public function getOrCall($callable)
49
    {
50
        return $callable();
51
    }
52
 
53
    public function getOrElse($default)
54
    {
55
        return $default;
56
    }
57
 
58
    public function getOrThrow(\Exception $ex)
59
    {
60
        throw $ex;
61
    }
62
 
63
    public function isEmpty(): bool
64
    {
65
        return true;
66
    }
67
 
68
    public function isDefined(): bool
69
    {
70
        return false;
71
    }
72
 
73
    public function orElse(Option $else)
74
    {
75
        return $else;
76
    }
77
 
78
    public function ifDefined($callable)
79
    {
80
        // Just do nothing in that case.
81
    }
82
 
83
    public function forAll($callable)
84
    {
85
        return $this;
86
    }
87
 
88
    public function map($callable)
89
    {
90
        return $this;
91
    }
92
 
93
    public function flatMap($callable)
94
    {
95
        return $this;
96
    }
97
 
98
    public function filter($callable)
99
    {
100
        return $this;
101
    }
102
 
103
    public function filterNot($callable)
104
    {
105
        return $this;
106
    }
107
 
108
    public function select($value)
109
    {
110
        return $this;
111
    }
112
 
113
    public function reject($value)
114
    {
115
        return $this;
116
    }
117
 
118
    public function getIterator(): EmptyIterator
119
    {
120
        return new EmptyIterator();
121
    }
122
 
123
    public function foldLeft($initialValue, $callable)
124
    {
125
        return $initialValue;
126
    }
127
 
128
    public function foldRight($initialValue, $callable)
129
    {
130
        return $initialValue;
131
    }
132
 
133
    private function __construct()
134
    {
135
    }
136
}