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

Created hydrator strategy chain #6194

Closed
wants to merge 3 commits into from

Conversation

ojhaujjwal
Copy link
Contributor

Feedbacks are welcome!

@moderndeveloperllc
Copy link
Contributor

Might want to put [WIP] into the title. I know that the ZF2 folks will want you to have written the tests for this too. I also wonder as to the need for this. You can already add multiple strategies to a hydrator with addStrategy(). Am I missing a use case for this?

@ojhaujjwal ojhaujjwal changed the title Created hydrator strategy chain [WIP] Created hydrator strategy chain Apr 25, 2014
@ojhaujjwal
Copy link
Contributor Author

@moderndeveloperllc
I have added WIP as you said! Thanks!
I will definitely add tests.. And, that`s what WIP means.
And, You cannot add multiple strategies to the same name!

@ojhaujjwal ojhaujjwal closed this Apr 25, 2014
@ojhaujjwal
Copy link
Contributor Author

@moderndeveloperllc

Use Case:

Support, there is an entity, Event like this:

class Event
{
    protected $time;

   public function setTime(DateTime $time)
   {
        $this->time = $time;
    }

   public function getTime()
    {
        return $this->time;
    }
}

Support a user enters a time in form for that event:

class DateTimeStringStrategy implements StrategyInterface
{
    public function extract($value)
    {
        return $value->format('Y-m-d'); 
    }

    public function hydrate($value)
    {
        return new DateTime($value);
    }
}

class TimeZoneConverterStrategy implements StrategyInterface
{
    public function extract($value)
    {
        $value->setTimezone($serverTimeZone);
        return $value;
    }

    public function hydrate($value)
    {
        $value->setTimezone($clientTimeZone); 
        return $value;
    }
} 

$strategy->attach(new DateTimeStringStrategy(), 10, 5);
$strategy->attach(new TimeZoneConverterStrategy(), 5, 10);
$hydrator->addStrategy('time', $strategy);

@ojhaujjwal ojhaujjwal reopened this Apr 25, 2014
@ojhaujjwal ojhaujjwal changed the title [WIP] Created hydrator strategy chain Created hydrator strategy chain Apr 29, 2014
@ojhaujjwal
Copy link
Contributor Author

I think the tests should be enough!

namespace Zend\Stdlib\Hydrator\Strategy\Exception;

class InvalidArgumentException extends \InvalidArgumentException implements ExceptionInterface
{}
Copy link
Member

Choose a reason for hiding this comment

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

Closing brace on its own line.

@ojhaujjwal
Copy link
Contributor Author

@Ocramius Done.

@localheinz
Copy link
Member

Please restart the build.

See #6205 (comment).

@Ocramius Ocramius added this to the 2.4.0 milestone May 5, 2014
@Ocramius Ocramius removed the WIP label May 5, 2014
@bakura10
Copy link
Contributor

I'm not sure about splitting into extraction strategies and hydration strategies (note: I've back ported your code to my ZF3 refactor). Usually, extraction and hydration are quite symmetrical, what is the use case of splitting into extraction and hydration strategies?

@ojhaujjwal
Copy link
Contributor Author

@bakura10 If we do not split the strategies to extraction and hydration strategies, then,we need to need to execute strategies in exactly the opposite order during hydration and extraction That will make things simpler.

Suppose , instead of hydrationStrategies as extractionStrategies, we have only strategies.

    public function hydrate($value)
    {
        foreach ($this->strategies as $strategy) {
            $value = $strategy->hydrate($value);
        }

        return $value;
    }

    public function extract($value)
    {
        $strategies = iterator_to_array($this->strategies);
        $strategies = array_reverse($strategies);
        foreach ($strategies as $strategy) {
            $value = $strategy->extract($value);
        }

        return $value;
    }

@bakura10
Copy link
Contributor

I see... What's so bad about the array reverse options? It's indeed much simpler to use to only add one strategy instead of two, and letting the strategy handle the ordering.

Envoyé de mon iPhone

Le 19 mai 2014 à 04:31, Ujjwal Ojha [email protected] a écrit :

@bakura10 If we do not split the strategies to extraction and hydration strategies, then,we need to need to execute strategies in exactly the opposite order during hydration and extraction That will make things simpler.

Suppose , instead of hydrationStrategies as extractionStrategies, we have only strategies.

public function hydrate($value)
{
    foreach ($this->strategies as $strategy) {
        $value = $strategy->hydrate($value);
    }

    return $value;
}

public function extract($value)
{
    $strategies = iterator_to_array($this->strategies);
    $strategies = array_reverse($strategies);
    foreach ($strategies as $strategy) {
        $value = $strategy->extract($value);
    }

    return $value;
}


Reply to this email directly or view it on GitHub.

@ojhaujjwal
Copy link
Contributor Author

There is nothing bad about the array reverse. That idea came to mind recently and I wanted your feedback. So, I will do it that way.

@ojhaujjwal
Copy link
Contributor Author

@bakura10 What do you think now?

));
}
foreach ($strategies as $value) {
$strategy = is_array($value) ? $value['name'] : $value;
Copy link
Member

Choose a reason for hiding this comment

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

What if $value['name'] does not exist? Seems fragile to me...

@adamlundrigan
Copy link
Contributor

@ojhaujjwal what is the status of this? Is there a matching documentation PR for this new feature?

@ojhaujjwal
Copy link
Contributor Author

@adamlundrigan Okay, I will create a documentation PR soon. Thanks for reminding.

/**
* Default priority at which strategies are added
*/
const DEFAULT_PRIORITY = 1000;
Copy link
Member

Choose a reason for hiding this comment

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

a default priority of 1 would match the default priority of all other components using events/queues, no?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I agree. Will change!

@Ocramius Ocramius self-assigned this Dec 31, 2014
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com)
Copy link
Contributor

Choose a reason for hiding this comment

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

2015 ;)

@ojhaujjwal ojhaujjwal force-pushed the hydrator-chain branch 2 times, most recently from 8e9d3e2 to 08bd3f8 Compare January 4, 2015 11:40
@ojhaujjwal
Copy link
Contributor Author

I have made this class final and removed unnecessary setters/getters!

*/
private function setStrategies($strategies)
{
if (!is_array($strategies) && !$strategies instanceof Traversable) {
Copy link
Member

Choose a reason for hiding this comment

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

You can probably skip all this and use ArrayUtils::iteratorToArray() instead, which already encapsulates this conditional

@ojhaujjwal
Copy link
Contributor Author

Okay. I have removed priorities support!

weierophinney added a commit that referenced this pull request Feb 19, 2015
weierophinney added a commit that referenced this pull request Feb 19, 2015
@weierophinney
Copy link
Member

Merged to develop for release with 2.4.0.

@ojhaujjwal Please write up some documentation for the https://github.com/zendframework/zf2-documentation repository! :)

weierophinney added a commit to zendframework/zend-stdlib that referenced this pull request May 15, 2015
…ydrator-chain

Created hydrator strategy chain
weierophinney added a commit to zendframework/zend-stdlib that referenced this pull request May 15, 2015
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants