1: <?php
2:
3: namespace Chromabits\Nucleus\Control;
4:
5: use Chromabits\Nucleus\Control\Interfaces\MonadInterface;
6: use Chromabits\Nucleus\Control\Traits\ChainTrait;
7: use Closure;
8:
9: /**
10: * Class Monad
11: *
12: * @author Eduardo Trujillo <ed@chromabits.com>
13: * @package Chromabits\Nucleus\Monads
14: */
15: abstract class Monad extends Applicative implements MonadInterface
16: {
17: use ChainTrait;
18:
19: /**
20: * The wrapped value.
21: *
22: * @var mixed
23: */
24: protected $value;
25:
26: /**
27: * Construct an instance of a Monad.
28: *
29: * @param $value
30: */
31: public function __construct($value)
32: {
33: parent::__construct();
34:
35: $this->value = $value;
36: }
37:
38: /**
39: * Apply a function.
40: *
41: * @param callable|Closure $closure
42: *
43: * @return MonadInterface
44: */
45: public function fmap(callable $closure)
46: {
47: return $this->bind(function ($a) use ($closure) {
48: return $this->of($closure($a));
49: });
50: }
51:
52: /**
53: * Wrap value inside a monadic value.
54: *
55: * @param $value
56: *
57: * @return static
58: */
59: public static function of($value)
60: {
61: if ($value instanceof static) {
62: return $value;
63: }
64:
65: return new static($value);
66: }
67: }
68: