-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Created Explode Strategy for hydrator #6227
Created Explode Strategy for hydrator #6227
Conversation
} | ||
|
||
/** | ||
* Converts the given value so that it can be hydrated by the hydrator. |
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.
This is a general description, it does not describe what this method does.
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.
See below
Just trying to break things 😃 $strategy = new ExplodeStrategy();
$strategy->setValueDelimiter(null);
$result = $strategy->hydrate('foo');
// Warning: explode(): Empty delimiter in... $strategy = new ExplodeStrategy();
$strategy->setValueDelimiter('');
$result = $strategy->hydrate('foo');
// Warning: explode(): Empty delimiter in... Explode has a third parameter (http://www.php.net/explode), should that be covered as well? |
@Martin-P Thank you for providing the details. |
@Martin-P Hey. I have done some changes. Please comment. |
Will this PR get merged for 2.4? |
*/ | ||
public function setExplodeLimit($explodeLimit) | ||
{ | ||
if (!is_int($explodeLimit)) { |
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.
Not sure if it needs implementation, but should 1
(as a string) be allowed as well?
$strategy->setExplodeLimit('1');
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.
Not sure.
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.
We may use is_numeric
instead of is_int
.
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.
is_numeric
allows too many things, like e.g. hexadecimal values. If you implement it, ctype_digit
is more accurate.
if (!ctype_digit((string) $explodeLimit)) {
However ctype_digit
does not allow negative values:
var_dump(ctype_digit('-1')); // false
Casting to int
could also be an option, but I'm not sure that is the way to do this.
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.
We need some expert's help.
ping @Ocramius
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 could cast to int
and replace the default value of null
with 0
. If you cast everything to int the implementation will be more simple
This will need a documentation PR to add it to the list of available implementations |
@ojhaujjwal @weierophinney does this seem like a common enough use case to warrant including in core? Just a thought: would it be better to provide a more generalized adapter to |
@adamlundrigan One use case is that it can be used in forms to take comma separated input from the user. |
* 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 ;)
753c990
to
26af277
Compare
…for the `Zend\Stdlib\Hydrator\Strategy` namespace
…he `Zend\Stdlib\Hydrator\Strategy` exceptions
…isallow `null` `$delimiter` in constructor
…valid (not-hydratable) type
… invalid (not-hydratable) type
…hydrate integer/double values
…hydrate integer/double values
…rate()` return values
… to be hydrated by `ExplodeStrategy#hydrate()`
… bijectively
… bijectively
…ertEquals` wherever possible
…o `null`, not to an empty `string`
… object type being hydrated
…de-strategy' into develop Close zendframework/zendframework#6227
Too simple and self-explanatory.
Please see the tests.