-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Conversation
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 |
@moderndeveloperllc |
Use Case:Support, there is an entity, 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); |
I think the tests should be enough! |
namespace Zend\Stdlib\Hydrator\Strategy\Exception; | ||
|
||
class InvalidArgumentException extends \InvalidArgumentException implements ExceptionInterface | ||
{} |
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.
Closing brace on its own line.
@Ocramius Done. |
Please restart the build. See #6205 (comment). |
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? |
@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 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;
} |
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
|
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. |
@bakura10 What do you think now? |
)); | ||
} | ||
foreach ($strategies as $value) { | ||
$strategy = is_array($value) ? $value['name'] : $value; |
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.
What if $value['name']
does not exist? Seems fragile to me...
@ojhaujjwal what is the status of this? Is there a matching documentation PR for this new feature? |
@adamlundrigan Okay, I will create a documentation PR soon. Thanks for reminding. |
/** | ||
* Default priority at which strategies are added | ||
*/ | ||
const DEFAULT_PRIORITY = 1000; |
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.
a default priority of 1
would match the default priority of all other components using events/queues, no?
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.
I agree. Will change!
* 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) |
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.
2015 ;)
8e9d3e2
to
08bd3f8
Compare
I have made this class final and removed unnecessary setters/getters! |
*/ | ||
private function setStrategies($strategies) | ||
{ | ||
if (!is_array($strategies) && !$strategies instanceof Traversable) { |
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.
You can probably skip all this and use ArrayUtils::iteratorToArray()
instead, which already encapsulates this conditional
7991aa7
to
96f1182
Compare
Okay. I have removed priorities support! |
Created hydrator strategy chain
Merged to develop for release with 2.4.0. @ojhaujjwal Please write up some documentation for the https://github.com/zendframework/zf2-documentation repository! :) |
…ydrator-chain Created hydrator strategy chain
Feedbacks are welcome!