Completed
Push — master ( 8bb399...6d8696 )
by Randy
03:07
created

Some   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
dl 0
loc 69
rs 10
c 0
b 0
f 0
wmc 7
lcom 1
cbo 3

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A isSome() 0 6 1
A unwrap() 0 4 1
A ensure() 0 4 2
A isEqualTo() 0 4 1
A isIdenticalTo() 0 4 1
1
<?php
2
3
namespace Dgame\Optional;
4
5
use function Dgame\Ensurance\enforce;
6
7
/**
8
 * Class Some
9
 * @package Dgame\Optional
10
 */
11
final class Some extends AbstractOptional
12
{
13
    /**
14
     * @var mixed
15
     */
16
    private $value;
17
18
    /**
19
     * Some constructor.
20
     *
21
     * @param $value
22
     */
23
    public function __construct($value)
24
    {
25
        enforce($value !== null)->orThrow('Value cannot be null');
26
27
        $this->value = $value;
28
    }
29
30
    /**
31
     * @param mixed $value
32
     *
33
     * @return bool
34
     */
35
    public function isSome(&$value = null): bool
36
    {
37
        $value = $this->value;
38
39
        return true;
40
    }
41
42
    /**
43
     * @return mixed
44
     */
45
    public function unwrap()
46
    {
47
        return $this->value;
48
    }
49
50
    /**
51
     * @param callable $callback
52
     *
53
     * @return OptionalInterface
54
     */
55
    public function ensure(callable $callback): OptionalInterface
56
    {
57
        return $callback($this->value) ? $this : None::instance();
58
    }
59
60
    /**
61
     * @param $value
62
     *
63
     * @return bool
64
     */
65
    public function isEqualTo($value): bool
66
    {
67
        return $value == $this->value;
68
    }
69
70
    /**
71
     * @param $value
72
     *
73
     * @return bool
74
     */
75
    public function isIdenticalTo($value): bool
76
    {
77
        return $value === $this->value;
78
    }
79
}