Add Zend\Stdlib\Hydrator\Strategy\ClosureStrategy #3230

Closed
wants to merge 1 commit into
from

Projects

None yet

5 participants

@bondvt04
Contributor

Example of use:

$hydrator->addStrategy('category', new ClosureStrategy(
    function(Category $value) {
        return (int)$value->id;
    },
    function($value) {
        return new Category((int)$value);
    }
));
@bondvt04
Contributor

Sorry, my bad, perhaps I should have used develop branch..

@ThomasCantonnet ThomasCantonnet and 1 other commented on an outdated diff Dec 16, 2012
...ary/Zend/Stdlib/Hydrator/Strategy/ClosureStrategy.php
+ protected $extractFunc = null;
+
+ /**
+ * Function, used in hydrate method, default:
+ * function($value) {
+ * return $value;
+ * };
+ * @var callable
+ */
+ protected $hydrateFunc = null;
+
+ public function __construct($extractFunc = null, $hydrateFunc = null)
+ {
+ if (isset($extractFunc)) {
+ if (!is_callable($extractFunc)) {
+ throw new \Exception('$extractFunc must be collable');
@ThomasCantonnet
ThomasCantonnet Dec 16, 2012 Contributor

callable, you wrote collable everywhere ;)

@bondvt04
bondvt04 Dec 16, 2012 Contributor

Yep, of course, 'call' shame

2012/12/16 Thomas Cantonnet notifications@github.com

In library/Zend/Stdlib/Hydrator/Strategy/ClosureStrategy.php:

  • protected $extractFunc = null;
  • /**
  • \* Function, used in hydrate method, default:
    
  • \* function($value) {
    
  • \*     return $value;
    
  • \* };
    
  • \* @var callable
    
  • */
    
  • protected $hydrateFunc = null;
  • public function __construct($extractFunc = null, $hydrateFunc = null)
  • {
  •    if (isset($extractFunc)) {
    
  •        if (!is_callable($extractFunc)) {
    
  •            throw new \Exception('$extractFunc must be collable');
    

callable, you wrote collable everywhere ;)


Reply to this email directly or view it on GitHubhttps://github.com/zendframework/zf2/pull/3230/files#r2429643.

@bondvt04
Contributor

replaced "collable" to "callable") run push -f, so commit hash may be different

@weierophinney
Member

That's fine; the main thing is that once we pull, our history should match
yours. :-)
On Dec 16, 2012 10:39 AM, "bondvt04" notifications@github.com wrote:

replaced "collable" to "callable") run push -f, so commit hash may be
different


Reply to this email directly or view it on GitHubhttps://github.com/zendframework/zf2/pull/3230#issuecomment-11419556.

@bondvt04
Contributor

find this:
Do not rebase commits that you have pushed to a public repository. If you follow that guideline, you’ll be fine. If you don’t, people will hate you, and you’ll be scorned by friends and family.
http://git-scm.com/book/en/Git-Branching-Rebasing#The-Perils-of-Rebasing

@weierophinney
Member

There's a difference between doing so for a patch meant for another repo
and doing so on a canonical repository. It's a good practice to rebase a
branch containing a patch off of the upstream branch so as to simplify the
history and make the patch diff simpler.

On Sunday, December 16, 2012, bondvt04 wrote:

find this:
Do not rebase commits that you have pushed to a public repository. If you
follow that guideline, you’ll be fine. If you don’t, people will hate you,
and you’ll be scorned by friends and family.
http://git-scm.com/book/en/Git-Branching-Rebasing#The-Perils-of-Rebasing


Reply to this email directly or view it on GitHubhttps://github.com/zendframework/zf2/pull/3230#issuecomment-11419867.

Matthew Weier O'Phinney
matthew@weierophinney.net
http://mwop.net/

@bondvt04
Contributor

Thanks for explanation, it is helps because i don't have yet the practice in working with the open source and communities..

@weierophinney
Member

Also, I need unit tests from you for this. :)

@bondvt04
Contributor

well, I'll try, need to learn something

@weierophinney
Member

@bondvt04 Can you get tests done this week? If not, I'll change the milestone to 2.2.0.

@bondvt04
Contributor

Yep! I have couple questions, not necessary to answer :) First - running 'php runtest.php' in project, cloned from canonical repository (!), master branch (!!) have few warnings. Maybe i do something wrong.. Okay, I'll deal, maybe, later. And second. I really have not idea, what exactly write into tests from hydrators strategy :) What aspects of it? I know now that I have to write tests first, and then the code, but I first wrote the strategy (because it is was useful to me in my case), and then heard about TDD :) Probably, for begin, I'll write tests for couple use cases with extracting and hydrating, in which I know how it should work.

@bakura10
Contributor

For lauching the test, try to go to the tests folder, and run 'phpunit ZendTest/Stdlib' (this will run only tests in subfolder Stdlib).

You can add basic test to ensure that the Closure thing work as expected. Basically, just create a hydrator with a your closure strategy (that will return some value like 'Foo'), hydrate the data and check that the object has been populated with what your closure strategy returns.

@bakura10 bakura10 commented on an outdated diff Jan 14, 2013
...ary/Zend/Stdlib/Hydrator/Strategy/ClosureStrategy.php
+ * return $value;
+ * };
+ * @var callable
+ */
+ protected $extractFunc = null;
+
+ /**
+ * Function, used in hydrate method, default:
+ * function($value) {
+ * return $value;
+ * };
+ * @var callable
+ */
+ protected $hydrateFunc = null;
+
+ public function __construct($extractFunc = null, $hydrateFunc = null)
@bakura10
bakura10 Jan 14, 2013 Contributor

Add docblock for constructor.

@bondvt04
Contributor

Thanks, bakura10, run tests in subfolder is awesome ability! :) I already create some testing for adding, retrieving and remove ClosureStrategy in ObjectProperty hydrator. Tomorrow I'll try finish testing with extract and hydrate, make 'commit --amend' and push here.

@bondvt04
Contributor

I added unit tests and dockblock. On local computer I create a few commits and after squash (rebase) they to one commit, so commit hash can be different.

@bondvt04
Contributor

Change docblock for constructor to more understandable.

@Maks3w Maks3w and 1 other commented on an outdated diff Jan 17, 2013
...ary/Zend/Stdlib/Hydrator/Strategy/ClosureStrategy.php
@@ -0,0 +1,106 @@
+<?php
+/**
+ * Zend Framework (http://framework.zend.com/)
+ *
+ * @link http://github.com/zendframework/zf2 for the canonical source repository
+ * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
@Maks3w
Maks3w Jan 17, 2013 Member

Happy new year

@bondvt04
bondvt04 Jan 17, 2013 Contributor

Yep, I gone create time machine :)

@bondvt04
bondvt04 Jan 17, 2013 Contributor

done.

@Maks3w Maks3w and 1 other commented on an outdated diff Jan 17, 2013
tests/ZendTest/Stdlib/HydratorClosureStrategyTest.php
@@ -0,0 +1,126 @@
+<?php
+/**
+ * Zend Framework (http://framework.zend.com/)
+ *
+ * @link http://github.com/zendframework/zf2 for the canonical source repository
+ * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
@Maks3w Maks3w and 1 other commented on an outdated diff Jan 17, 2013
...st/Stdlib/TestAsset/HydratorClosureStrategyEntity.php
@@ -0,0 +1,24 @@
+<?php
+/**
+ * Zend Framework (http://framework.zend.com/)
+ *
+ * @link http://github.com/zendframework/zf2 for the canonical source repository
+ * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
@bondvt04 bondvt04 Add Zend\Stdlib\Hydrator\Strategy\ClosureStrategy
Example of use:
$hydrator->addStrategy('category', new ClosureStrategy(
    function(Category $value) {
        return (int)$value->id;
    },
    function($value) {
        return new Category((int)$value);
    }
));
9fab01d
@weierophinney weierophinney added a commit that referenced this pull request Jan 18, 2013
@weierophinney weierophinney [#3230] CS fixes
- Trailing whitespace
e6a2905
@weierophinney
Member

Merged to develop, as this is a new feature; will release with 2.1.0.

@bondvt04
Contributor

Great! Contribute into my favorite framework :)

2013/1/18 weierophinney notifications@github.com

Merged to develop, as this is a new feature; will release with 2.1.0.


Reply to this email directly or view it on GitHubhttps://github.com/zendframework/zf2/pull/3230#issuecomment-12441185.

@weierophinney weierophinney added a commit to zendframework/zend-stdlib that referenced this pull request May 15, 2015
@weierophinney weierophinney [zendframework/zendframework#3230] CS fixes
- Trailing whitespace
196c1da
@weierophinney weierophinney added a commit to zendframework/zend-stdlib that referenced this pull request May 15, 2015
@weierophinney weierophinney Merge branch 'feature/3230' into develop 9218ee4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment