Skip to content

Commit

Permalink
Describe strict mode, rearrange info
Browse files Browse the repository at this point in the history
  • Loading branch information
samdark committed Jan 12, 2024
1 parent eab3cfb commit 5026083
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 23 deletions.
4 changes: 2 additions & 2 deletions docs/guide/en/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
The package provides a way to create and hydrate objects from a set of raw data.

- [General usage](general-usage.md)
- [Type casting](typecasting.md)
- [Mapping](mapping.md)
- [Configuration with PHP attributes](configuration-with-attributes.md)
- [Creating own attributes](creating-own-attributes.md)
- Attribute reslover factory
- Object factory
- [Mapping](mapping.md)
- [Type casting](typecasting.md)
14 changes: 0 additions & 14 deletions docs/guide/en/configuration-with-attributes.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,6 @@

You can configure how the hydrator creates or hydrates a specific class using attributes.

## Skipping hydration

To skip hydration of a specific property, use `SkipHydration` attribute:

```php
use \Yiisoft\Hydrator\Attribute\SkipHydration;

class MyClass
{
#[SkipHydration]
private $property;
}
```

## Resolving dependencies

To resolve dependencies by specific ID using DI container, use `Di` attribute:
Expand Down
2 changes: 0 additions & 2 deletions docs/guide/en/general-usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,5 +53,3 @@ $object = $hydrator->create(Car::class, [

That would pass the `name` constructor argument of the `Car` object and create a new `Engine` object for `engine`
argument passing `V8` as the `name` argument to its constructor.

// TODO: strict mode
35 changes: 32 additions & 3 deletions docs/guide/en/mapping.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,33 @@ $data = ['header' => 'First post', 'text' => 'Hello, world!'];
Hydrator allows you to map data:

```php
$hydrator = new Hydrator();
use Yiisoft\Hydrator\Hydrator;
use Yiisoft\Hydrator\ArrayData;

$object = new SimpleClass();
$hydrator = new Hydrator();

$map = ['title' => 'header', 'body' => 'text'],;
$hydrator->hydrate($object, new ArrayData($data, $map));
$post = $hydrator->create(Post::class, new ArrayData($data, $map));
```

This way we take `header` key for `title` and `text` key for `body`.

## Strict mode

You can enable strict mode by passing `true` as a third argument of `ArrayData`:

```php
use Yiisoft\Hydrator\Hydrator;
use Yiisoft\Hydrator\ArrayData;

$hydrator = new Hydrator();

$map = ['title' => 'header', 'body' => 'text'],;
$post = $hydrator->create(Post::class, new ArrayData($data, $map, true));
```

In this case, keys absent from the map are ignored so everything should be mapped explicitly.

## Using attributes

Alternatively to specifying mapping as an array, you can use `Data` attribute to define mapping inline:
Expand All @@ -65,3 +82,15 @@ $person = $hydrator->create(Person::class, [
'last_name' => 'Doe',
]);
```

To skip hydration of a specific property, use `SkipHydration` attribute:

```php
use \Yiisoft\Hydrator\Attribute\SkipHydration;

class MyClass
{
#[SkipHydration]
private $property;
}
```
2 changes: 0 additions & 2 deletions docs/guide/en/typecasting.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,5 +113,3 @@ $money = $hydrator->create(Money::class, [
'currency' => 'AMD',
]);
```


0 comments on commit 5026083

Please sign in to comment.