PHP code example of cable8mm / good-code

1. Go to this page and download the library: Download cable8mm/good-code library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

cable8mm / good-code example snippets




use App\Models\OptionGood;
use Cable8mm\GoodCode\Enums\GoodCodeType;
use Cable8mm\GoodCode\GoodCode;

/**
 * For option products, retrieve the master_code of the option and update it.
 */
if (GoodCodeType::of($this->data->get('sellerGoodsCd')) == GoodCodeType::OPTION) {
    $code = GoodCode::of(
        $this->data->get('sellerGoodsCd'),
        option: $this->data->get('option'),
        callback: function ($key, $option) {
            return OptionGood::findMasterCode($key)->option($option)->first()->masterCode();
        }
    )->code();

    $this->data->put('masterGoodsCd', $code);
}



use Cable8mm\GoodCode\Enums\GoodCodeType;
use Cable8mm\GoodCode\GoodCode;
use Cable8mm\GoodCode\ValueObjects\SetGood;

/**
 * For composite and gift products, retrieve the set product and update the master_code.
 */
if (
    GoodCodeType::of($this->data->get('sellerGoodsCd')) == GoodCodeType::COMPLEX
    || GoodCodeType::of($this->data->get('sellerGoodsCd')) == GoodCodeType::GIFT
) {
    $code = GoodCode::of(
        $this->data->get('sellerGoodsCd'),
        callback: function ($key) {
            return SetGood::findComCode($key)->master_code;
        }
    )->code();

    $this->data->put('masterGoodsCd', $code);
}



use Cable8mm\GoodCode\GoodCode;

print GoodCode::of('SET7369x4zz4235x6')->value();
//=> ['7369'=>4,'4235'=>6]

print GoodCode::setCodeOf(['1234' => 2, '5678' => 1,])->code();
//=> 'set1234x2ZZ5678x1'



use Cable8mm\GoodCode\GoodCode;

print GoodCode::of('COM10', callback: function ($key) {
    $a = [ 10 => '123'];

    return $a[$key];
})->value();
//=> '123'



use Cable8mm\GoodCode\GoodCode;

print GoodCode::of('GIF11', callback: function ($key) {
    $a = [
        11 => '456',
    ];

    return $a[$key];
});
//=> '456'



use Cable8mm\GoodCode\GoodCode;

print GoodCode::of($optionCode, option: $optionName, callback: function ($key, $option) {
    $a = [
        10 => [
            'Super Smash Bros. Ultimate' => 'COM4',
            'Animal Crossing: New Horizons' => '3124',
            'The Legend of Zelda: Tears of the Kingdom' => '1234',
            'Call of Duty®: Black Ops 6' => '2314',
            'Grand Theft Auto V' => '43123',
            '42342', 'name' => 'Marvel\'s Spider-Man 2',
        ],
    ];

    return $a[$key][$option];
})->value();
//=> '3124'




use Cable8mm\GoodCode\ValueObjects\SetGood;

print SetGood::of('SET43x3zz253x3')->goods();
//=> ['43' => 3, '253' => 3]

print SetGood::ofArray(['43' => 3, '253' => 3])->code();
//=> SET43x3zz253x3



use Cable8mm\GoodCode\ReceiptCode;

print ReceiptCode::of('PO-20250312-0001')->code;
//=> PO-20250312-0001

print ReceiptCode::of('PO-20250312-0001')->prefix;
//=> PO

print ReceiptCode::of('PO-20250312-0001')->ymd;
//=> 20250312

print ReceiptCode::of('PO-20250312-0001')->number;
//=> 0001

print ReceiptCode::of('PO-20250312-0001')->nextCode();
//=> PO-20250312-0002

print ReceiptCode::of()->nextCode();
//=> PO-[Today's ymd]-0001

print ReceiptCode::of(prefix: 'CT')->nextCode();
//=> CT-[Today's ymd]-0001

print LocationCode::of(warehouse: 'AUK', rack: 'R3', shelf: 'S32')->locationCode();
print LocationCode::of(['warehouse' => 'AUK', 'rack' => 'R3', 'shelf' => 'S32')->locationCode();
print LocationCode::of(warehouse: 'AUK', rack: 'R3', shelf: 'S32'); //` Stringable` supported
//=> AUK-R3-S32

print Sku::of(code: 123, prefix: 'PO');     //=> PO123
print Sku::of(123);                         //=> 123
print Sku::of(123, prefix: 'PO');           //=> PO123