This repository has been archived by the owner on Jan 30, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 41
Prepare develop branch to target PHP 7.2 #80
Merged
Ocramius
merged 35 commits into
zendframework:develop
from
weierophinney:feature/php-7.1-support
Nov 29, 2018
Merged
Changes from 1 commit
Commits
Show all changes
35 commits
Select commit
Hold shift + click to select a range
b3a3de7
Update dependencies
weierophinney 7b6d85e
Add typehints wherever possible
weierophinney 583cd24
Incorporate changes requested by @vaclavvanik
weierophinney 722b1e4
Update mkdocs.yml
weierophinney 0d07c0d
Adds phpstan into CI toolchain
weierophinney 554d32d
Fix errors reported by phpstan
weierophinney 553d40b
Versions documentation
weierophinney 8ab8be9
Set explicit `object` param/return types in event implementations
weierophinney 042ce54
Use `object` return type for `HydratorListener::onHydrate()`
weierophinney fbcbebf
Adds migration document for v3
weierophinney 6334c28
Revert rename of `pages` to `nav` in `mkdocs.yml`
weierophinney e8cc238
Fix errors flagged by phpstan
weierophinney c867dc0
Use a dist file for phpstan
weierophinney 60f8dc4
Do not iterate if value is null
weierophinney 28d63e7
Use method_exists + is_callable
weierophinney a1225fd
Document all iterable and array parameters, return values, and proper…
weierophinney b81c4ac
Remove unnecessary whitespace in AggregateHydrator
weierophinney 65a93dc
Use the IdentityNamingStrategy as a default naming strategy if none i…
weierophinney 085c710
Import all global functions
weierophinney e3fafb1
Provide documentation for the HydratorPluginManagerFactory
weierophinney 03c7599
Removes unused import in StrategyChain
weierophinney 87d074b
Note that the `$hydrator` property is nullable in HydratorAwareTrait
weierophinney 5e85841
Final CS pass
weierophinney deeaeef
Use PHP 7.3, not nightly, in test matrix
weierophinney 4850813
Remove unnecessary ternary when setting `$explodeLimit`
weierophinney b6115d7
DomainException already implements ExceptionInterface
weierophinney 800dc12
Remove unnecessary property declarations in HydratingArrayIterator
weierophinney 7f62d77
Use null coalesce instead of ternary within ArrayMapNamingStrategy
weierophinney 125194c
Use null coalesce within CompositeNamingStrategy instead of ternary
weierophinney 33e18e1
Remove third argument to `substr` within UnderscoreToCamelCaseFilter
weierophinney b0babc4
Incorporate feedback from @webimpress
weierophinney 046a2ea
Correct typehint for NamingStrategyInterface::extract `$object` argument
weierophinney 1f2e3f7
Updates license docblocks for all test classes and assets
weierophinney e48e508
Remove `lcfirst` import in UnderscoreToCamelCaseFilter
weierophinney b7b5b9a
Sorting of imports when get_class/gettype are used
weierophinney File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,145 +1,6 @@ | ||
# AggregateHydrator | ||
|
||
`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> |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 destroyinggit blame
outputThere was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 👌