-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGoodCode.php
More file actions
127 lines (111 loc) · 3.34 KB
/
GoodCode.php
File metadata and controls
127 lines (111 loc) · 3.34 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
<?php
namespace Cable8mm\GoodCode;
use BadFunctionCallException;
use Cable8mm\GoodCode\Enums\GoodCodeType;
use Cable8mm\GoodCode\ValueObjects\SetGood;
/**
* Make set code, option code and so on.
*/
class GoodCode
{
/**
* @var GoodCodeType The type of the code
*/
private GoodCodeType $type;
/**
* Constructor.
*
* @param string $code The code
* @param \Cable8mm\GoodCode\Enums\GoodCodeType $originType The type of the code
*/
public function __construct(
private string $code,
private GoodCodeType $originType,
) {
$this->type = GoodCodeType::of($code);
}
/**
* Gets the `code` property
*/
public function code(): string
{
return $this->code;
}
/**
* Gets the `originalType` property
*/
public function originalType(): GoodCodeType
{
return $this->originType;
}
/**
* Gets the `type` property
*/
public function type(): GoodCodeType
{
return $this->type;
}
/**
* Output value for good code.
* If the code is set code, it will be array of good values.
* If the code is normal code, it will be good code string.
* the code shouldn't be option, complex and gift code.
*
* @throws BadFunctionCallException
*/
public function value(): int|string|array
{
if ($this->type == GoodCodeType::OPTION || $this->type == GoodCodeType::GIFT || $this->type == GoodCodeType::COMPLEX) {
return throw new BadFunctionCallException('Only complex and set code types are supported');
}
if ($this->type == GoodCodeType::SET) {
return SetGood::of($this->code)->goods();
}
return $this->code;
}
/**
* Get ID from GIF and COM codes.
*
* @param string $code GIF or COM code
* @return int The method returns ID
*/
public static function getId(string $code): int
{
return (int) preg_replace('/[^0-9]/', '', $code);
}
/**
* Create GoodCode instance from given code string and callback function
*
* @param string $code `good_code`, `set_code`, `option_code`
* @param ?callable $callback Function to call
* @param ?string $option `option_good_option` name
* @return GoodCode The method returns GoodCode instance.
*/
public static function of(string $code, ?string $option = null, ?callable $callback = null): GoodCode
{
$type = $originalType = GoodCodeType::of($code);
if ($type == GoodCodeType::OPTION) {
$code = $callback(self::getId($code), $option);
$type = GoodCodeType::of($code);
}
if ($type == GoodCodeType::COMPLEX || $type == GoodCodeType::GIFT) {
$code = $callback(self::getId($code));
}
return new GoodCode($code, $originalType);
}
/**
* Make SetCode from key-value set code array.
*
* @param array<string,int> $setCodes key-value set code array
* @return GoodCode The method returns GoodCode instance with the SetCode array
*
* @example GoodCode::setCodeOf(['7369'=>4,'4235'=>6])
*/
public static function setCodeOf(array $setCodes): GoodCode
{
return new GoodCode(
SetGood::ofArray($setCodes)->code(),
GoodCodeType::SET
);
}
}