create-berna-stencil 2.0.9 → 2.0.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (166) hide show
  1. package/bin/create.js +2 -1
  2. package/package.json +1 -1
  3. package/src/backend/_core/vendor/composer/autoload_static.php +10 -10
  4. package/src/backend/_core/vendor/composer/installed.json +6 -6
  5. package/src/backend/_core/vendor/composer/installed.php +2 -2
  6. package/src/backend/_core/vendor/graham-campbell/result-type/.gitattributes +9 -0
  7. package/src/backend/_core/vendor/graham-campbell/result-type/.github/CODE_OF_CONDUCT.md +132 -0
  8. package/src/backend/_core/vendor/graham-campbell/result-type/.github/CONTRIBUTING.md +31 -0
  9. package/src/backend/_core/vendor/graham-campbell/result-type/.github/FUNDING.yml +2 -0
  10. package/src/backend/_core/vendor/graham-campbell/result-type/.github/SECURITY.md +14 -0
  11. package/src/backend/_core/vendor/graham-campbell/result-type/.github/workflows/stale.yml +11 -0
  12. package/src/backend/_core/vendor/graham-campbell/result-type/.github/workflows/tests.yml +40 -0
  13. package/src/backend/_core/vendor/graham-campbell/result-type/CHANGELOG.md +53 -0
  14. package/src/backend/_core/vendor/graham-campbell/result-type/LICENSE +21 -21
  15. package/src/backend/_core/vendor/graham-campbell/result-type/README.md +42 -0
  16. package/src/backend/_core/vendor/graham-campbell/result-type/composer.json +33 -33
  17. package/src/backend/_core/vendor/graham-campbell/result-type/phpunit.xml.dist +13 -0
  18. package/src/backend/_core/vendor/graham-campbell/result-type/src/Error.php +121 -121
  19. package/src/backend/_core/vendor/graham-campbell/result-type/src/Result.php +69 -69
  20. package/src/backend/_core/vendor/graham-campbell/result-type/src/Success.php +120 -120
  21. package/src/backend/_core/vendor/graham-campbell/result-type/tests/ResultTest.php +95 -0
  22. package/src/backend/_core/vendor/phpoption/phpoption/.gitattributes +13 -0
  23. package/src/backend/_core/vendor/phpoption/phpoption/.github/CODE_OF_CONDUCT.md +132 -0
  24. package/src/backend/_core/vendor/phpoption/phpoption/.github/CONTRIBUTING.md +30 -0
  25. package/src/backend/_core/vendor/phpoption/phpoption/.github/FUNDING.yml +2 -0
  26. package/src/backend/_core/vendor/phpoption/phpoption/.github/SECURITY.md +14 -0
  27. package/src/backend/_core/vendor/phpoption/phpoption/.github/workflows/static.yml +40 -0
  28. package/src/backend/_core/vendor/phpoption/phpoption/.github/workflows/tests.yml +40 -0
  29. package/src/backend/_core/vendor/phpoption/phpoption/LICENSE +200 -200
  30. package/src/backend/_core/vendor/phpoption/phpoption/Makefile +17 -0
  31. package/src/backend/_core/vendor/phpoption/phpoption/README.md +201 -0
  32. package/src/backend/_core/vendor/phpoption/phpoption/composer.json +50 -50
  33. package/src/backend/_core/vendor/phpoption/phpoption/phpstan-baseline.neon +44 -0
  34. package/src/backend/_core/vendor/phpoption/phpoption/phpstan.neon.dist +7 -0
  35. package/src/backend/_core/vendor/phpoption/phpoption/phpunit.xml.dist +13 -0
  36. package/src/backend/_core/vendor/phpoption/phpoption/src/PhpOption/LazyOption.php +175 -175
  37. package/src/backend/_core/vendor/phpoption/phpoption/src/PhpOption/None.php +136 -136
  38. package/src/backend/_core/vendor/phpoption/phpoption/src/PhpOption/Option.php +434 -434
  39. package/src/backend/_core/vendor/phpoption/phpoption/src/PhpOption/Some.php +169 -169
  40. package/src/backend/_core/vendor/phpoption/phpoption/tests/PhpOption/Tests/EnsureTest.php +72 -0
  41. package/src/backend/_core/vendor/phpoption/phpoption/tests/PhpOption/Tests/LazyOptionTest.php +357 -0
  42. package/src/backend/_core/vendor/phpoption/phpoption/tests/PhpOption/Tests/NoneTest.php +153 -0
  43. package/src/backend/_core/vendor/phpoption/phpoption/tests/PhpOption/Tests/OptionTest.php +166 -0
  44. package/src/backend/_core/vendor/phpoption/phpoption/tests/PhpOption/Tests/SomeTest.php +194 -0
  45. package/src/backend/_core/vendor/phpoption/phpoption/tests/bootstrap.php +8 -0
  46. package/src/backend/_core/vendor/phpoption/phpoption/vendor-bin/phpstan/composer.json +8 -0
  47. package/src/backend/_core/vendor/symfony/polyfill-ctype/Ctype.php +232 -232
  48. package/src/backend/_core/vendor/symfony/polyfill-ctype/LICENSE +19 -19
  49. package/src/backend/_core/vendor/symfony/polyfill-ctype/README.md +12 -12
  50. package/src/backend/_core/vendor/symfony/polyfill-ctype/bootstrap.php +50 -50
  51. package/src/backend/_core/vendor/symfony/polyfill-ctype/bootstrap80.php +46 -46
  52. package/src/backend/_core/vendor/symfony/polyfill-ctype/composer.json +38 -38
  53. package/src/backend/_core/vendor/symfony/polyfill-mbstring/LICENSE +19 -19
  54. package/src/backend/_core/vendor/symfony/polyfill-mbstring/Mbstring.php +1077 -1077
  55. package/src/backend/_core/vendor/symfony/polyfill-mbstring/README.md +13 -13
  56. package/src/backend/_core/vendor/symfony/polyfill-mbstring/Resources/unidata/caseFolding.php +119 -119
  57. package/src/backend/_core/vendor/symfony/polyfill-mbstring/Resources/unidata/lowerCase.php +1397 -1397
  58. package/src/backend/_core/vendor/symfony/polyfill-mbstring/Resources/unidata/titleCaseRegexp.php +5 -5
  59. package/src/backend/_core/vendor/symfony/polyfill-mbstring/Resources/unidata/upperCase.php +1489 -1489
  60. package/src/backend/_core/vendor/symfony/polyfill-mbstring/bootstrap.php +171 -171
  61. package/src/backend/_core/vendor/symfony/polyfill-mbstring/bootstrap80.php +167 -167
  62. package/src/backend/_core/vendor/symfony/polyfill-mbstring/composer.json +39 -39
  63. package/src/backend/_core/vendor/symfony/polyfill-php80/LICENSE +19 -19
  64. package/src/backend/_core/vendor/symfony/polyfill-php80/Php80.php +115 -115
  65. package/src/backend/_core/vendor/symfony/polyfill-php80/PhpToken.php +106 -106
  66. package/src/backend/_core/vendor/symfony/polyfill-php80/README.md +25 -25
  67. package/src/backend/_core/vendor/symfony/polyfill-php80/Resources/stubs/Attribute.php +31 -31
  68. package/src/backend/_core/vendor/symfony/polyfill-php80/Resources/stubs/PhpToken.php +16 -16
  69. package/src/backend/_core/vendor/symfony/polyfill-php80/Resources/stubs/Stringable.php +20 -20
  70. package/src/backend/_core/vendor/symfony/polyfill-php80/Resources/stubs/UnhandledMatchError.php +16 -16
  71. package/src/backend/_core/vendor/symfony/polyfill-php80/Resources/stubs/ValueError.php +16 -16
  72. package/src/backend/_core/vendor/symfony/polyfill-php80/bootstrap.php +42 -42
  73. package/src/backend/_core/vendor/symfony/polyfill-php80/composer.json +37 -37
  74. package/src/backend/_core/vendor/vlucas/phpdotenv/.editorconfig +15 -0
  75. package/src/backend/_core/vendor/vlucas/phpdotenv/.gitattributes +15 -0
  76. package/src/backend/_core/vendor/vlucas/phpdotenv/.github/CODE_OF_CONDUCT.md +132 -0
  77. package/src/backend/_core/vendor/vlucas/phpdotenv/.github/CONTRIBUTING.md +30 -0
  78. package/src/backend/_core/vendor/vlucas/phpdotenv/.github/FUNDING.yml +2 -0
  79. package/src/backend/_core/vendor/vlucas/phpdotenv/.github/SECURITY.md +14 -0
  80. package/src/backend/_core/vendor/vlucas/phpdotenv/.github/workflows/static.yml +40 -0
  81. package/src/backend/_core/vendor/vlucas/phpdotenv/.github/workflows/tests.yml +70 -0
  82. package/src/backend/_core/vendor/vlucas/phpdotenv/LICENSE +30 -30
  83. package/src/backend/_core/vendor/vlucas/phpdotenv/Makefile +17 -0
  84. package/src/backend/_core/vendor/vlucas/phpdotenv/README.md +370 -0
  85. package/src/backend/_core/vendor/vlucas/phpdotenv/UPGRADING.md +196 -0
  86. package/src/backend/_core/vendor/vlucas/phpdotenv/composer.json +60 -60
  87. package/src/backend/_core/vendor/vlucas/phpdotenv/phpstan-baseline.neon +157 -0
  88. package/src/backend/_core/vendor/vlucas/phpdotenv/phpstan.neon.dist +7 -0
  89. package/src/backend/_core/vendor/vlucas/phpdotenv/phpunit.xml.dist +13 -0
  90. package/src/backend/_core/vendor/vlucas/phpdotenv/src/Dotenv.php +267 -267
  91. package/src/backend/_core/vendor/vlucas/phpdotenv/src/Exception/ExceptionInterface.php +12 -12
  92. package/src/backend/_core/vendor/vlucas/phpdotenv/src/Exception/InvalidEncodingException.php +12 -12
  93. package/src/backend/_core/vendor/vlucas/phpdotenv/src/Exception/InvalidFileException.php +12 -12
  94. package/src/backend/_core/vendor/vlucas/phpdotenv/src/Exception/InvalidPathException.php +12 -12
  95. package/src/backend/_core/vendor/vlucas/phpdotenv/src/Exception/ValidationException.php +12 -12
  96. package/src/backend/_core/vendor/vlucas/phpdotenv/src/Loader/Loader.php +48 -48
  97. package/src/backend/_core/vendor/vlucas/phpdotenv/src/Loader/LoaderInterface.php +20 -20
  98. package/src/backend/_core/vendor/vlucas/phpdotenv/src/Loader/Resolver.php +65 -65
  99. package/src/backend/_core/vendor/vlucas/phpdotenv/src/Parser/Entry.php +59 -59
  100. package/src/backend/_core/vendor/vlucas/phpdotenv/src/Parser/EntryParser.php +299 -299
  101. package/src/backend/_core/vendor/vlucas/phpdotenv/src/Parser/Lexer.php +58 -58
  102. package/src/backend/_core/vendor/vlucas/phpdotenv/src/Parser/Lines.php +127 -127
  103. package/src/backend/_core/vendor/vlucas/phpdotenv/src/Parser/Parser.php +53 -53
  104. package/src/backend/_core/vendor/vlucas/phpdotenv/src/Parser/ParserInterface.php +19 -19
  105. package/src/backend/_core/vendor/vlucas/phpdotenv/src/Parser/Value.php +88 -88
  106. package/src/backend/_core/vendor/vlucas/phpdotenv/src/Repository/Adapter/AdapterInterface.php +15 -15
  107. package/src/backend/_core/vendor/vlucas/phpdotenv/src/Repository/Adapter/ApacheAdapter.php +89 -89
  108. package/src/backend/_core/vendor/vlucas/phpdotenv/src/Repository/Adapter/ArrayAdapter.php +80 -80
  109. package/src/backend/_core/vendor/vlucas/phpdotenv/src/Repository/Adapter/EnvConstAdapter.php +88 -88
  110. package/src/backend/_core/vendor/vlucas/phpdotenv/src/Repository/Adapter/GuardedWriter.php +85 -85
  111. package/src/backend/_core/vendor/vlucas/phpdotenv/src/Repository/Adapter/ImmutableWriter.php +110 -110
  112. package/src/backend/_core/vendor/vlucas/phpdotenv/src/Repository/Adapter/MultiReader.php +48 -48
  113. package/src/backend/_core/vendor/vlucas/phpdotenv/src/Repository/Adapter/MultiWriter.php +64 -64
  114. package/src/backend/_core/vendor/vlucas/phpdotenv/src/Repository/Adapter/PutenvAdapter.php +91 -91
  115. package/src/backend/_core/vendor/vlucas/phpdotenv/src/Repository/Adapter/ReaderInterface.php +17 -17
  116. package/src/backend/_core/vendor/vlucas/phpdotenv/src/Repository/Adapter/ReplacingWriter.php +104 -104
  117. package/src/backend/_core/vendor/vlucas/phpdotenv/src/Repository/Adapter/ServerConstAdapter.php +88 -88
  118. package/src/backend/_core/vendor/vlucas/phpdotenv/src/Repository/Adapter/WriterInterface.php +27 -27
  119. package/src/backend/_core/vendor/vlucas/phpdotenv/src/Repository/AdapterRepository.php +107 -107
  120. package/src/backend/_core/vendor/vlucas/phpdotenv/src/Repository/RepositoryBuilder.php +272 -272
  121. package/src/backend/_core/vendor/vlucas/phpdotenv/src/Repository/RepositoryInterface.php +51 -51
  122. package/src/backend/_core/vendor/vlucas/phpdotenv/src/Store/File/Paths.php +44 -44
  123. package/src/backend/_core/vendor/vlucas/phpdotenv/src/Store/File/Reader.php +81 -81
  124. package/src/backend/_core/vendor/vlucas/phpdotenv/src/Store/FileStore.php +72 -72
  125. package/src/backend/_core/vendor/vlucas/phpdotenv/src/Store/StoreBuilder.php +141 -141
  126. package/src/backend/_core/vendor/vlucas/phpdotenv/src/Store/StoreInterface.php +17 -17
  127. package/src/backend/_core/vendor/vlucas/phpdotenv/src/Store/StringStore.php +37 -37
  128. package/src/backend/_core/vendor/vlucas/phpdotenv/src/Util/Regex.php +112 -112
  129. package/src/backend/_core/vendor/vlucas/phpdotenv/src/Util/Str.php +108 -108
  130. package/src/backend/_core/vendor/vlucas/phpdotenv/src/Validator.php +207 -207
  131. package/src/backend/_core/vendor/vlucas/phpdotenv/tests/Dotenv/DotenvTest.php +387 -0
  132. package/src/backend/_core/vendor/vlucas/phpdotenv/tests/Dotenv/Loader/LoaderTest.php +86 -0
  133. package/src/backend/_core/vendor/vlucas/phpdotenv/tests/Dotenv/Parser/EntryParserTest.php +234 -0
  134. package/src/backend/_core/vendor/vlucas/phpdotenv/tests/Dotenv/Parser/LexerTest.php +40 -0
  135. package/src/backend/_core/vendor/vlucas/phpdotenv/tests/Dotenv/Parser/LinesTest.php +53 -0
  136. package/src/backend/_core/vendor/vlucas/phpdotenv/tests/Dotenv/Parser/ParserTest.php +98 -0
  137. package/src/backend/_core/vendor/vlucas/phpdotenv/tests/Dotenv/Repository/Adapter/ArrayAdapterTest.php +57 -0
  138. package/src/backend/_core/vendor/vlucas/phpdotenv/tests/Dotenv/Repository/Adapter/EnvConstAdapterTest.php +75 -0
  139. package/src/backend/_core/vendor/vlucas/phpdotenv/tests/Dotenv/Repository/Adapter/PutenvAdapterTest.php +52 -0
  140. package/src/backend/_core/vendor/vlucas/phpdotenv/tests/Dotenv/Repository/Adapter/ServerConstAdapterTest.php +75 -0
  141. package/src/backend/_core/vendor/vlucas/phpdotenv/tests/Dotenv/Repository/RepositoryTest.php +305 -0
  142. package/src/backend/_core/vendor/vlucas/phpdotenv/tests/Dotenv/Store/StoreTest.php +141 -0
  143. package/src/backend/_core/vendor/vlucas/phpdotenv/tests/Dotenv/ValidatorTest.php +479 -0
  144. package/src/backend/_core/vendor/vlucas/phpdotenv/tests/fixtures/env/.env +5 -0
  145. package/src/backend/_core/vendor/vlucas/phpdotenv/tests/fixtures/env/assertions.env +18 -0
  146. package/src/backend/_core/vendor/vlucas/phpdotenv/tests/fixtures/env/booleans.env +33 -0
  147. package/src/backend/_core/vendor/vlucas/phpdotenv/tests/fixtures/env/commented.env +15 -0
  148. package/src/backend/_core/vendor/vlucas/phpdotenv/tests/fixtures/env/empty.env +1 -0
  149. package/src/backend/_core/vendor/vlucas/phpdotenv/tests/fixtures/env/example.env +1 -0
  150. package/src/backend/_core/vendor/vlucas/phpdotenv/tests/fixtures/env/exported.env +7 -0
  151. package/src/backend/_core/vendor/vlucas/phpdotenv/tests/fixtures/env/immutable.env +1 -0
  152. package/src/backend/_core/vendor/vlucas/phpdotenv/tests/fixtures/env/integers.env +17 -0
  153. package/src/backend/_core/vendor/vlucas/phpdotenv/tests/fixtures/env/large.env +2 -0
  154. package/src/backend/_core/vendor/vlucas/phpdotenv/tests/fixtures/env/multibyte.env +3 -0
  155. package/src/backend/_core/vendor/vlucas/phpdotenv/tests/fixtures/env/multiline.env +14 -0
  156. package/src/backend/_core/vendor/vlucas/phpdotenv/tests/fixtures/env/multiple.env +4 -0
  157. package/src/backend/_core/vendor/vlucas/phpdotenv/tests/fixtures/env/mutable.env +1 -0
  158. package/src/backend/_core/vendor/vlucas/phpdotenv/tests/fixtures/env/nested.env +15 -0
  159. package/src/backend/_core/vendor/vlucas/phpdotenv/tests/fixtures/env/quoted.env +11 -0
  160. package/src/backend/_core/vendor/vlucas/phpdotenv/tests/fixtures/env/specialchars.env +8 -0
  161. package/src/backend/_core/vendor/vlucas/phpdotenv/tests/fixtures/env/unicodevarnames.env +2 -0
  162. package/src/backend/_core/vendor/vlucas/phpdotenv/tests/fixtures/env/utf8-with-bom-encoding.env +3 -0
  163. package/src/backend/_core/vendor/vlucas/phpdotenv/tests/fixtures/env/windows.env +1 -0
  164. package/src/backend/_core/vendor/vlucas/phpdotenv/vendor-bin/phpstan/composer.json +15 -0
  165. package/src/backend/config.php +19 -0
  166. package/src/frontend/data/site.json +43 -43
@@ -1,434 +1,434 @@
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 ArrayAccess;
22
- use IteratorAggregate;
23
-
24
- /**
25
- * @template T
26
- *
27
- * @implements IteratorAggregate<T>
28
- */
29
- abstract class Option implements IteratorAggregate
30
- {
31
- /**
32
- * Creates an option given a return value.
33
- *
34
- * This is intended for consuming existing APIs and allows you to easily
35
- * convert them to an option. By default, we treat ``null`` as the None
36
- * case, and everything else as Some.
37
- *
38
- * @template S
39
- *
40
- * @param S $value The actual return value.
41
- * @param S $noneValue The value which should be considered "None"; null by
42
- * default.
43
- *
44
- * @return Option<S>
45
- */
46
- public static function fromValue($value, $noneValue = null)
47
- {
48
- if ($value === $noneValue) {
49
- return None::create();
50
- }
51
-
52
- return new Some($value);
53
- }
54
-
55
- /**
56
- * Creates an option from an array's value.
57
- *
58
- * If the key does not exist in the array, the array is not actually an
59
- * array, or the array's value at the given key is null, None is returned.
60
- * Otherwise, Some is returned wrapping the value at the given key.
61
- *
62
- * @template S
63
- *
64
- * @param array<string|int,S>|ArrayAccess<string|int,S>|null $array A potential array or \ArrayAccess value.
65
- * @param string|int|null $key The key to check.
66
- *
67
- * @return Option<S>
68
- */
69
- public static function fromArraysValue($array, $key)
70
- {
71
- if ($key === null || !(is_array($array) || $array instanceof ArrayAccess) || !isset($array[$key])) {
72
- return None::create();
73
- }
74
-
75
- return new Some($array[$key]);
76
- }
77
-
78
- /**
79
- * Creates a lazy-option with the given callback.
80
- *
81
- * This is also a helper constructor for lazy-consuming existing APIs where
82
- * the return value is not yet an option. By default, we treat ``null`` as
83
- * None case, and everything else as Some.
84
- *
85
- * @template S
86
- *
87
- * @param callable $callback The callback to evaluate.
88
- * @param array $arguments The arguments for the callback.
89
- * @param S $noneValue The value which should be considered "None";
90
- * null by default.
91
- *
92
- * @return LazyOption<S>
93
- */
94
- public static function fromReturn($callback, array $arguments = [], $noneValue = null)
95
- {
96
- return new LazyOption(static function () use ($callback, $arguments, $noneValue) {
97
- /** @var mixed */
98
- $return = call_user_func_array($callback, $arguments);
99
-
100
- if ($return === $noneValue) {
101
- return None::create();
102
- }
103
-
104
- return new Some($return);
105
- });
106
- }
107
-
108
- /**
109
- * Option factory, which creates new option based on passed value.
110
- *
111
- * If value is already an option, it simply returns. If value is callable,
112
- * LazyOption with passed callback created and returned. If Option
113
- * returned from callback, it returns directly. On other case value passed
114
- * to Option::fromValue() method.
115
- *
116
- * @template S
117
- *
118
- * @param Option<S>|callable|S $value
119
- * @param S $noneValue Used when $value is mixed or
120
- * callable, for None-check.
121
- *
122
- * @return Option<S>|LazyOption<S>
123
- */
124
- public static function ensure($value, $noneValue = null)
125
- {
126
- if ($value instanceof self) {
127
- return $value;
128
- } elseif (is_callable($value)) {
129
- return new LazyOption(static function () use ($value, $noneValue) {
130
- /** @var mixed */
131
- $return = $value();
132
-
133
- if ($return instanceof self) {
134
- return $return;
135
- } else {
136
- return self::fromValue($return, $noneValue);
137
- }
138
- });
139
- } else {
140
- return self::fromValue($value, $noneValue);
141
- }
142
- }
143
-
144
- /**
145
- * Lift a function so that it accepts Option as parameters.
146
- *
147
- * We return a new closure that wraps the original callback. If any of the
148
- * parameters passed to the lifted function is empty, the function will
149
- * return a value of None. Otherwise, we will pass all parameters to the
150
- * original callback and return the value inside a new Option, unless an
151
- * Option is returned from the function, in which case, we use that.
152
- *
153
- * @template S
154
- *
155
- * @param callable $callback
156
- * @param mixed $noneValue
157
- *
158
- * @return callable
159
- */
160
- public static function lift($callback, $noneValue = null)
161
- {
162
- return static function () use ($callback, $noneValue) {
163
- /** @var array<int, mixed> */
164
- $args = func_get_args();
165
-
166
- $reduced_args = array_reduce(
167
- $args,
168
- /** @param bool $status */
169
- static function ($status, self $o) {
170
- return $o->isEmpty() ? true : $status;
171
- },
172
- false
173
- );
174
- // if at least one parameter is empty, return None
175
- if ($reduced_args) {
176
- return None::create();
177
- }
178
-
179
- $args = array_map(
180
- /** @return T */
181
- static function (self $o) {
182
- // it is safe to do so because the fold above checked
183
- // that all arguments are of type Some
184
- /** @var T */
185
- return $o->get();
186
- },
187
- $args
188
- );
189
-
190
- return self::ensure(call_user_func_array($callback, $args), $noneValue);
191
- };
192
- }
193
-
194
- /**
195
- * Returns the value if available, or throws an exception otherwise.
196
- *
197
- * @throws \RuntimeException If value is not available.
198
- *
199
- * @return T
200
- */
201
- abstract public function get();
202
-
203
- /**
204
- * Returns the value if available, or the default value if not.
205
- *
206
- * @template S
207
- *
208
- * @param S $default
209
- *
210
- * @return T|S
211
- */
212
- abstract public function getOrElse($default);
213
-
214
- /**
215
- * Returns the value if available, or the results of the callable.
216
- *
217
- * This is preferable over ``getOrElse`` if the computation of the default
218
- * value is expensive.
219
- *
220
- * @template S
221
- *
222
- * @param callable():S $callable
223
- *
224
- * @return T|S
225
- */
226
- abstract public function getOrCall($callable);
227
-
228
- /**
229
- * Returns the value if available, or throws the passed exception.
230
- *
231
- * @param \Exception $ex
232
- *
233
- * @return T
234
- */
235
- abstract public function getOrThrow(\Exception $ex);
236
-
237
- /**
238
- * Returns true if no value is available, false otherwise.
239
- *
240
- * @return bool
241
- */
242
- abstract public function isEmpty();
243
-
244
- /**
245
- * Returns true if a value is available, false otherwise.
246
- *
247
- * @return bool
248
- */
249
- abstract public function isDefined();
250
-
251
- /**
252
- * Returns this option if non-empty, or the passed option otherwise.
253
- *
254
- * This can be used to try multiple alternatives, and is especially useful
255
- * with lazy evaluating options:
256
- *
257
- * ```php
258
- * $repo->findSomething()
259
- * ->orElse(new LazyOption(array($repo, 'findSomethingElse')))
260
- * ->orElse(new LazyOption(array($repo, 'createSomething')));
261
- * ```
262
- *
263
- * @param Option<T> $else
264
- *
265
- * @return Option<T>
266
- */
267
- abstract public function orElse(self $else);
268
-
269
- /**
270
- * This is similar to map() below except that the return value has no meaning;
271
- * the passed callable is simply executed if the option is non-empty, and
272
- * ignored if the option is empty.
273
- *
274
- * In all cases, the return value of the callable is discarded.
275
- *
276
- * ```php
277
- * $comment->getMaybeFile()->ifDefined(function($file) {
278
- * // Do something with $file here.
279
- * });
280
- * ```
281
- *
282
- * If you're looking for something like ``ifEmpty``, you can use ``getOrCall``
283
- * and ``getOrElse`` in these cases.
284
- *
285
- * @deprecated Use forAll() instead.
286
- *
287
- * @param callable(T):mixed $callable
288
- *
289
- * @return void
290
- */
291
- abstract public function ifDefined($callable);
292
-
293
- /**
294
- * This is similar to map() except that the return value of the callable has no meaning.
295
- *
296
- * The passed callable is simply executed if the option is non-empty, and ignored if the
297
- * option is empty. This method is preferred for callables with side-effects, while map()
298
- * is intended for callables without side-effects.
299
- *
300
- * @param callable(T):mixed $callable
301
- *
302
- * @return Option<T>
303
- */
304
- abstract public function forAll($callable);
305
-
306
- /**
307
- * Applies the callable to the value of the option if it is non-empty,
308
- * and returns the return value of the callable wrapped in Some().
309
- *
310
- * If the option is empty, then the callable is not applied.
311
- *
312
- * ```php
313
- * (new Some("foo"))->map('strtoupper')->get(); // "FOO"
314
- * ```
315
- *
316
- * @template S
317
- *
318
- * @param callable(T):S $callable
319
- *
320
- * @return Option<S>
321
- */
322
- abstract public function map($callable);
323
-
324
- /**
325
- * Applies the callable to the value of the option if it is non-empty, and
326
- * returns the return value of the callable directly.
327
- *
328
- * In contrast to ``map``, the return value of the callable is expected to
329
- * be an Option itself; it is not automatically wrapped in Some().
330
- *
331
- * @template S
332
- *
333
- * @param callable(T):Option<S> $callable must return an Option
334
- *
335
- * @return Option<S>
336
- */
337
- abstract public function flatMap($callable);
338
-
339
- /**
340
- * If the option is empty, it is returned immediately without applying the callable.
341
- *
342
- * If the option is non-empty, the callable is applied, and if it returns true,
343
- * the option itself is returned; otherwise, None is returned.
344
- *
345
- * @param callable(T):bool $callable
346
- *
347
- * @return Option<T>
348
- */
349
- abstract public function filter($callable);
350
-
351
- /**
352
- * If the option is empty, it is returned immediately without applying the callable.
353
- *
354
- * If the option is non-empty, the callable is applied, and if it returns false,
355
- * the option itself is returned; otherwise, None is returned.
356
- *
357
- * @param callable(T):bool $callable
358
- *
359
- * @return Option<T>
360
- */
361
- abstract public function filterNot($callable);
362
-
363
- /**
364
- * If the option is empty, it is returned immediately.
365
- *
366
- * If the option is non-empty, and its value does not equal the passed value
367
- * (via a shallow comparison ===), then None is returned. Otherwise, the
368
- * Option is returned.
369
- *
370
- * In other words, this will filter all but the passed value.
371
- *
372
- * @param T $value
373
- *
374
- * @return Option<T>
375
- */
376
- abstract public function select($value);
377
-
378
- /**
379
- * If the option is empty, it is returned immediately.
380
- *
381
- * If the option is non-empty, and its value does equal the passed value (via
382
- * a shallow comparison ===), then None is returned; otherwise, the Option is
383
- * returned.
384
- *
385
- * In other words, this will let all values through except the passed value.
386
- *
387
- * @param T $value
388
- *
389
- * @return Option<T>
390
- */
391
- abstract public function reject($value);
392
-
393
- /**
394
- * Binary operator for the initial value and the option's value.
395
- *
396
- * If empty, the initial value is returned. If non-empty, the callable
397
- * receives the initial value and the option's value as arguments.
398
- *
399
- * ```php
400
- *
401
- * $some = new Some(5);
402
- * $none = None::create();
403
- * $result = $some->foldLeft(1, function($a, $b) { return $a + $b; }); // int(6)
404
- * $result = $none->foldLeft(1, function($a, $b) { return $a + $b; }); // int(1)
405
- *
406
- * // This can be used instead of something like the following:
407
- * $option = Option::fromValue($integerOrNull);
408
- * $result = 1;
409
- * if ( ! $option->isEmpty()) {
410
- * $result += $option->get();
411
- * }
412
- * ```
413
- *
414
- * @template S
415
- *
416
- * @param S $initialValue
417
- * @param callable(S, T):S $callable
418
- *
419
- * @return S
420
- */
421
- abstract public function foldLeft($initialValue, $callable);
422
-
423
- /**
424
- * foldLeft() but with reversed arguments for the callable.
425
- *
426
- * @template S
427
- *
428
- * @param S $initialValue
429
- * @param callable(T, S):S $callable
430
- *
431
- * @return S
432
- */
433
- abstract public function foldRight($initialValue, $callable);
434
- }
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 ArrayAccess;
22
+ use IteratorAggregate;
23
+
24
+ /**
25
+ * @template T
26
+ *
27
+ * @implements IteratorAggregate<T>
28
+ */
29
+ abstract class Option implements IteratorAggregate
30
+ {
31
+ /**
32
+ * Creates an option given a return value.
33
+ *
34
+ * This is intended for consuming existing APIs and allows you to easily
35
+ * convert them to an option. By default, we treat ``null`` as the None
36
+ * case, and everything else as Some.
37
+ *
38
+ * @template S
39
+ *
40
+ * @param S $value The actual return value.
41
+ * @param S $noneValue The value which should be considered "None"; null by
42
+ * default.
43
+ *
44
+ * @return Option<S>
45
+ */
46
+ public static function fromValue($value, $noneValue = null)
47
+ {
48
+ if ($value === $noneValue) {
49
+ return None::create();
50
+ }
51
+
52
+ return new Some($value);
53
+ }
54
+
55
+ /**
56
+ * Creates an option from an array's value.
57
+ *
58
+ * If the key does not exist in the array, the array is not actually an
59
+ * array, or the array's value at the given key is null, None is returned.
60
+ * Otherwise, Some is returned wrapping the value at the given key.
61
+ *
62
+ * @template S
63
+ *
64
+ * @param array<string|int,S>|ArrayAccess<string|int,S>|null $array A potential array or \ArrayAccess value.
65
+ * @param string|int|null $key The key to check.
66
+ *
67
+ * @return Option<S>
68
+ */
69
+ public static function fromArraysValue($array, $key)
70
+ {
71
+ if ($key === null || !(is_array($array) || $array instanceof ArrayAccess) || !isset($array[$key])) {
72
+ return None::create();
73
+ }
74
+
75
+ return new Some($array[$key]);
76
+ }
77
+
78
+ /**
79
+ * Creates a lazy-option with the given callback.
80
+ *
81
+ * This is also a helper constructor for lazy-consuming existing APIs where
82
+ * the return value is not yet an option. By default, we treat ``null`` as
83
+ * None case, and everything else as Some.
84
+ *
85
+ * @template S
86
+ *
87
+ * @param callable $callback The callback to evaluate.
88
+ * @param array $arguments The arguments for the callback.
89
+ * @param S $noneValue The value which should be considered "None";
90
+ * null by default.
91
+ *
92
+ * @return LazyOption<S>
93
+ */
94
+ public static function fromReturn($callback, array $arguments = [], $noneValue = null)
95
+ {
96
+ return new LazyOption(static function () use ($callback, $arguments, $noneValue) {
97
+ /** @var mixed */
98
+ $return = call_user_func_array($callback, $arguments);
99
+
100
+ if ($return === $noneValue) {
101
+ return None::create();
102
+ }
103
+
104
+ return new Some($return);
105
+ });
106
+ }
107
+
108
+ /**
109
+ * Option factory, which creates new option based on passed value.
110
+ *
111
+ * If value is already an option, it simply returns. If value is callable,
112
+ * LazyOption with passed callback created and returned. If Option
113
+ * returned from callback, it returns directly. On other case value passed
114
+ * to Option::fromValue() method.
115
+ *
116
+ * @template S
117
+ *
118
+ * @param Option<S>|callable|S $value
119
+ * @param S $noneValue Used when $value is mixed or
120
+ * callable, for None-check.
121
+ *
122
+ * @return Option<S>|LazyOption<S>
123
+ */
124
+ public static function ensure($value, $noneValue = null)
125
+ {
126
+ if ($value instanceof self) {
127
+ return $value;
128
+ } elseif (is_callable($value)) {
129
+ return new LazyOption(static function () use ($value, $noneValue) {
130
+ /** @var mixed */
131
+ $return = $value();
132
+
133
+ if ($return instanceof self) {
134
+ return $return;
135
+ } else {
136
+ return self::fromValue($return, $noneValue);
137
+ }
138
+ });
139
+ } else {
140
+ return self::fromValue($value, $noneValue);
141
+ }
142
+ }
143
+
144
+ /**
145
+ * Lift a function so that it accepts Option as parameters.
146
+ *
147
+ * We return a new closure that wraps the original callback. If any of the
148
+ * parameters passed to the lifted function is empty, the function will
149
+ * return a value of None. Otherwise, we will pass all parameters to the
150
+ * original callback and return the value inside a new Option, unless an
151
+ * Option is returned from the function, in which case, we use that.
152
+ *
153
+ * @template S
154
+ *
155
+ * @param callable $callback
156
+ * @param mixed $noneValue
157
+ *
158
+ * @return callable
159
+ */
160
+ public static function lift($callback, $noneValue = null)
161
+ {
162
+ return static function () use ($callback, $noneValue) {
163
+ /** @var array<int, mixed> */
164
+ $args = func_get_args();
165
+
166
+ $reduced_args = array_reduce(
167
+ $args,
168
+ /** @param bool $status */
169
+ static function ($status, self $o) {
170
+ return $o->isEmpty() ? true : $status;
171
+ },
172
+ false
173
+ );
174
+ // if at least one parameter is empty, return None
175
+ if ($reduced_args) {
176
+ return None::create();
177
+ }
178
+
179
+ $args = array_map(
180
+ /** @return T */
181
+ static function (self $o) {
182
+ // it is safe to do so because the fold above checked
183
+ // that all arguments are of type Some
184
+ /** @var T */
185
+ return $o->get();
186
+ },
187
+ $args
188
+ );
189
+
190
+ return self::ensure(call_user_func_array($callback, $args), $noneValue);
191
+ };
192
+ }
193
+
194
+ /**
195
+ * Returns the value if available, or throws an exception otherwise.
196
+ *
197
+ * @throws \RuntimeException If value is not available.
198
+ *
199
+ * @return T
200
+ */
201
+ abstract public function get();
202
+
203
+ /**
204
+ * Returns the value if available, or the default value if not.
205
+ *
206
+ * @template S
207
+ *
208
+ * @param S $default
209
+ *
210
+ * @return T|S
211
+ */
212
+ abstract public function getOrElse($default);
213
+
214
+ /**
215
+ * Returns the value if available, or the results of the callable.
216
+ *
217
+ * This is preferable over ``getOrElse`` if the computation of the default
218
+ * value is expensive.
219
+ *
220
+ * @template S
221
+ *
222
+ * @param callable():S $callable
223
+ *
224
+ * @return T|S
225
+ */
226
+ abstract public function getOrCall($callable);
227
+
228
+ /**
229
+ * Returns the value if available, or throws the passed exception.
230
+ *
231
+ * @param \Exception $ex
232
+ *
233
+ * @return T
234
+ */
235
+ abstract public function getOrThrow(\Exception $ex);
236
+
237
+ /**
238
+ * Returns true if no value is available, false otherwise.
239
+ *
240
+ * @return bool
241
+ */
242
+ abstract public function isEmpty();
243
+
244
+ /**
245
+ * Returns true if a value is available, false otherwise.
246
+ *
247
+ * @return bool
248
+ */
249
+ abstract public function isDefined();
250
+
251
+ /**
252
+ * Returns this option if non-empty, or the passed option otherwise.
253
+ *
254
+ * This can be used to try multiple alternatives, and is especially useful
255
+ * with lazy evaluating options:
256
+ *
257
+ * ```php
258
+ * $repo->findSomething()
259
+ * ->orElse(new LazyOption(array($repo, 'findSomethingElse')))
260
+ * ->orElse(new LazyOption(array($repo, 'createSomething')));
261
+ * ```
262
+ *
263
+ * @param Option<T> $else
264
+ *
265
+ * @return Option<T>
266
+ */
267
+ abstract public function orElse(self $else);
268
+
269
+ /**
270
+ * This is similar to map() below except that the return value has no meaning;
271
+ * the passed callable is simply executed if the option is non-empty, and
272
+ * ignored if the option is empty.
273
+ *
274
+ * In all cases, the return value of the callable is discarded.
275
+ *
276
+ * ```php
277
+ * $comment->getMaybeFile()->ifDefined(function($file) {
278
+ * // Do something with $file here.
279
+ * });
280
+ * ```
281
+ *
282
+ * If you're looking for something like ``ifEmpty``, you can use ``getOrCall``
283
+ * and ``getOrElse`` in these cases.
284
+ *
285
+ * @deprecated Use forAll() instead.
286
+ *
287
+ * @param callable(T):mixed $callable
288
+ *
289
+ * @return void
290
+ */
291
+ abstract public function ifDefined($callable);
292
+
293
+ /**
294
+ * This is similar to map() except that the return value of the callable has no meaning.
295
+ *
296
+ * The passed callable is simply executed if the option is non-empty, and ignored if the
297
+ * option is empty. This method is preferred for callables with side-effects, while map()
298
+ * is intended for callables without side-effects.
299
+ *
300
+ * @param callable(T):mixed $callable
301
+ *
302
+ * @return Option<T>
303
+ */
304
+ abstract public function forAll($callable);
305
+
306
+ /**
307
+ * Applies the callable to the value of the option if it is non-empty,
308
+ * and returns the return value of the callable wrapped in Some().
309
+ *
310
+ * If the option is empty, then the callable is not applied.
311
+ *
312
+ * ```php
313
+ * (new Some("foo"))->map('strtoupper')->get(); // "FOO"
314
+ * ```
315
+ *
316
+ * @template S
317
+ *
318
+ * @param callable(T):S $callable
319
+ *
320
+ * @return Option<S>
321
+ */
322
+ abstract public function map($callable);
323
+
324
+ /**
325
+ * Applies the callable to the value of the option if it is non-empty, and
326
+ * returns the return value of the callable directly.
327
+ *
328
+ * In contrast to ``map``, the return value of the callable is expected to
329
+ * be an Option itself; it is not automatically wrapped in Some().
330
+ *
331
+ * @template S
332
+ *
333
+ * @param callable(T):Option<S> $callable must return an Option
334
+ *
335
+ * @return Option<S>
336
+ */
337
+ abstract public function flatMap($callable);
338
+
339
+ /**
340
+ * If the option is empty, it is returned immediately without applying the callable.
341
+ *
342
+ * If the option is non-empty, the callable is applied, and if it returns true,
343
+ * the option itself is returned; otherwise, None is returned.
344
+ *
345
+ * @param callable(T):bool $callable
346
+ *
347
+ * @return Option<T>
348
+ */
349
+ abstract public function filter($callable);
350
+
351
+ /**
352
+ * If the option is empty, it is returned immediately without applying the callable.
353
+ *
354
+ * If the option is non-empty, the callable is applied, and if it returns false,
355
+ * the option itself is returned; otherwise, None is returned.
356
+ *
357
+ * @param callable(T):bool $callable
358
+ *
359
+ * @return Option<T>
360
+ */
361
+ abstract public function filterNot($callable);
362
+
363
+ /**
364
+ * If the option is empty, it is returned immediately.
365
+ *
366
+ * If the option is non-empty, and its value does not equal the passed value
367
+ * (via a shallow comparison ===), then None is returned. Otherwise, the
368
+ * Option is returned.
369
+ *
370
+ * In other words, this will filter all but the passed value.
371
+ *
372
+ * @param T $value
373
+ *
374
+ * @return Option<T>
375
+ */
376
+ abstract public function select($value);
377
+
378
+ /**
379
+ * If the option is empty, it is returned immediately.
380
+ *
381
+ * If the option is non-empty, and its value does equal the passed value (via
382
+ * a shallow comparison ===), then None is returned; otherwise, the Option is
383
+ * returned.
384
+ *
385
+ * In other words, this will let all values through except the passed value.
386
+ *
387
+ * @param T $value
388
+ *
389
+ * @return Option<T>
390
+ */
391
+ abstract public function reject($value);
392
+
393
+ /**
394
+ * Binary operator for the initial value and the option's value.
395
+ *
396
+ * If empty, the initial value is returned. If non-empty, the callable
397
+ * receives the initial value and the option's value as arguments.
398
+ *
399
+ * ```php
400
+ *
401
+ * $some = new Some(5);
402
+ * $none = None::create();
403
+ * $result = $some->foldLeft(1, function($a, $b) { return $a + $b; }); // int(6)
404
+ * $result = $none->foldLeft(1, function($a, $b) { return $a + $b; }); // int(1)
405
+ *
406
+ * // This can be used instead of something like the following:
407
+ * $option = Option::fromValue($integerOrNull);
408
+ * $result = 1;
409
+ * if ( ! $option->isEmpty()) {
410
+ * $result += $option->get();
411
+ * }
412
+ * ```
413
+ *
414
+ * @template S
415
+ *
416
+ * @param S $initialValue
417
+ * @param callable(S, T):S $callable
418
+ *
419
+ * @return S
420
+ */
421
+ abstract public function foldLeft($initialValue, $callable);
422
+
423
+ /**
424
+ * foldLeft() but with reversed arguments for the callable.
425
+ *
426
+ * @template S
427
+ *
428
+ * @param S $initialValue
429
+ * @param callable(T, S):S $callable
430
+ *
431
+ * @return S
432
+ */
433
+ abstract public function foldRight($initialValue, $callable);
434
+ }