Skip to content

Adding inspection engine#575

Open
nyamsprod wants to merge 8 commits intomasterfrom
feature/inspection-engine
Open

Adding inspection engine#575
nyamsprod wants to merge 8 commits 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\FieldList;
use League\Csv\Schema\StringField;
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(
        StringField::cases(confidenceThreshold: .8),
        new EnumField(enumClass: Gender::class, confidenceThreshold: .6),
    );
$inspector = new Inspector(fieldList: $fields, sampleLimit: 10);

$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::String
    $column->name(), // 'string(uuid)'
);


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
@nyamsprod nyamsprod force-pushed the feature/inspection-engine branch from 9e23a78 to 0026985 Compare May 8, 2026 14:53
@nyamsprod nyamsprod force-pushed the feature/inspection-engine branch from 80cf2ab to 7f26820 Compare May 9, 2026 12:07
@nyamsprod nyamsprod force-pushed the feature/inspection-engine branch from 7008012 to cc6d4bb Compare May 9, 2026 16:26
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