Skip to content
This repository has been archived by the owner on Jan 30, 2020. It is now read-only.

Prepare develop branch to target PHP 7.2 #80

Merged
merged 35 commits into from
Nov 29, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
b3a3de7
Update dependencies
weierophinney Nov 19, 2018
7b6d85e
Add typehints wherever possible
weierophinney Nov 19, 2018
583cd24
Incorporate changes requested by @vaclavvanik
weierophinney Nov 20, 2018
722b1e4
Update mkdocs.yml
weierophinney Nov 20, 2018
0d07c0d
Adds phpstan into CI toolchain
weierophinney Nov 26, 2018
554d32d
Fix errors reported by phpstan
weierophinney Nov 26, 2018
553d40b
Versions documentation
weierophinney Nov 26, 2018
8ab8be9
Set explicit `object` param/return types in event implementations
weierophinney Nov 26, 2018
042ce54
Use `object` return type for `HydratorListener::onHydrate()`
weierophinney Nov 26, 2018
fbcbebf
Adds migration document for v3
weierophinney Nov 26, 2018
6334c28
Revert rename of `pages` to `nav` in `mkdocs.yml`
weierophinney Nov 26, 2018
e8cc238
Fix errors flagged by phpstan
weierophinney Nov 27, 2018
c867dc0
Use a dist file for phpstan
weierophinney Nov 27, 2018
60f8dc4
Do not iterate if value is null
weierophinney Nov 27, 2018
28d63e7
Use method_exists + is_callable
weierophinney Nov 27, 2018
a1225fd
Document all iterable and array parameters, return values, and proper…
weierophinney Nov 27, 2018
b81c4ac
Remove unnecessary whitespace in AggregateHydrator
weierophinney Nov 27, 2018
65a93dc
Use the IdentityNamingStrategy as a default naming strategy if none i…
weierophinney Nov 27, 2018
085c710
Import all global functions
weierophinney Nov 27, 2018
e3fafb1
Provide documentation for the HydratorPluginManagerFactory
weierophinney Nov 28, 2018
03c7599
Removes unused import in StrategyChain
weierophinney Nov 28, 2018
87d074b
Note that the `$hydrator` property is nullable in HydratorAwareTrait
weierophinney Nov 28, 2018
5e85841
Final CS pass
weierophinney Nov 28, 2018
deeaeef
Use PHP 7.3, not nightly, in test matrix
weierophinney Nov 28, 2018
4850813
Remove unnecessary ternary when setting `$explodeLimit`
weierophinney Nov 28, 2018
b6115d7
DomainException already implements ExceptionInterface
weierophinney Nov 28, 2018
800dc12
Remove unnecessary property declarations in HydratingArrayIterator
weierophinney Nov 28, 2018
7f62d77
Use null coalesce instead of ternary within ArrayMapNamingStrategy
weierophinney Nov 28, 2018
125194c
Use null coalesce within CompositeNamingStrategy instead of ternary
weierophinney Nov 28, 2018
33e18e1
Remove third argument to `substr` within UnderscoreToCamelCaseFilter
weierophinney Nov 28, 2018
b0babc4
Incorporate feedback from @webimpress
weierophinney Nov 28, 2018
046a2ea
Correct typehint for NamingStrategyInterface::extract `$object` argument
weierophinney Nov 28, 2018
1f2e3f7
Updates license docblocks for all test classes and assets
weierophinney Nov 28, 2018
e48e508
Remove `lcfirst` import in UnderscoreToCamelCaseFilter
weierophinney Nov 28, 2018
b7b5b9a
Sorting of imports when get_class/gettype are used
weierophinney Nov 28, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
151 changes: 6 additions & 145 deletions docs/book/aggregate.md
Original file line number Diff line number Diff line change
@@ -1,145 +1,6 @@
# AggregateHydrator
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Were these docs moved with git mv, before the redorects were put in place? Asking to avoid destroying git blame output

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, but because the files still exist, the change tracking gets all messed up.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If it's only github messing up the UI, that's fine, just needed to make sure of it 👌


`Zend\Hydrator\Aggregate\AggregateHydrator` is an implementation of
`Zend\Hydrator\HydratorInterface` that composes multiple hydrators via event listeners.

You typically want to use an aggregate hydrator when you want to hydrate or extract data from
complex objects that implement multiple interfaces, and therefore need multiple hydrators to handle
that in subsequent steps.

## Installation requirements

The `AggregateHydrator` depends on the zend-eventmanager component, so be sure to have it
installed before getting started:

```bash
$ composer require zendframework/zend-eventmanager
```

## Basic usage

A simple use case may be hydrating a `BlogPost` object, which contains data for the user that
created it, the time it was created, the current publishing status, etc:

```php
use Zend\Hydrator\Aggregate\AggregateHydrator;

$hydrator = new AggregateHydrator();

// attach the various hydrators capable of handling simpler interfaces
$hydrator->add(new My\BlogPostHydrator());
$hydrator->add(new My\UserAwareObjectHydrator());
$hydrator->add(new My\TimestampedObjectHydrator());
$hydrator->add(new My\PublishableObjectHydrator());
// ...

// Now retrieve the BlogPost object
// ...

// you can now extract complex data from a blogpost
$data = $hydrator->extract($blogPost);

// or you can fill the object with complex data
$blogPost = $hydrator->hydrate($data, $blogPost);
```

> ### Hydrator priorities
>
> `AggregateHydrator::add` has a second optional argument, `$priority`. If you
> have two or more hydrators that conflict with each other for same data keys,
> you may decide which one to execute first or last by passing a higher or lower
> integer priority, respectively, to this argument.

In order to work with this logic, each of the hydrators that are attached should
ignore any unknown object type passed in:

```php
namespace My;

use Zend\Hydrator\HydratorInterface

class BlogPostHydrator implements HydratorInterface
{
public function hydrate($data, $object)
{
if (! $object instanceof BlogPost) {
return $object;
}

// ... continue hydration ...
}

public function extract($object)
{
if (! $object instanceof BlogPost) {
return array();
}

// ... continue extraction ...
}
}
```

## Advanced use cases

Since the `AggregateHydrator` is event-driven, you can use the `EventManager`
API to tweak its behaviour.

Common use cases include:

- Removal of hydrated data keys (passwords/confidential information) depending
on business rules.
- Caching of the hydration/extraction process.
- Transformations on extracted data, for compatibility with third-party APIs.

In the following example, a cache listener is introduced to speed up hydration, which can be
very useful when the same data is requested multiple times:

```php
use Zend\Hydrator\Aggregate\AggregateHydrator;
use Zend\Hydrator\Aggregate\ExtractEvent;
use Zend\Cache\Storage\Adapter\Memory;

$hydrator = new AggregateHydrator();

// Attach the various hydrators:
$hydrator->add(new My\BlogPostHydrator());
$hydrator->add(new My\UserAwareObjectHydrator());
$hydrator->add(new My\TimestampedObjectHydrator());
$hydrator->add(new My\PublishableObjectHydrator());
// ...

$cache = new Memory();
$cacheReadListener = function (ExtractEvent $event) use ($cache) {
$object = $event->getExtractionObject();

if (! $object instanceof BlogPost) {
return;
}

if ($cache->hasItem($object->getId())) {
$event->setExtractedData($cache->getItem($object->getId()));
$event->stopPropagation();
}
};

$cacheWriteListener = function (ExtractEvent $event) use ($cache) {
$object = $event->getExtractionObject();

if (! $object instanceof BlogPost) {
return;
}

$cache->setItem($object->getId(), $event->getExtractedData());
};

// Attaching a high priority listener executed before extraction logic:
$eventManager = $hydrator->getEventManager();
$eventManager()->attach(ExtractEvent::EVENT_EXTRACT, $cacheReadListener, 1000);

// Attaching a low priority listener executed after extraction logic:
$eventManager()->attach(ExtractEvent::EVENT_EXTRACT, $cacheWriteListener, -1000);
```

With an aggregate hydrator configured in this way, any
`$hydrator->extract($blogPost)` operation will be cached.
<noscript><meta http-equiv="refresh" content="0; url=/zend-hydrator/v2/aggregate/"></noscript>
<script>
document.addEventListener("DOMContentLoaded", function (event) {
window.location.pathname = '/zend-hydrator/v2/aggregate/';
});
</script>
Loading