This repository was archived by the owner on Jun 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPostStatus.php
More file actions
134 lines (119 loc) · 2.78 KB
/
PostStatus.php
File metadata and controls
134 lines (119 loc) · 2.78 KB
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
<?php
namespace Kuriv\PHPDesignPatterns\More\Repository;
use InvalidArgumentException;
class PostStatus
{
/**
* Just a test constant.
*
* @var int
*/
public const STATUS_DRAFT_ID = 1;
/**
* Just a test constant.
*
* @var string
*/
public const STATUS_DRAFT = 'Draft';
/**
* Just a test constant.
*
* @var int
*/
public const STATUS_PUBLISHED_ID = 2;
/**
* Just a test constant.
*
* @var string
*/
public const STATUS_PUBLISHED = 'Published';
/**
* Define an array of allowed status.
*
* @var array
*/
private static array $validStatus = [
self::STATUS_DRAFT_ID => self::STATUS_DRAFT,
self::STATUS_PUBLISHED_ID => self::STATUS_PUBLISHED
];
/**
* Store the status ID.
*
* @var int
*/
private int $status_id;
/**
* Store the status string.
*
* @var string
*/
private string $status_string;
/**
* Store the status ID and status string to the current instance.
*
* @param int $status_id
* @param string $status_string
* @return void
*/
private function __construct(int $status_id, string $status_string)
{
$this->status_id = $status_id;
$this->status_string = $status_string;
}
/**
* Return the current status string.
*
* @param void
* @return string
*/
public function __toString(): string
{
return $this->status_string;
}
/**
* Get the created instance by status ID.
*
* @param int $status_id
* @return PostStatus
*/
public static function getInstanceByStatusID(int $status_id): PostStatus
{
if (!isset(self::$validStatus[$status_id])) {
throw new InvalidArgumentException('Invalid status ID given');
}
return new self($status_id, self::$validStatus[$status_id]);
}
/**
* Get the created instance by status string.
*
* @param string $status_string
* @return PostStatus
*/
public static function getInstanceByStatusString(string $status_string): PostStatus
{
if (!in_array($status_string, self::$validStatus, true)) {
throw new InvalidArgumentException('Invalid status string given');
}
return new self(array_search($status_string, self::$validStatus, true), $status_string);
}
/**
* Get the status ID.
*
* @param void
* @return int
*/
public function getStatusID(): int
{
return $this->status_id;
}
/**
* Get the status string.
*
* @param void
* @return string
*/
public function getStatusString(): string
{
return $this->status_string;
}
}