PageRenderTime 27ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/phpoption/phpoption/src/PhpOption/Option.php

https://github.com/nattaphat/hgis
PHP | 252 lines | 36 code | 21 blank | 195 comment | 2 complexity | 3951c00a25267398e4ab9a9f43ef7034 MD5 | raw file
  1. <?php
  2. /*
  3. * Copyright 2012 Johannes M. Schmitt <schmittjoh@gmail.com>
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. namespace PhpOption;
  18. /**
  19. * Base Option Class.
  20. *
  21. * @author Johannes M. Schmitt <schmittjoh@gmail.com>
  22. */
  23. abstract class Option
  24. {
  25. /**
  26. * Creates an option given a return value.
  27. *
  28. * This is intended for consuming existing APIs and allows you to easily
  29. * convert them to an option. By default, we treat ``null`` as the None case,
  30. * and everything else as Some.
  31. *
  32. * @param mixed $value The actual return value.
  33. * @param mixed $noneValue The value which should be considered "None"; null
  34. * by default.
  35. *
  36. * @return Option
  37. */
  38. public static function fromValue($value, $noneValue = null)
  39. {
  40. if ($value === $noneValue) {
  41. return None::create();
  42. }
  43. return new Some($value);
  44. }
  45. /**
  46. * Creates a lazy-option with the given callback.
  47. *
  48. * This is also a helper constructor for lazy-consuming existing APIs where
  49. * the return value is not yet an option. By default, we treat ``null`` as
  50. * None case, and everything else as Some.
  51. *
  52. * @param callable $callback The callback to evaluate.
  53. * @param array $arguments
  54. * @param mixed $noneValue The value which should be considered "None"; null
  55. * by default.
  56. *
  57. * @return Option
  58. */
  59. public static function fromReturn($callback, array $arguments = array(), $noneValue = null)
  60. {
  61. return new LazyOption(function() use ($callback, $arguments, $noneValue) {
  62. $return = call_user_func_array($callback, $arguments);
  63. if ($return === $noneValue) {
  64. return None::create();
  65. }
  66. return new Some($return);
  67. });
  68. }
  69. /**
  70. * Returns the value if available, or throws an exception otherwise.
  71. *
  72. * @throws \RuntimeException if value is not available
  73. *
  74. * @return mixed
  75. */
  76. abstract public function get();
  77. /**
  78. * Returns the value if available, or the default value if not.
  79. *
  80. * @param mixed $default
  81. *
  82. * @return mixed
  83. */
  84. abstract public function getOrElse($default);
  85. /**
  86. * Returns the value if available, or the results of the callable.
  87. *
  88. * This is preferable over ``getOrElse`` if the computation of the default
  89. * value is expensive.
  90. *
  91. * @param callable $callable
  92. *
  93. * @return mixed
  94. */
  95. abstract public function getOrCall($callable);
  96. /**
  97. * Returns the value if available, or throws the passed exception.
  98. *
  99. * @param \Exception $ex
  100. *
  101. * @return mixed
  102. */
  103. abstract public function getOrThrow(\Exception $ex);
  104. /**
  105. * Returns true if no value is available, false otherwise.
  106. *
  107. * @return boolean
  108. */
  109. abstract public function isEmpty();
  110. /**
  111. * Returns true if a value is available, false otherwise.
  112. *
  113. * @return boolean
  114. */
  115. abstract public function isDefined();
  116. /**
  117. * Returns this option if non-empty, or the passed option otherwise.
  118. *
  119. * This can be used to try multiple alternatives, and is especially useful
  120. * with lazy evaluating options:
  121. *
  122. * ```php
  123. * $repo->findSomething()
  124. * ->orElse(new LazyOption(array($repo, 'findSomethingElse')))
  125. * ->orElse(new LazyOption(array($repo, 'createSomething')));
  126. * ```
  127. *
  128. * @param Option $else
  129. *
  130. * @return Option
  131. */
  132. abstract public function orElse(Option $else);
  133. /**
  134. * This is similar to map() below except that the return value has no meaning;
  135. * the passed callable is simply executed if the option is non-empty, and
  136. * ignored if the option is empty.
  137. *
  138. * In all cases, the return value of the callable is discarded.
  139. *
  140. * ```php
  141. * $comment->getMaybeFile()->ifDefined(function($file) {
  142. * // Do something with $file here.
  143. * });
  144. * ```
  145. *
  146. * If you're looking for something like ``whenEmpty``, you can use ``getOrCall``
  147. * and ``getOrElse`` in these cases.
  148. *
  149. * @param callable $callable
  150. *
  151. * @return void
  152. */
  153. abstract public function ifDefined($callable);
  154. /**
  155. * Applies the callable to the value of the option if it is non-empty,
  156. * and returns the return value of the callable wrapped in Some().
  157. *
  158. * If the option is empty, then the callable is not applied.
  159. *
  160. * ```php
  161. * (new Some("foo"))->map('strtoupper')->get(); // "FOO"
  162. * ```
  163. *
  164. * @param callable $callable
  165. *
  166. * @return Option
  167. */
  168. abstract public function map($callable);
  169. /**
  170. * Applies the callable to the value of the option if it is non-empty, and
  171. * returns the return value of the callable directly.
  172. *
  173. * In contrast to ``map``, the return value of the callable is expected to
  174. * be an Option itself; it is not automatically wrapped in Some().
  175. *
  176. * @param callable $callable must return an Option
  177. *
  178. * @return Option
  179. */
  180. abstract public function flatMap($callable);
  181. /**
  182. * If the option is empty, it is returned immediately without applying the callable.
  183. *
  184. * If the option is non-empty, the callable is applied, and if it returns true,
  185. * the option itself is returned; otherwise, None is returned.
  186. *
  187. * @param callable $callable
  188. *
  189. * @return Option
  190. */
  191. abstract public function filter($callable);
  192. /**
  193. * If the option is empty, it is returned immediately without applying the callable.
  194. *
  195. * If the option is non-empty, the callable is applied, and if it returns false,
  196. * the option itself is returned; otherwise, None is returned.
  197. *
  198. * @param callable $callable
  199. *
  200. * @return Option
  201. */
  202. abstract public function filterNot($callable);
  203. /**
  204. * If the option is empty, it is returned immediately.
  205. *
  206. * If the option is non-empty, and its value does not equal the passed value
  207. * (via a shallow comparison ===), then None is returned. Otherwise, the
  208. * Option is returned.
  209. *
  210. * In other words, this will filter all but the passed value.
  211. *
  212. * @param mixed $value
  213. *
  214. * @return Option
  215. */
  216. abstract public function select($value);
  217. /**
  218. * If the option is empty, it is returned immediately.
  219. *
  220. * If the option is non-empty, and its value does equal the passed value (via
  221. * a shallow comparison ===), then None is returned; otherwise, the Option is
  222. * returned.
  223. *
  224. * In other words, this will let all values through expect the passed value.
  225. *
  226. * @param mixed $value
  227. *
  228. * @return Option
  229. */
  230. abstract public function reject($value);
  231. }