/
Result.php
102 lines (88 loc) · 2.02 KB
/
Result.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
<?php
declare(strict_types=1);
namespace SilverStripe\MFA\State;
use SilverStripe\Core\Injector\Injectable;
/**
* An immutable result object often detailing the result of a registration or validation completed by the
* respective handlers
*
* @method static Result create(bool $success = true, string $message = '', array $context = [])
*/
class Result
{
use Injectable;
/**
* Indicates this result was successful
*
* @var bool
*/
protected $success;
/**
* An message describing the result
*
* @var string
*/
protected $message = '';
/**
* Context provided by the handler returning this result
*
* @var array
*/
protected $context = [];
/**
* @param bool $success
* @param string $message
* @param array $context
*/
public function __construct(bool $success = true, string $message = '', array $context = [])
{
$this->success = $success;
$this->message = $message;
$this->context = $context;
}
/**
* @return bool
*/
public function isSuccessful(): bool
{
return $this->success;
}
/**
* @return string
*/
public function getMessage(): string
{
return $this->message;
}
/**
* @return array
*/
public function getContext(): array
{
return $this->context;
}
/**
* @param bool $success
* @return Result
*/
public function setSuccessful(bool $success): Result
{
return new static($success, $this->getMessage(), $this->getContext());
}
/**
* @param string $message
* @return Result
*/
public function setMessage(string $message): Result
{
return new static($this->isSuccessful(), $message, $this->getContext());
}
/**
* @param array $context
* @return Result
*/
public function setContext(array $context): Result
{
return new static($this->isSuccessful(), $this->getMessage(), $context);
}
}