1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Demv\JSend; |
4
|
|
|
|
5
|
|
|
use function Dgame\Ensurance\ensure; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class Status |
9
|
|
|
* @package Demv\JSend |
10
|
|
|
*/ |
11
|
|
|
final class Status implements StatusInterface |
12
|
|
|
{ |
13
|
|
|
public const DEFAULT_MAPPING = [ |
14
|
|
|
-1 => self::STATUS_ERROR, |
15
|
|
|
0 => self::STATUS_FAIL, |
16
|
|
|
1 => self::STATUS_SUCCESS |
17
|
|
|
]; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var StatusInterface[] |
21
|
|
|
*/ |
22
|
|
|
private static $instances = []; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var string |
26
|
|
|
*/ |
27
|
|
|
private $status; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Status constructor. |
31
|
|
|
* |
32
|
|
|
* @param string $status |
33
|
|
|
*/ |
34
|
|
|
private function __construct(string $status) |
35
|
|
|
{ |
36
|
|
|
$this->status = $status; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param string $status |
41
|
|
|
* |
42
|
|
|
* @return StatusInterface |
43
|
|
|
*/ |
44
|
|
|
public static function instance(string $status): StatusInterface |
45
|
|
|
{ |
46
|
|
|
ensure($status)->isIn([self::STATUS_SUCCESS, self::STATUS_FAIL, self::STATUS_ERROR]) |
47
|
|
|
->orThrow('Expected valid status'); |
48
|
|
|
if (!array_key_exists($status, self::$instances)) { |
49
|
|
|
self::$instances[$status] = new self($status); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
return self::$instances[$status]; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @return StatusInterface |
57
|
|
|
*/ |
58
|
|
|
public static function success(): StatusInterface |
59
|
|
|
{ |
60
|
|
|
return self::instance(self::STATUS_SUCCESS); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @return StatusInterface |
65
|
|
|
*/ |
66
|
|
|
public static function fail(): StatusInterface |
67
|
|
|
{ |
68
|
|
|
return self::instance(self::STATUS_FAIL); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @return StatusInterface |
73
|
|
|
*/ |
74
|
|
|
public static function error(): StatusInterface |
75
|
|
|
{ |
76
|
|
|
return self::instance(self::STATUS_ERROR); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @param int $value |
81
|
|
|
* @param array $mapping |
82
|
|
|
* |
83
|
|
|
* @return StatusInterface |
84
|
|
|
*/ |
85
|
|
|
public static function translate(int $value, array $mapping = self::DEFAULT_MAPPING): StatusInterface |
86
|
|
|
{ |
87
|
|
|
ensure($value)->isKeyOf($mapping)->orThrow('Cannot map %d, there is not mapping available', $value); |
88
|
|
|
$status = $mapping[$value]; |
89
|
|
|
ensure($status)->isNotEmpty()->isString()->orThrow('Status must be string'); |
90
|
|
|
|
91
|
|
|
return self::instance($status); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @return bool |
96
|
|
|
*/ |
97
|
|
|
public function isFail(): bool |
98
|
|
|
{ |
99
|
|
|
return $this->status === self::STATUS_FAIL; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @return bool |
104
|
|
|
*/ |
105
|
|
|
public function isSuccess(): bool |
106
|
|
|
{ |
107
|
|
|
return $this->status === self::STATUS_SUCCESS; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @return bool |
112
|
|
|
*/ |
113
|
|
|
public function isError(): bool |
114
|
|
|
{ |
115
|
|
|
return $this->status === self::STATUS_ERROR; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @return string |
120
|
|
|
*/ |
121
|
|
|
public function __toString(): string |
122
|
|
|
{ |
123
|
|
|
return $this->status; |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|