Skip to content

Commit ea8f3e0

Browse files
authored
Redesign all of codes (#16)
* Replace namespace from `GoodCodeParser` to `GoodCode` * Remove useless classes * Create a single class for all good types * Prevent vscode setting files from `.gitignore` * Update`README.md`
1 parent 2493138 commit ea8f3e0

19 files changed

Lines changed: 255 additions & 534 deletions

.gitignore

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -136,10 +136,6 @@ fabric.properties
136136

137137
### VisualStudioCode ###
138138
.vscode/*
139-
!.vscode/settings.json
140-
!.vscode/tasks.json
141-
!.vscode/launch.json
142-
!.vscode/extensions.json
143139

144140
### VisualStudioCode Patch ###
145141
# Ignore all local history of files

README.md

Lines changed: 55 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -30,61 +30,69 @@ composer require cable8mm/good-code
3030

3131
## Usage
3232

33+
Facades provides a simple implementation for `set-code`:
34+
3335
```php
3436
<?php
35-
36-
print GoodCode::getSetCodes('set7369x4ZZ4235x6')
37+
print GoodCode::of('SET7369x4zz4235x6')->value();
3738
//=> ['7369'=>4,'4235'=>6]
3839

39-
print GoodCode::makeSetCode(
40+
print GoodCode::setCodeOf(
4041
[
4142
'1234' => 2,
4243
'5678' => 1,
4344
]
44-
)
45-
//=> set1234x2ZZ5678x1
46-
47-
print GoodCode::getId('COM10')
48-
//=> 10
49-
50-
print GoodCode::getId('GIF239')
51-
//=> 239
52-
53-
$parsed = (new GoodCodeParser('com2'))->with(ComplexGood::class, [
54-
1 => 'set11319x1ZZ11626x1ZZ11624x1ZZ11628x1',
55-
2 => 'set11318x1ZZP3800x1ZZP7776x1ZZP9732x1',
56-
3 => 'set11318x1ZZP2526x1ZZP7776x1'
57-
])->get();
58-
59-
// set11318x1ZZP3800x1ZZP7776x1ZZP9732x1
60-
61-
$parsed = (new GoodCodeParser('gif1'))->with(GiftGood::class, [
62-
1 => 'set11319x1ZZ11626x1ZZ11624x1ZZ11628x1',
63-
2 => 'set11318x1ZZP3800x1ZZP7776x1ZZP9732x1',
64-
3 => 'set11318x1ZZP2526x1ZZP7776x1'
65-
])->get();
66-
67-
// set11319x1ZZ11626x1ZZ11624x1ZZ11628x1
68-
69-
$inOptionCode = 'OPT1';
70-
$inOptionName = 'Super Smash Bros. Ultimate';
71-
72-
$parsed = (new OptionCodeParser($inOptionCode, $inOptionName))
73-
->with(OptionGood::class, [
74-
['id' => 1, 'code' => 'OPT1', 'name' => 'Nintendo Switch Super Sales'],
75-
['id' => 2, 'code' => 'OPT2', 'name' => 'Playstation Super Sales'],
76-
], [
77-
['code' => 1, 'master_code' => 'COM4', 'name' => 'Super Smash Bros. Ultimate'],
78-
['code' => 1, 'master_code' => '3124', 'name' => 'Animal Crossing: New Horizons'],
79-
['code' => 1, 'master_code' => '1234', 'name' => 'The Legend of Zelda: Tears of the Kingdom'],
80-
['code' => 1, 'master_code' => '4324', 'name' => 'Super Mario 3D World + Bowser\'s Fury'],
81-
['code' => 2, 'master_code' => '2314', 'name' => 'Call of Duty®: Black Ops 6'],
82-
['code' => 2, 'master_code' => '43123', 'name' => 'Grand Theft Auto V'],
83-
['code' => 2, 'master_code' => '42342', 'name' => 'Marvel\'s Spider-Man 2'],
84-
])
85-
->get();
86-
87-
// COM4
45+
)->code();
46+
//=> 'set1234x2ZZ5678x1'
47+
```
48+
49+
Facades provides a simple implementation for `complex-code`:
50+
51+
```php
52+
print GoodCode::of('COM10', callback: function ($key) {
53+
$a = [
54+
10 => '123',
55+
];
56+
57+
return $a[$key];
58+
})->value();
59+
//=> '123'
60+
```
61+
62+
Facades provides a simple implementation for `gift-code`:
63+
64+
```php
65+
print GoodCode::of('GIF11', callback: function ($key) {
66+
$a = [
67+
11 => '456',
68+
];
69+
70+
return $a[$key];
71+
});
72+
//=> '456'
73+
```
74+
75+
Facades provides a simple implementation for `option-code`:
76+
77+
> [!TIP]
78+
> `option-code` are matching with **both** `option-code` **and** `option-good-option` name. Unfortunately all of online shops like Coupang and 11st have not send any key for option to sellers.
79+
80+
```php
81+
print GoodCode::of($optionCode, option: $optionName, callback: function ($key, $option) {
82+
$a = [
83+
10 => [
84+
'Super Smash Bros. Ultimate' => 'COM4',
85+
'Animal Crossing: New Horizons' => '3124',
86+
'The Legend of Zelda: Tears of the Kingdom' => '1234',
87+
'Call of Duty®: Black Ops 6' => '2314',
88+
'Grand Theft Auto V' => '43123',
89+
'42342', 'name' => 'Marvel\'s Spider-Man 2',
90+
],
91+
];
92+
93+
return $a[$key][$option];
94+
})->value();
95+
//=> '3124'
8896

8997
```
9098

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,12 @@
2626
],
2727
"autoload": {
2828
"psr-4": {
29-
"Cable8mm\\GoodCodeParser\\": "src/"
29+
"Cable8mm\\GoodCode\\": "src/"
3030
}
3131
},
3232
"autoload-dev": {
3333
"psr-4": {
34-
"Cable8mm\\GoodCodeParser\\Tests\\": "tests/"
34+
"Cable8mm\\GoodCode\\Tests\\": "tests/"
3535
}
3636
},
3737
"minimum-stability": "dev",

src/Contracts/OptionParser.php

Lines changed: 0 additions & 20 deletions
This file was deleted.

src/Contracts/Parser.php

Lines changed: 0 additions & 21 deletions
This file was deleted.

src/Enums/GoodCodeType.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace Cable8mm\GoodCodeParser\Enums;
3+
namespace Cable8mm\GoodCode\Enums;
44

55
enum GoodCodeType
66
{
@@ -14,16 +14,16 @@ public function prefix(): string
1414
{
1515
return match ($this) {
1616
GoodCodeType::NORMAL => '',
17-
GoodCodeType::SET => 'set',
18-
GoodCodeType::COMPLEX => 'com',
19-
GoodCodeType::GIFT => 'gif',
20-
GoodCodeType::OPTION => 'opt',
17+
GoodCodeType::SET => 'SET',
18+
GoodCodeType::COMPLEX => 'COM',
19+
GoodCodeType::GIFT => 'GIF',
20+
GoodCodeType::OPTION => 'OPT',
2121
};
2222
}
2323

2424
public static function of(string $code): GoodCodeType
2525
{
26-
$prefix = substr($code, 0, 3);
26+
$prefix = strtoupper(substr($code, 0, 3));
2727

2828
return match ($prefix) {
2929
GoodCodeType::SET->prefix() => GoodCodeType::SET,

src/GoodCode.php

Lines changed: 123 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,154 @@
11
<?php
22

3-
namespace Cable8mm\GoodCodeParser;
3+
namespace Cable8mm\GoodCode;
44

5-
use Cable8mm\GoodCodeParser\Enums\GoodCodeType;
6-
use Cable8mm\GoodCodeParser\Parsers\SetGood;
5+
use BadFunctionCallException;
6+
use Cable8mm\GoodCode\Enums\GoodCodeType;
7+
use InvalidArgumentException;
78

89
/**
910
* Make set code, option code and so on.
1011
*/
1112
class GoodCode
1213
{
14+
const SET_CODE_DELIMITER = 'zz';
15+
16+
const SET_CODE_DELIMITER_COUNT = 'x';
17+
18+
private GoodCodeType $type;
19+
20+
public function __construct(
21+
private string $code,
22+
private GoodCodeType $originType,
23+
) {
24+
$this->type = GoodCodeType::of($code);
25+
}
26+
27+
public function code(): string
28+
{
29+
return $this->code;
30+
}
31+
32+
public function originalType(): GoodCodeType
33+
{
34+
return $this->originType;
35+
}
36+
37+
public function type(): GoodCodeType
38+
{
39+
return $this->type;
40+
}
41+
42+
/**
43+
* Output value for good code.
44+
* If the code is set code, it will be array of good values.
45+
* If the code is normal code, it will be good code string.
46+
* the code shouldn't be option, complex and gift code.
47+
*/
48+
public function value(): int|string|array
49+
{
50+
if ($this->type == GoodCodeType::OPTION || $this->type == GoodCodeType::GIFT || $this->type == GoodCodeType::COMPLEX) {
51+
return throw new BadFunctionCallException('Only complex and set code types are supported');
52+
}
53+
54+
if ($this->type == GoodCodeType::SET) {
55+
return self::getSetCodes($this->code);
56+
}
57+
58+
return $this->code;
59+
}
60+
61+
/**
62+
* Get ID from GIF and COM codes.
63+
*
64+
* @param string $code GIF or COM code
65+
* @return int The method returns ID
66+
*/
67+
public static function getId(string $code): int
68+
{
69+
return (int) preg_replace('/[^0-9]/', '', $code);
70+
}
71+
72+
/**
73+
* Create GoodCode instance from given code string and callback function
74+
*
75+
* @param string $code `good_code`, `set_code`, `option_code`
76+
* @param ?callable $callback Function to call
77+
* @param ?string $option `option_good_option` name
78+
* @return GoodCode The method returns GoodCode instance.
79+
*/
80+
public static function of(string $code, ?string $option = null, ?callable $callback = null): GoodCode
81+
{
82+
$type = $originalType = GoodCodeType::of($code);
83+
84+
if ($type == GoodCodeType::OPTION) {
85+
$code = $callback(self::getId($code), $option);
86+
$type = GoodCodeType::of($code);
87+
}
88+
89+
if ($type == GoodCodeType::COMPLEX || $type == GoodCodeType::GIFT) {
90+
$code = $callback(self::getId($code));
91+
}
92+
93+
return new GoodCode($code, $originalType);
94+
}
95+
1396
/**
1497
* Make SetCode from key-value set code array.
1598
*
1699
* @param array $setCodes key-value set code array
17-
* @return string The method returns the SetCode
100+
* @return GoodCode The method returns GoodCode instance with the SetCode array
18101
*
19-
* @example GoodCode::makeSetCode(['7369'=>4,'4235'=>6]) set7369x4ZZ4235x6
102+
* @example GoodCode::setCodeOf(['7369'=>4,'4235'=>6])
20103
*/
21-
public static function makeSetCode(array $setCodes): string
104+
public static function setCodeOf(array $setCodes): GoodCode
22105
{
23-
return GoodCodeType::SET->prefix().implode(SetGood::DELIMITER, array_map(function ($v, $k) {
24-
return $k.SetGood::DELIMITER_COUNT.$v;
25-
}, $setCodes, array_keys($setCodes)));
106+
return new GoodCode(
107+
self::makeSetCode($setCodes),
108+
GoodCodeType::SET
109+
);
26110
}
27111

28112
/**
29-
* Make set code key-value array from SetCode.
113+
* Make SetCode from key-value set code array.
30114
*
31-
* @param string $setCode SetCode
32-
* @return array key-value set code
115+
* @param array $setCodes key-value set code array
116+
* @return string The method returns GoodCode instance with the SetCode string
33117
*
34-
* @example GoodCode::setCodes('set7369x4ZZ4235x6) ['7369'=>4,'4235'=>6]
118+
* @example GoodCode::setCodeOf(['7369'=>4,'4235'=>6])
35119
*/
36-
public static function getSetCodes(string $setCode): array
120+
private static function makeSetCode(array $setCodes): string
37121
{
38-
return (new GoodCodeParser($setCode))
39-
->with(SetGood::class)
40-
->get();
122+
return GoodCodeType::SET->prefix().implode(self::SET_CODE_DELIMITER, array_map(function ($v, $k) {
123+
return $k.self::SET_CODE_DELIMITER_COUNT.$v;
124+
}, $setCodes, array_keys($setCodes)));
41125
}
42126

43127
/**
44-
* Get ID from GIF and COM codes.
128+
* Find set-good code by parsing. A set of good code aka set-code is a combination of two more goods codes.
45129
*
46-
* @param string $code GIF or COM code
47-
* @return int The method returns ID
130+
* @param string $setCode "set1232x3ZZ322ZZ4313x4" means "1232" x 3 + "322" x 4 + "4313" x 4. "1232", "322" and "4312" are good codes.
131+
* @return array The method returns good code array
132+
*
133+
* @throws InvalidArgumentException
48134
*/
49-
public static function getId(string $code): int
135+
public static function getSetCodes(string $setCode): array
50136
{
51-
return (int) preg_replace('/[^0-9]/', '', $code);
137+
$escape = preg_replace('/^'.GoodCodeType::SET->prefix().'/i', '', $setCode);
138+
139+
$goodCodes = explode(self::SET_CODE_DELIMITER, $escape);
140+
141+
$parsedCodes = [];
142+
143+
foreach ($goodCodes as $code) {
144+
if (preg_match('/'.self::SET_CODE_DELIMITER_COUNT.'/i', $code)) {
145+
[$k, $v] = explode(self::SET_CODE_DELIMITER_COUNT, $code);
146+
} else {
147+
[$k, $v] = [$code, 1];
148+
}
149+
$parsedCodes[$k] = $v;
150+
}
151+
152+
return $parsedCodes;
52153
}
53154
}

0 commit comments

Comments
 (0)