PHP code example of azaharizaman / nexus-product

1. Go to this page and download the library: Download azaharizaman/nexus-product 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/ */

    

azaharizaman / nexus-product example snippets


use Nexus\Product\Services\ProductManager;
use Nexus\Product\Enums\ProductType;
use Nexus\Product\Enums\TrackingMethod;
use Nexus\Product\ValueObjects\Sku;
use Nexus\Uom\ValueObjects\Quantity;

$productManager->createStandaloneVariant(
    tenantId: 'tenant-123',
    code: 'WIDGET-001',
    name: 'Premium Widget',
    type: ProductType::STORABLE,
    trackingMethod: TrackingMethod::SERIAL_NUMBER,
    weight: new Quantity(2.5, 'kg'),
    categoryCode: 'HARDWARE'
);

// 1. Create template
$template = $productManager->createTemplate(
    tenantId: 'tenant-123',
    code: 'TSHIRT-X',
    name: 'T-Shirt Model X',
    description: 'Premium cotton t-shirt',
    categoryCode: 'APPAREL'
);

// 2. Define attributes
$colorAttribute = $attributeRepository->findByCode('COLOR');
$sizeAttribute = $attributeRepository->findByCode('SIZE');

// 3. Generate variants
$variants = $variantGenerator->generateVariants(
    templateId: $template->getId(),
    attributes: [
        'COLOR' => ['Red', 'Blue', 'Green'],
        'SIZE' => ['S', 'M', 'L', 'XL']
    ]
);
// Creates 12 variants (3 colors × 4 sizes)

use Nexus\Product\ValueObjects\Barcode;
use Nexus\Product\Enums\BarcodeFormat;

// EAN-13 validation
$barcode = new Barcode('5901234123457', BarcodeFormat::EAN13);

// Barcode service
$barcodeService->validate($barcode); // true if valid checksum
$barcodeService->lookupVariant($barcode); // Find product by barcode

// Configuration via Nexus\Setting
$settings->setInt('product.max_variants_per_template', 1000);

// This will throw VariantExplosionException if > 1000 combinations
$variantGenerator->generateVariants($templateId, [
    'COLOR' => [...], // 10 values
    'SIZE' => [...],  // 10 values
    'STYLE' => [...], // 10 values
    'FABRIC' => [...] // 10 values = 10,000 variants!
]);

use Nexus\Product\ValueObjects\DimensionSet;
use Nexus\Uom\ValueObjects\Quantity;

$dimensions = new DimensionSet(
    weight: new Quantity(5.5, 'kg'),
    length: new Quantity(30, 'cm'),
    width: new Quantity(20, 'cm'),
    height: new Quantity(10, 'cm'),
    volume: new Quantity(6, 'L')
);

use Nexus\Product\Services\SkuGenerator;
use Nexus\Sequencing\Contracts\SequenceGeneratorInterface;

$skuGenerator = new SkuGenerator($sequenceGenerator);
$sku = $skuGenerator->generateSku('tenant-123', 'PRODUCT');
// Result: "PRD-2024-00001"

interface ProductVariantInterface {
    public function getDefaultRevenueAccountCode(): ?string;
    public function getDefaultCostAccountCode(): ?string;
    public function getDefaultInventoryAccountCode(): ?string;
}

interface PurchaseOrderLineInterface {
    public function getProductVariantId(): ?string;
    public function getItemDescription(): string; // Fallback for legacy
}

use Nexus\Product\ValueObjects\Sku;

$sku = new Sku('PRD-2024-00001');
$sku->getValue(); // "PRD-2024-00001"
$sku->toArray(); // ['value' => 'PRD-2024-00001']

use Nexus\Product\ValueObjects\Barcode;
use Nexus\Product\Enums\BarcodeFormat;

$barcode = new Barcode('5901234123457', BarcodeFormat::EAN13);
$barcode->getValue(); // "5901234123457"
$barcode->getFormat(); // BarcodeFormat::EAN13

use Nexus\Product\ValueObjects\DimensionSet;
use Nexus\Uom\ValueObjects\Quantity;

$dimensions = new DimensionSet(
    weight: new Quantity(2.5, 'kg'),
    length: new Quantity(30, 'cm'),
    width: new Quantity(20, 'cm'),
    height: new Quantity(15, 'cm')
);

$dimensions->toArray();
// [
//     'weight' => ['value' => 2.5, 'unit' => 'kg'],
//     'length' => ['value' => 30, 'unit' => 'cm'],
//     ...
// ]

enum ProductType: string {
    case STORABLE = 'storable';     // Physical goods with inventory tracking
    case CONSUMABLE = 'consumable'; // Items consumed without stock tracking
    case SERVICE = 'service';       // Intangible services
}

enum TrackingMethod: string {
    case NONE = 'none';                   // No tracking
    case LOT_NUMBER = 'lot_number';       // Batch/lot tracking
    case SERIAL_NUMBER = 'serial_number'; // Unique instance tracking
}

enum BarcodeFormat: string {
    case EAN13 = 'ean13';       // European Article Number (13 digits)
    case UPCA = 'upca';         // Universal Product Code (12 digits)
    case CODE128 = 'code128';   // High-density alphanumeric
    case QR = 'qr';             // QR Code (2D)
    case CUSTOM = 'custom';     // Custom format
}