Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Release #3

Merged
merged 28 commits into from May 23, 2014
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
9facb59
Release
bakura10 May 17, 2014
ec38ed5
Change namespace
bakura10 May 17, 2014
1739e0e
Feedback
bakura10 May 17, 2014
c6deb42
Add strategies to constructor
bakura10 May 17, 2014
197b00e
Add strategies to constructor
bakura10 May 17, 2014
e6feb51
Fix CS
bakura10 May 17, 2014
0b33b4c
Prevent context instantiation
bakura10 May 17, 2014
e87a106
Add README
bakura10 May 17, 2014
1345e20
Allow null context
bakura10 May 18, 2014
6e56490
Add support for timestamp
bakura10 May 18, 2014
cd99bc2
Add ClosureStrategy and StrategyChain
bakura10 May 18, 2014
35be2e2
Removing strategies in constructor for now
bakura10 May 18, 2014
ed039ac
Rewrite composite filter
bakura10 May 18, 2014
4b5dc5f
Transfering context to callback filter
bakura10 May 18, 2014
d5cf9ef
Remove Strategy Chain for now
bakura10 May 19, 2014
a80c926
Add FilterChain and update tests
bakura10 May 20, 2014
6a95ec7
Remove coveralls
bakura10 May 20, 2014
b3a603e
Optimization
bakura10 May 20, 2014
16b70e8
Remove elseif
bakura10 May 20, 2014
03b4dd6
Use traits for reusability
bakura10 May 20, 2014
99f7cdf
Add a useCache hint
bakura10 May 20, 2014
35b7522
If you dont like that, blame @ocramius, not me. Thx bye
bakura10 May 20, 2014
3053ac8
Fix tests
bakura10 May 20, 2014
b546754
Remove filters from final hydrators
bakura10 May 20, 2014
3c27229
Add rationale behind final
bakura10 May 20, 2014
77ee00d
Remove ZF2 compatibility but use custom SM key instead
bakura10 May 20, 2014
7ec2294
Fix tests
bakura10 May 21, 2014
38c396d
Add missing test
bakura10 May 21, 2014
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
@@ -0,0 +1,3 @@
vendor/
composer.lock
composer.phar
22 changes: 22 additions & 0 deletions .travis.yml
@@ -0,0 +1,22 @@
language: php

php:
- 5.5
- 5.6
- hhvm
- hhvm-nightly

before_script:
- composer self-update
- composer update --prefer-source --dev

script:
- ./vendor/bin/phpunit --coverage-clover ./build/logs/clover.xml --group=Coverage
- ./vendor/bin/phpcs --standard=PSR2 ./src/ ./tests

after_script:
- php vendor/bin/coveralls -v

notifications:
irc: "irc.freenode.org#zftalk.modules"
email: false
43 changes: 43 additions & 0 deletions README.md
@@ -0,0 +1,43 @@
# Hydrator

This component is a prototype for Zend Framework 3 hydrator component, that you can use today for new projects (see
below for more details). Compared to original implementation, it offers:

* A massive performance boost (this new implementation is between 5-10 times faster than current one)
* Cleaner (a lot of features that were hacked throughout ZF2 life have been integrated from the beginning).
* Provides built-in strategies for common use cases (as of now, `DateStrategy` has well as a hydrator that allows
nested hydrators).
* Some interfaces have been modified to be more explicit (see `NamingStrategyInterface` for instance).
* Contexts are handled in a more homogeneous way through two new context objects: `ExtractionContext` and
`HydrationContext`. In ZF2, you could only get the hydrated array data during hydration phase, now you can also
have access to the hydrated object.

## Using in existing/new projects

This library can be used in any projects. If you are a Zend Framework 2 user, you can actually use it now in new
projects, by providing the following configuration:

```php
use Hydrator\Factory\HydratorPluginManagerFactory;

return [
'service_manager' => [
'factories' => [
'HydratorManager' => HydratorPluginManagerFactory::class
]
]
];
```

### Limitations

Current implementation has some compatibility layer to make it compatible with ZF2. For example, this library's
hydrator interface extends `Zend\Stdlib\Hydrator\HydratorInterface`. Furthermore, the hydrator plugin manager allows
you to create both `Hydrator\HydratorInterface` (new) and `Zend\Stdlib\Hydrator\HydratorInterface` (old).

Actually, if your third-party modules or other ZF2 components are using hydrators in a very simple way (using
`extract` and `hydrate` method), this library could be used as a drop-in with very few changes.

However, as soon as the code uses more advanced features like naming strategies, custom filters or strategies,
there is great chance that it won't work because of conflicting interfaces. You are therefore encouraged to
carefully test your code before using this library.
@@ -0,0 +1,91 @@
<?php
/*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the MIT license.
*/

namespace HydratorBenchmark;

use Athletic\AthleticEvent;
use Hydrator\ArraySerializableHydrator;
use Hydrator\Context\HydrationContext;
use HydratorBenchmark\Asset\ArraySerializableObject;

class ArraySerializableHydratorBenchmark extends AthleticEvent
{
/**
* @var ArraySerializableHydrator
*/
protected $hydrator;

public function __construct()
{
$this->hydrator = new ArraySerializableHydrator();
}

/**
* @iterations 100
*/
public function hydratorExtractionWithTwentyProperties()
{
$object = new ArraySerializableObject();
$this->hydrator->extract($object);
}

/**
* @iterations 100
*/
public function hydratorExtractionReusingHydratorWithTwentyProperties()
{
for ($i = 0 ; $i != 20 ; ++$i) {
$object = new ArraySerializableObject();
$this->hydrator->extract($object);
}
}

/**
* @iterations 100
*/
public function hydratorHydrationWithTwentyProperties()
{
$object = new ArraySerializableObject();
$data = [
'one' => 1, 'two' => 2, 'three' => 3, 'four' => 4, 'five' => 5,
'six' => 6, 'seven' => 7, 'eight' => 8, 'nine' => 9, 'ten' => 10,
'eleven' => 11, 'twelve' => 12, 'thirteen' => 13, 'fourteen' => 14, 'fifteen' => 15,
'sixteen' => 16, 'seventeen' => 17, 'eighteen' => 18, 'nineteen' => 19, 'twenty' => 19
];

$this->hydrator->hydrate($data, $object);
}

/**
* @iterations 100
*/
public function hydratorHydrationReusingHydratorWithTwentyProperties()
{
for ($i = 0 ; $i != 20 ; ++$i) {
$object = new ArraySerializableObject();
$data = [
'one' => 1, 'two' => 2, 'three' => 3, 'four' => 4, 'five' => 5,
'six' => 6, 'seven' => 7, 'eight' => 8, 'nine' => 9, 'ten' => 10,
'eleven' => 11, 'twelve' => 12, 'thirteen' => 13, 'fourteen' => 14, 'fifteen' => 15,
'sixteen' => 16, 'seventeen' => 17, 'eighteen' => 18, 'nineteen' => 19, 'twenty' => 19
];

$this->hydrator->hydrate($data, $object);
}
}
}
62 changes: 62 additions & 0 deletions benchmarks/HydratorBenchmark/Asset/ArraySerializableObject.php
@@ -0,0 +1,62 @@
<?php
/*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the MIT license.
*/

namespace HydratorBenchmark\Asset;

class ArraySerializableObject
{
protected $one = 'one';
protected $two = 'two';
protected $three = 'three';
protected $four = 'four';
protected $five = 'five';
protected $six = 'six';
protected $seven = 'seven';
protected $eight = 'eight';
protected $nine = 'nine';
protected $ten = 'ten';
protected $eleven = 'eleven';
protected $twelve = 'twelve';
protected $thirteen = 'thirteen';
protected $fourteen = 'fourteen';
protected $fifteen = 'fifteen';
protected $sixteen = 'sixteen';
protected $seventeen = 'seventeen';
protected $eighteen = 'eighteen';
protected $nineteen = 'nineteen';
protected $twenty = 'twenty';

public function getArrayCopy()
{
return [
'one' => $this->one, 'two' => $this->two, 'three' => $this->three, 'four' => $this->four,
'five' => $this->five, 'six' => $this->six, 'seven' => $this->seven, 'eight' => $this->eight,
'nine' => $this->nine, 'ten' => $this->ten, 'eleven' => $this->eleven, 'twelve' => $this->twelve,
'thirteen' => $this->thirteen, 'fourteen' => $this->fourteen, 'fifteen' => $this->fifteen,
'sixteen' => $this->sixteen, 'seventeen' => $this->seventeen, 'eighteen' => $this->eighteen,
'nineteen' => $this->nineteen, 'twenty' => $this->twenty
];
}

public function populate(array $data)
{
foreach ($data as $key => $value) {
$this->$key = $value;
}
}
}