PHP code example of shmandalf / excelentor

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

    

shmandalf / excelentor example snippets



Shmandalf\Excelentor\Attributes\{Header, Column};
use Shmandalf\Excelentor\Parser;
use Shmandalf\Excelentor\ValidatorFactory;

// Excel like column names, can be numeric indexes instead or even skipped
#[Header(columns: ['A' => 'name', 'B' => 'email', 'C' => 'age'])]
class ApprenticeWizard
{
    #[Column(rule: 'r a reason
        // it can be configured in the Header attribute
        // by passing the $rows argument
        'Some header',
        'Name',
        'Here',
    ],
    // Demo data
    [
        'Melissa',
        '[email protected]',
        45,
    ],
    [
        'Lera',
        '[email protected]',
        46,
    ],
    [
        'Anastasia',
        '[email protected]',
        47,
    ],
    [
        'Dmitry',
        '[email protected]',
        48,
    ],
];

// Cast the parsing incantation on the ancient scroll!
$parser = new Parser(ApprenticeWizard::class, new ValidatorFactory('ru'));

// Scry for cursed runes and magical anomalies
$errors = $parser->validateAll($spreadsheetData);
// The scroll is pure, magical currents are stable
foreach ($errors as $error) {
    echo "Arcane disturbance: " . $error->getMessage() .
        " at spell line " . $error->getLineNo() . "\n";
}

// Summon entities from the magical parchment!
// Entries with dark enchantments vanish into the mist
$wizards = $parser->parse($spreadsheetData);
foreach ($wizards as $wizard) {
    echo "{$wizard->getName()} has sworn the Oath of Arcana!\n";
}

// Supports excel-style column names
#[Header(columns: ['A' => 'sku', 'B' => 'price', 'C' => 'stock'])]

// You can even skip certain columns if you want
#[Header(columns: ['B' => 'sku', 'AB' => 'stock'])]

// For files without a header row
#[NoHeader(columns: [0 => 'name', 1 => 'email'])]

// Or simply
#[NoHeader(columns: ['name', 'email'])]

#[Column] public string $name;   // "John" → "John"
#[Column] public int $age;       // "30" → 30
#[Column] public float $price;   // "99.99" → 99.99
#[Column] public bool $active;   // "true" → true
#[Column] public Carbon $date;   // "2023-01-15" → Carbon instance
#[Column] public ?float $score;  // "" → null (nullable!)

// not sure about this unique rule yet haha
#[Column(rule: '(rule: '

use Shmandalf\Excelentor\Attributes\{Header, Column};
use Carbon\Carbon;

#[Header(columns: [
    'A' => 'sku',
    'B' => 'name',
    'C' => 'price',
    'D' => 'in_stock',
    'E' => 'created_at'
])]
class ProductImport {
    #[Column(rule: 'c Carbon $created_at;
}

use Shmandalf\Excelentor\Parser;
use Shmandalf\Excelentor\ValidatorFactory;

$parser = new Parser(ProductImport::class, new ValidatorFactory());

// From array (Excel/CSV readers)
$data = [
    ['SKU', 'Name', 'Price', 'In Stock', 'Created At'],
    ['PROD-001', 'Laptop', '999.99', 'true', '2023-01-15'],
    ['PROD-002', 'Mouse', '49.50', 'false', '2023-02-20'],
];

// From CSV file (using League\Csv)
// $csv = Reader::createFromPath('products.csv');
// $data = $csv->getRecords();

foreach ($parser->parse($data) as $product) {
    // $product is a fully typed ProductImport object!
    echo "{$product->sku}: {$product->name} - \${$product->price}\n";
}

#[Header(columns: [
    'A' => 'sku',
    'D' => 'name',
    'F' => 'price',
    'G' => 'stock',
    'X' => 'active'
])]
class EcommerceImport {
    // unique? :) prolly not now
    #[Column(rule: 'e: '

#[NoHeader(columns: [0 => 'email', 1 => 'name', 2 => 'birth_date'])]
class UserRegistration {
    #[Column(rule: 'ule: '

#[Column(format: 'd/m/Y')]       // "15/01/2023" → Carbon
#[Column(format: 'm-d-Y')]       // "01-15-2023" → Carbon
#[Column(format: 'Y年m月d日')]    // "2023年01月15日" → Carbon

#[Column] public bool $flag; // Accepts: true/false, yes/no, 1/0, on/off, да/нет, +/-

// From PhpSpreadsheet
$spreadsheet = IOFactory::load('file.xlsx');
$data = $spreadsheet->getActiveSheet()->toArray();

// From League CSV
$csv = Reader::createFromPath('file.csv');
$data = $csv->getRecords();

// From database export
$data = $pdo->query('SELECT * FROM export')->fetchAll(PDO::FETCH_NUM);

// All work with the same parser!

#[Header(columns: [...])]
class StrictImport {
    // Parsing stops at first validation error
}