-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBox.php
More file actions
211 lines (166 loc) · 4.69 KB
/
Box.php
File metadata and controls
211 lines (166 loc) · 4.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
<?php
/**
* Box is the base class for Full, Empty, and Failure.
*
* Boxs are used to indicate the presence or absence of a value, with the possibility to indicate the reason for its absence. Return values do not need to be explicitly checked since iterators are available. This is inspired by Lift's Box and Scala's Option. Ideally this would be an abstract class but PHP doesn't let an abstract class implement an interface.
*/
class Box implements Iterator {
protected $value;
private $times = 0;
function __construct($value) {
$this->value = $value;
}
function current() {
return $this->value;
}
function key() {
return 0;
}
function next() {
$this->times++;
return ($this->times < 1);
}
function rewind() {
$this->times = 0;
}
function valid() {
return ($this->times < 1);
}
function isEmpty() {
return !isset($this->value);
}
function isDefined() {
return !$this->isEmpty();
}
function map($f) {
return $this;
}
function flatMap($f) {
return $this;
}
function each($f) {
}
function open() {
throw new Exception('No such element.');
}
function openOr($d) {
return $d;
}
function orElse($d) {
$this->assertBox($d);
return $this;
}
function filter($f) {
$this->assertCallable($f);
return $this;
}
function exists($f) {
$this->assertCallable($f);
return false;
}
function isA($c) {
return $None;
}
function failMsg($msg) {
return $this;
}
function compoundFailMsg($msg) {
return $this->failMsg($msg);
}
protected function assertCallable($f) {
if (!is_callable($f)) {
throw new Exception("$f is not callbable");
}
}
protected function assertBox($b) {
if (!($b instanceof Box)) {
throw new Exception("$b is not a Box");
}
}
}
class Full extends Box {
function open() {
return $this->value;
}
function openOr($d) {
return $this->open();
}
function map($f) {
$this->assertCallable($f);
return new Full($f($this->value));
}
function flatMap($f) {
$this->assertCallable($f);
$tmp = $f($this->value);
$this->assertBox($tmp);
return $tmp;
}
function each($f) {
$this->assertCallable($f);
$f($this->value);
}
function exists($f) {
$this->assertCallable($f);
return $f($this->value);
}
function isA($c) {
global $Empty;
return ($this->value instanceof $c ? $this : $Empty);
}
}
class EmptyBox extends Box {
function __construct() {
}
function next() {
return false;
}
function valid() {
return false;
}
function orElse($d) {
$this->assertBox($d);
return $d;
}
function failMsg($str) {
return new Failure($str);
}
}
$Empty = new EmptyBox();
class Failure extends EmptyBox {
public $message, $exception, $failure;
function __construct($message, Box $exception = NULL, Box $failure = NULL) {
global $Empty;
$this->message = $message;
$this->exception = (!is_null($exception) && $exception->isDefined() && ($exception->open() instanceof Exception) ? $exception : $Empty);
$this->failure = (!is_null($failure) && $failure->isDefined() && ($failure->open() instanceof Failure) ? $failure : $Empty);
}
function open() {
throw new Exception(
"Trying to open a Failure Box: " + $this->message,
// could reuse the previous exception's code but probably best not
// ($this->exception->map(create_function('$v', 'return $v->getCode();'))->openOr(0)),
0,
($this->exception->openOr(NULL))
);
}
function failMsg($msg) {
return $this;
}
function compoundFailMsg($msg) {
global $Empty;
return new Failure($msg, $Empty, new Full($this));
}
function isA($c) {
return $this;
}
function chainList() {
if (!is_null($this->failure) && $this->failure->isDefined()) {
return array_merge(array($this->failure->open()), $this->failure->open()->chainList());
}
return array();
}
function messageChain() {
return implode(' <-', array_map(create_function('$a', 'return $a->message;'), array_merge(array($this), $this->chainList())));
}
}
?>