Exemplo n.º 1
0
 public function map(\Closure $closure)
 {
     $array = is_array($this->subject) ? $this->subject : [$this->subject];
     foreach ($array as $key => $value) {
         $closure_ret_val = $closure(\Maybe($value), \Maybe($key));
         $array[$key] = $closure_ret_val instanceof static ? $closure_ret_val->val() : $closure_ret_val;
     }
     return \Maybe(is_array($this->subject) ? $array : $array[0]);
 }
Exemplo n.º 2
0
 function bind($fn)
 {
     return Maybe($fn($this->value));
 }
Exemplo n.º 3
0
 public function testNonNullValueIsInstanceOfSome()
 {
     $this->assertSame('Pirminis\\Some', get_class(\Maybe(23)));
 }
Exemplo n.º 4
0
 /**
  * {@inheritdoc}
  */
 public function bind(callable $fn)
 {
     return Maybe($fn($this->value));
 }
Exemplo n.º 5
0
// result: int(9900)
// you can also nest maps
$order = \Maybe(new Order(new User('John')));
$name = $order->map(function ($order) {
    var_dump('Got order...');
    return $order->getUser()->map(function ($user) {
        var_dump('Got user...');
        return $user->getName()->map(function ($name) {
            var_dump('Got name...');
            return $name;
        });
    });
});
var_dump($name->val());
// result:
// string(12) "Got order..."
// string(11) "Got user..."
// string(11) "Got name..."
// string(4) "John"
// 11. you can also use methods 'is_some()' and 'is_none()' to simple test if
// monad's value is set (some) or empty (none)
$name = \Maybe();
var_dump($name->is_some());
// bool(false)
var_dump($name->is_none());
// bool(true)
$age = \Maybe(28);
var_dump($age->is_some());
// bool(true)
var_dump($age->is_none());
// bool(false)
Exemplo n.º 6
0
 /**
  * Create a child of the Maybe construct
  * @param mixed|null $head
  * @return None|Some
  */
 static function of($head = null)
 {
     return Maybe($head);
 }