Skip to content

Commit

Permalink
Improve documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
nyamsprod committed Nov 16, 2023
1 parent c56aea4 commit 98929ac
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 17 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ composer require league/csv:^9.0

## Configuration

**Warning:** If your CSV document was created or is read on a **Legacy Macintosh computer**, add the following lines before
> [!TIP]
> If your CSV document was created or is read on a **Legacy Macintosh computer**, add the following lines before
using the library to help [PHP detect line ending](http://php.net/manual/en/function.fgetcsv.php#refsect1-function.fgetcsv-returnvalues).

```php
Expand All @@ -60,7 +61,8 @@ if (!ini_get('auto_detect_line_endings')) {
}
```

**The ini setting is deprecated since PHP version 8.1 and will be removed in PHP 9.0**
> [!WARNING]
> The ini setting is deprecated since PHP version 8.1 and will be removed in PHP 9.0**
## Testing

Expand Down
30 changes: 15 additions & 15 deletions docs/9.0/reader/record-mapping.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,6 @@ We can define a PHP DTO using the following properties.
```php
<?php

use League\Csv\Serializer\Cell;

final readonly class Weather
{
public function __construct(
Expand All @@ -80,7 +78,6 @@ an `Iterator` containing only instances of your specified class.

```php
use League\Csv\Reader;
use League\Csv\Serializer\Denormalizer

$csv = Reader::createFromString($document);
$csv->setHeaderOffset(0);
Expand Down Expand Up @@ -139,6 +136,9 @@ The attribute can take up to three (3) arguments which are all optional:
- 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.
- 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.

<p class="message-notice">You can use the mechanism on a CSV without a header row but it requires
adding a <code>Cell</code> attribute on each property or method needed for the conversion.</p>

<p class="message-warning">The <code>reflectionProperty</code> key can not be used with the
<code>castArguments</code> as it is a reserved argument used by the <code>TypeCasting</code> class.</p>

Expand All @@ -158,8 +158,8 @@ before typecasting whereas `Denormalizer::disallowEmptyStringAsNull` will preser
Using these methods will affect the results of the process throughout your codebase.

```php
use League\Csv\Serializer\Denormalizer;
use League\Csv\Reader;
use League\Csv\Serializer\Denormalizer;

$csv = Reader::createFromString($document);
$csv->setHeaderOffset(0);
Expand Down Expand Up @@ -223,9 +223,9 @@ as one the `Enum` name. The same logic applies for the `default` value. If the d
is not `null` and the value given is incorrect, the mechanism will throw an exception.

```php
use League\Csv\Serializer;
use League\Csv\Serializer\Cell;

#[Serializer\Cell(
#[Cell(
offset:1,
cast:Serializer\CastToEnum::class,
castArguments: ['default' => 'Galway', 'enum' => Place::class]
Expand Down Expand Up @@ -293,9 +293,9 @@ If you use the array shape `list` or `csv` you can also typecast the `array` con
optional `type` argument as shown below.

```php
use League\Csv\Serializer\Cell;
use League\Csv\Serializer;

#[Cell(
#[Serializer\Cell(
cast:Serializer\CastToArray::class,
castArguments: [
'shape' => 'csv',
Expand All @@ -321,7 +321,7 @@ any built-in type or a specific class.

```php
use App\Domain\Money\Naira;
use League\Csv\Serializer\Denormalizer;
use League\Csv\Serializer;

$castToNaira = function (?string $value, bool $isNullable, int $default = null): ?Naira {
if (null === $value && $isNullable) {
Expand All @@ -335,7 +335,7 @@ $castToNaira = function (?string $value, bool $isNullable, int $default = null):
return Naira::fromKobos(filter_var($value, FILTER_VALIDATE_INT));
};

Denormalizer::registerType(Naira::class, $castToNaira);
Serializer\Denormalizer::registerType(Naira::class, $castToNaira);
```

The `Denormalizer` will automatically call the closure for any `App\Domain\Money\Naira` conversion. You can
Expand All @@ -356,9 +356,9 @@ private ?Naira $amount;
In the following example, we redefine how to typecast to integer.

```php
use League\Csv\Serializer\Denormalizer;
use League\Csv\Serializer;

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

The closure will take precedence over the `CastToInt` class to convert
Expand All @@ -381,9 +381,9 @@ where:
To complete the feature you can use `Denormalizer::unregisterType` to remove a registered closure for a specific `type`.

```php
use League\Csv\Serializer\Denormalizer;
use League\Csv\Serializer;

Denormalizer::unregisterType(Naira::class);
Serializer\Denormalizer::unregisterType(Naira::class);
```

The two (2) methods are static.
Expand Down Expand Up @@ -460,7 +460,7 @@ final class CastToNaira implements TypeCasting
$this->isNullable = $reflectionType->allowsNull();
}

public function toVariable(?string $value): ?Money
public function toVariable(?string $value): ?Naira
{
try {
if (null === $value && $this->isNullable) {
Expand Down

0 comments on commit 98929ac

Please sign in to comment.