Skip to content

Commit bdac6d1

Browse files
committed
Improve record type inference documentation
1 parent 8fe3687 commit bdac6d1

1 file changed

Lines changed: 30 additions & 23 deletions

File tree

docs/9.0/reader/record-type-inference.md

Lines changed: 30 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ description: Detect your CSV field type using an ergonomic and deterministic typ
66

77
# Schema inference
88

9+
<p class="message-notice">Added in version <code>9.29.0</code>.</p>
10+
911
CSV files do not contain type information: every value is initially read as text. Schema inference allows you to inspect a CSV and automatically determine the most appropriate type for each column.
1012

1113
The inferred schema can then be used to parse the records into their corresponding PHP values.
@@ -20,10 +22,10 @@ id,name,active,amount
2022

2123
schema inference can determine that:
2224

23-
-`id` is numeric;
24-
-`name` is a string;
25-
-`active` is boolean;
26-
-`amount` is numeric.
25+
- `id` is numeric;
26+
- `name` is a string;
27+
- `active` is boolean;
28+
- `amount` is numeric.
2729

2830
The feature is available through `Reader::inferSchema()` and `Reader::inferRecords()`.
2931

@@ -137,7 +139,7 @@ For example, with a sample limit of `100`, the first 100 records are used to det
137139

138140
The default sample size is `10`.
139141

140-
## Controlling the inferred types
142+
### Controlling the inferred types
141143

142144
The `Inspector` also controls which field types are considered during inference.
143145

@@ -168,7 +170,7 @@ The available built-in field types cover common values such as booleans, numbers
168170

169171
Only the fields included in the `FieldList` are considered during schema inference.
170172

171-
## Built-in field types
173+
#### Built-in field types
172174

173175
The inspection engine provides a field implementation for each supported field type:
174176

@@ -210,7 +212,7 @@ $fieldList = FieldList::default()
210212

211213
This distinction is important: **a field being available does not mean that it is automatically used during inference**. Only fields present in the `FieldList` are considered by the `Inspector`.
212214

213-
## Custom field types
215+
#### Custom field types
214216

215217
If the built-in field types do not cover a particular kind of value, a custom field can be added to the inspector.
216218

@@ -240,7 +242,7 @@ $inspector = Inspector::default()
240242

241243
The custom field will then participate in schema inference alongside the built-in fields.
242244

243-
## Choosing between the methods
245+
### Choosing between the methods
244246

245247
Use `inferSchema()` when you need to **inspect, reuse, or modify the inferred schema**:
246248

@@ -262,9 +264,9 @@ In both cases, schema inference is performed from a sample of the CSV and the re
262264

263265
A Tabular data provides three ways to iterate over its records:
264266

265-
-`getRecords()` reads the values as they appear in the CSV;
266-
-`getRecordsAsObject()` converts each record into a specific object;
267-
-`inferRecords()` infers the types of the columns and parses the records accordingly.
267+
- `getRecords()` reads the values as they appear in the CSV;
268+
- `getRecordsAsObject()` converts each record into a specific object;
269+
- `inferRecords()` infers the types of the columns and parses the records accordingly.
268270

269271
The differences become clearer when the same data is read using each method.
270272

@@ -308,7 +310,7 @@ David;35;Paris;42fe384c-9dab-483c-b8e2-44c73a5e9043;R
308310

309311
This value is useful for illustrating the difference between the three APIs.
310312

311-
### `getRecords()`: read the CSV as-is
313+
### Read the CSV as-is
312314

313315
`getRecords()` does not interpret the values.
314316

@@ -332,7 +334,7 @@ No validation or type conversion takes place.
332334

333335
Use `getRecords()` when you want to handle the CSV values yourself.
334336

335-
### `inferRecords()`: infer the types
337+
### Infer the types
336338

337339
`inferRecords()` examines the CSV and builds a schema from the values it finds.
338340

@@ -356,7 +358,9 @@ The `gender` value remains a string because the default field list does not know
356358

357359
This illustrates an important property of inference:
358360

359-
> `inferRecords()` only applies the types that can be inferred from the configured field list.
361+
<p class="message-notice">
362+
<code>inferRecords()</code> only applies the types that can be inferred from the configured field list.
363+
</p>
360364

361365
#### Inferring the schema
362366

@@ -431,7 +435,7 @@ In short:
431435
- **`inferSchema()`** tells you *what the CSV is understood to contain*.
432436
- **`inferRecords()`** gives you the records *according to that understanding*.
433437

434-
### `inferRecords()` with a custom inspector
438+
### Using a custom inspector
435439

436440
The inference can be configured when the CSV contains domain-specific types.
437441

@@ -475,7 +479,7 @@ Because `"R"` is not a valid case of `Gender`, the `EnumField` cannot parse it a
475479

476480
This is an important difference from `getRecordsAsObject()`: **inferred parsing is tolerant of values that cannot be parsed by the inferred field**.
477481

478-
### `getRecordsAsObject()`: enforce a known structure
482+
### Enforce a known structure
479483

480484
When the target object is known, `getRecordsAsObject()` can explicitly define how each column should be converted.
481485

@@ -532,14 +536,17 @@ The difference is not simply about type conversion. Each method has a different
532536
| `inferRecords()` | Infer and parse data | Discovered from CSV | Parsed value or `null` |
533537
| `getRecordsAsObject()` | Map to a known object | Explicitly defined | Exception |
534538

535-
#### Which one should I use?
536-
537-
Use **`getRecords()`** when you want the raw CSV values and will handle conversion yourself.
538-
539-
Use **`inferRecords()`** when you want the library to discover useful types from the CSV without having to define a complete data model.
540539

541-
Use **`getRecordsAsObject()`** when you have a known object model and want the CSV to be converted according to that model, with conversion failures treated as errors.
540+
<div class="message-notice">
541+
<ul>
542+
<li>Use <code>getRecords()</code> when you want the raw CSV values and will handle conversion yourself.</li>
543+
<li>Use <code>inferRecords()</code> when you want the library to discover useful types from the CSV without having to define a complete data model.</li>
544+
<li>Use <code>getRecordsAsObject()</code> when you have a known object model and want the CSV to be converted according to that model, with conversion failures treated as errors.</li>
545+
</ul>
546+
</div>
542547

543548
In summary:
544549

545-
> **`getRecords()` preserves the input, `inferRecords()` interprets the input, and `getRecordsAsObject()` validates the input against an explicit object model.**
550+
<p class="message-warning">
551+
<code>getRecords()</code> preserves the input, <code>inferRecords()</code> interprets the input, and <code>getRecordsAsObject()</code> validates the input against an explicit object model.
552+
</p>

0 commit comments

Comments
 (0)