Skip to content

Adding inspection engine#575

Open
nyamsprod wants to merge 1 commit intomasterfrom
feature/inspection-engine
Open

Adding inspection engine#575
nyamsprod wants to merge 1 commit intomasterfrom
feature/inspection-engine

Conversation

@nyamsprod
Copy link
Copy Markdown
Member

@nyamsprod nyamsprod commented Apr 19, 2026

This PR introduces a Inspection engine to try to infer the CSV schema a quick usage will be more useful than a long text:

<?php

declare(strict_types=1);

use League\Csv\Schema\Inspector;
use League\Csv\Schema\EnumField;
use League\Csv\Schema\RegexpField;
use League\Csv\Schema\FieldList;
use League\Csv\Reader;

require __DIR__ . '/vendor/autoload.php';

enum Gender
{
    case M;
    case F;
}

$doc = <<<DOC
name;age;city;id;sexe
Alice;25;New York;3391f7c0-d059-4e90-a73a-14f3140c6870;F
Bob;30;London;;M
Charlie;20;Berlin;3391f7c0-d059-4e90-a73a-14f3140c6870;R
David;35;Paris;42fe384c-9dab-483c-b8e2-44c73a5e9043;F
Frank;40;Tokyo;42fe384c-9dab-483c-b8e2-44c73a5e9043;
DOC;

$fields = FieldList::default()
    ->append(
        RegexpField::uuid(.5),
        new EnumField(Gender::class, .6)
    );
$inspector = Inspector::default()->withFields($fields);

$document = Reader::fromPath('data://text/csv;base64,' . base64_encode($doc));
$document->setHeaderOffset(0);
$document->setDelimiter(';');
$schema = $document->inferSchema($inspector);

$column = $schema->get('age');
var_dump(
    $column->type(), // FieldType::Numeric
    $column->name(), // 'numeric'
);

$column = $schema->get('id');
var_dump(
    $column->type(),            // FieldType::StructuredString
    $column->name(),            // 'uuid'
    $column->metadata->all(),   // ["regexp" => "/^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i"]
);


foreach ($document->inferRecords($inspector) as $record) {
    // the $record values are cast according the the 
    // schema info or null is returned if the raw data can not be parsed.
}

This is a work in progress, feedback and criticism are welcomed!!

@nyamsprod nyamsprod self-assigned this Apr 19, 2026
@nyamsprod nyamsprod force-pushed the feature/inspection-engine branch 5 times, most recently from 4dfb68c to b751f4a Compare April 26, 2026 12:49
@nyamsprod nyamsprod force-pushed the feature/inspection-engine branch 4 times, most recently from 1b5994c to f81333a Compare April 29, 2026 06:33
@nyamsprod nyamsprod force-pushed the feature/inspection-engine branch from f81333a to d57f115 Compare April 29, 2026 06:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant