You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
If you are working with a class which implements the `TabularDataReader` interface you can use this functionality
49
-
directly by calling the `TabularDataReader::getObjects` method.
50
-
51
-
Here's an example using the `Reader` class which implements the `TabularDataReader` interface:
52
-
53
-
```php
54
-
use League\Csv\Reader;
55
-
56
-
$csv = Reader::createFromString($document);
57
-
$csv->setHeaderOffset(0);
58
-
foreach ($csv->getObjects(Weather::class) as $weather) {
59
-
// each $weather entry will be an instance of the Weather class;
60
-
}
61
-
```
62
-
63
-
In the following sections we will explain the conversion and how it can be configured.
65
+
All classes are defined under the `League\Csv\Serializer` namespace.
64
66
65
67
## Prerequisite
66
68
@@ -117,7 +119,7 @@ To get instances of your object, you now can call one of the `Denormalizer` meth
117
119
118
120
```php
119
121
use League\Csv\Reader;
120
-
use League\Csv\Denormalizer
122
+
use League\Csv\Serializer\Denormalizer
121
123
122
124
$csv = Reader::createFromString($document);
123
125
$csv->setHeaderOffset(0);
@@ -157,22 +159,9 @@ the following type:
157
159
158
160
the `nullable` aspect of the property is also automatically handled.
159
161
160
-
To complete the conversion you can use the `Cell` attribute. This attribute will override
161
-
the automatic resolution and enable fine-tuning type casting. It can be used on class
162
-
properties and methods regardless of their visibility.
163
-
164
-
The attribute can take up to three (3) arguments which are all optional:
165
-
166
-
- The `offset` argument tells the engine which record key to use via its numeric or name offset. If not present the property name or the name of the first argument of the `setter` method will be used. In such case, you are required to specify the property names information.
167
-
- The `cast` argument which accept the name of a class implementing the `TypeCasting` interface and responsible for type casting the record value. If not present, the mechanism will try to resolve the typecasting based on the propery or method argument type.
168
-
- The `castArguments` argument enables controlling typecasting by providing extra arguments to the `TypeCasting` class constructor. The argument expects an associative array and relies on named arguments to inject its value to the `TypeCasting` implementing class constructor.
169
-
170
-
<pclass="message-warning">The <code>reflectionProperty</code> key can not be used with the
171
-
<code>castArguments</code> as it is a reserved argument used by the <code>TypeCasting</code> class.</p>
172
-
173
-
In any case, if type casting fails, an exception will be thrown.
162
+
To complete the conversion you can use the `Cell` attribute.
174
163
175
-
Here's an example of how the attribute could be used:
164
+
Here's an example of how the attribute can be used:
176
165
177
166
```php
178
167
use League\Csv\Serializer;
@@ -195,6 +184,20 @@ The above rule can be translated in plain english like this:
195
184
> using the date format `!Y-m-d` and the `Africa/Nairobi` timezone. Once created,
196
185
> inject the instance into the class private property `observedOn`.
197
186
187
+
This attribute will override the automatic resolution and enable fine-tuning type casting.
188
+
It can be used on class properties and methods regardless of their visibility.
189
+
190
+
The attribute can take up to three (3) arguments which are all optional:
191
+
192
+
- The `offset` argument tells the engine which record key to use via its numeric or name offset. If not present the property name or the name of the first argument of the `setter` method will be used. In such case, you are required to specify the property names information.
193
+
- The `cast` argument which accept the name of a class implementing the `TypeCasting` interface and responsible for type casting the record value. If not present, the mechanism will try to resolve the typecasting based on the propery or method argument type.
194
+
- The `castArguments` argument enables controlling typecasting by providing extra arguments to the `TypeCasting` class constructor. The argument expects an associative array and relies on named arguments to inject its value to the `TypeCasting` implementing class constructor.
195
+
196
+
<pclass="message-warning">The <code>reflectionProperty</code> key can not be used with the
197
+
<code>castArguments</code> as it is a reserved argument used by the <code>TypeCasting</code> class.</p>
198
+
199
+
In any case, if type casting fails, an exception will be thrown.
200
+
198
201
### Handling the empty string
199
202
200
203
Out of the box the `Denormalizer` makes no distinction between an empty string and the `null` value.
@@ -209,7 +212,7 @@ before typecasting whereas `Denormalizer::disallowEmptyStringAsNull` will mainta
209
212
Using these methods will affect the `Denormalizer` usage throughout your codebase.
210
213
211
214
```php
212
-
use League\Csv\Denormalizer;
215
+
use League\Csv\Serializer\Denormalizer;
213
216
214
217
$record = [
215
218
'date' => '2023-10-30',
@@ -237,8 +240,6 @@ All the built-in methods support the `nullable` and the `mixed` types.
237
240
238
241
For scalar conversion, type casting is done via PHP's `ext-filter` extension.
239
242
240
-
All classes are defined under the `League\Csv\Serializer` namespace.
241
-
242
243
### CastToString
243
244
244
245
Converts the array value to a string or `null` depending on the property type information. The class takes one
@@ -365,7 +366,7 @@ any built-in type or a specific class.
365
366
366
367
```php
367
368
use App\Domain\Money;
368
-
use League\Csv\Denormalizer;
369
+
use League\Csv\Serializer\Denormalizer;
369
370
370
371
$typeCasting = function (
371
372
?string $value,
@@ -406,7 +407,7 @@ to the `int` type. If you still wish to use the `CastToInt` class you are requir
406
407
explicitly declare it via the `Cell` attribute `cast` argument.
407
408
408
409
```php
409
-
use League\Csv\Denormalizer;
410
+
use League\Csv\Serializer\Denormalizer;
410
411
411
412
Denormalizer::registerType('int', fn (?string $value): int => 42);
0 commit comments