Skip to content

Commit 6e9f9b8

Browse files
committed
Move everything under the Serializer namepsace
1 parent 456cd16 commit 6e9f9b8

File tree

5 files changed

+46
-60
lines changed

5 files changed

+46
-60
lines changed

docs/9.0/reader/record-mapping.md

Lines changed: 39 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,23 @@ title: Denormalize a Tabular Data record into an object
77

88
<p class="message-notice">New in version <code>9.12.0</code></p>
99

10+
If you are working with a class which implements the `TabularDataReader` interface you can use this functionality
11+
directly by calling the `TabularDataReader::getObjects` method.
12+
13+
Here's an example using the `Reader` class which implements the `TabularDataReader` interface:
14+
15+
```php
16+
use League\Csv\Reader;
17+
18+
$csv = Reader::createFromString($document);
19+
$csv->setHeaderOffset(0);
20+
foreach ($csv->getObjects(Weather::class) as $weather) {
21+
// each $weather entry will be an instance of the Weather class;
22+
}
23+
```
24+
25+
In the following sections we will explain the mechanism use and how you can control it.
26+
1027
## Assign an array to an object
1128

1229
To work with objects instead of arrays the `Denormalizer` class is introduced to expose a
@@ -18,7 +35,7 @@ The class exposes four (4) methods to ease `array` to `object` conversion:
1835
- `Denormalizer::denormalize` and `Denormalizer::assign` which convert a single record into a new instance of the specified class.
1936

2037
```php
21-
use League\Csv\Denormalizer;
38+
use League\Csv\Serializer\Denormalizer;
2239

2340
$record = [
2441
'date' => '2023-10-30',
@@ -45,22 +62,7 @@ foreach (Denormalizer::assignAll(Weather::class, $collection, ['date', 'temperat
4562
}
4663
```
4764

48-
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.
6466

6567
## Prerequisite
6668

@@ -117,7 +119,7 @@ To get instances of your object, you now can call one of the `Denormalizer` meth
117119

118120
```php
119121
use League\Csv\Reader;
120-
use League\Csv\Denormalizer
122+
use League\Csv\Serializer\Denormalizer
121123

122124
$csv = Reader::createFromString($document);
123125
$csv->setHeaderOffset(0);
@@ -157,22 +159,9 @@ the following type:
157159

158160
the `nullable` aspect of the property is also automatically handled.
159161

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-
<p class="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.
174163

175-
Here's an example of how the attribute could be used:
164+
Here's an example of how the attribute can be used:
176165

177166
```php
178167
use League\Csv\Serializer;
@@ -195,6 +184,20 @@ The above rule can be translated in plain english like this:
195184
> using the date format `!Y-m-d` and the `Africa/Nairobi` timezone. Once created,
196185
> inject the instance into the class private property `observedOn`.
197186
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+
<p class="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+
198201
### Handling the empty string
199202

200203
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
209212
Using these methods will affect the `Denormalizer` usage throughout your codebase.
210213

211214
```php
212-
use League\Csv\Denormalizer;
215+
use League\Csv\Serializer\Denormalizer;
213216

214217
$record = [
215218
'date' => '2023-10-30',
@@ -237,8 +240,6 @@ All the built-in methods support the `nullable` and the `mixed` types.
237240

238241
For scalar conversion, type casting is done via PHP's `ext-filter` extension.
239242

240-
All classes are defined under the `League\Csv\Serializer` namespace.
241-
242243
### CastToString
243244

244245
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.
365366

366367
```php
367368
use App\Domain\Money;
368-
use League\Csv\Denormalizer;
369+
use League\Csv\Serializer\Denormalizer;
369370

370371
$typeCasting = function (
371372
?string $value,
@@ -406,7 +407,7 @@ to the `int` type. If you still wish to use the `CastToInt` class you are requir
406407
explicitly declare it via the `Cell` attribute `cast` argument.
407408

408409
```php
409-
use League\Csv\Denormalizer;
410+
use League\Csv\Serializer\Denormalizer;
410411

411412
Denormalizer::registerType('int', fn (?string $value): int => 42);
412413
```

src/Reader.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Closure;
1818
use Iterator;
1919
use JsonSerializable;
20+
use League\Csv\Serializer\Denormalizer;
2021
use League\Csv\Serializer\MappingFailed;
2122
use League\Csv\Serializer\TypeCastingFailed;
2223
use SplFileObject;

src/ResultSet.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use Generator;
2020
use Iterator;
2121
use JsonSerializable;
22+
use League\Csv\Serializer\Denormalizer;
2223
use League\Csv\Serializer\MappingFailed;
2324
use League\Csv\Serializer\TypeCastingFailed;
2425
use LimitIterator;

src/Denormalizer.php renamed to src/Serializer/Denormalizer.php

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,24 +11,11 @@
1111

1212
declare(strict_types=1);
1313

14-
namespace League\Csv;
14+
namespace League\Csv\Serializer;
1515

1616
use Closure;
1717
use Iterator;
18-
use League\Csv\Serializer\CastToArray;
19-
use League\Csv\Serializer\CastToBool;
20-
use League\Csv\Serializer\CastToDate;
21-
use League\Csv\Serializer\CastToEnum;
22-
use League\Csv\Serializer\CastToFloat;
23-
use League\Csv\Serializer\CastToInt;
24-
use League\Csv\Serializer\CastToString;
25-
use League\Csv\Serializer\Cell;
26-
use League\Csv\Serializer\ClosureCasting;
27-
use League\Csv\Serializer\MappingFailed;
28-
use League\Csv\Serializer\PropertySetter;
29-
use League\Csv\Serializer\Type;
30-
use League\Csv\Serializer\TypeCasting;
31-
use League\Csv\Serializer\TypeCastingFailed;
18+
use League\Csv\MapIterator;
3219
use ReflectionAttribute;
3320
use ReflectionClass;
3421
use ReflectionException;
@@ -50,7 +37,7 @@ final class Denormalizer
5037
private readonly ReflectionClass $class;
5138
/** @var array<ReflectionProperty> */
5239
private readonly array $properties;
53-
/** @var non-empty-array<PropertySetter> */
40+
/** @var array<PropertySetter> */
5441
private readonly array $propertySetters;
5542

5643
/**
@@ -201,7 +188,7 @@ private function setClass(string $className): ReflectionClass
201188
*
202189
* @throws MappingFailed
203190
*
204-
* @return non-empty-array<PropertySetter>
191+
* @return array<PropertySetter>
205192
*/
206193
private function setPropertySetters(array $propertyNames): array
207194
{

src/DenormalizerTest.php renamed to src/Serializer/DenormalizerTest.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,12 @@
1111

1212
declare(strict_types=1);
1313

14-
namespace League\Csv;
14+
namespace League\Csv\Serializer;
1515

1616
use Countable;
1717
use DateTime;
1818
use DateTimeImmutable;
1919
use DateTimeInterface;
20-
use League\Csv\Serializer\CastToDate;
21-
use League\Csv\Serializer\CastToEnum;
22-
use League\Csv\Serializer\Cell;
23-
use League\Csv\Serializer\MappingFailed;
2420
use PHPUnit\Framework\TestCase;
2521
use SplFileObject;
2622
use stdClass;

0 commit comments

Comments
 (0)