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
Boolean conversion #527
Boolean conversion #527
Conversation
Default Boolean conversion behaviour
added postgresql literals to Boolean conversion
Changed to call platform specific Boolean conversion
added postgresql platform conversion test
Hello, thank you for creating this pull request. I have automatically opened an issue http://www.doctrine-project.org/jira/browse/DBAL-811 We use Jira to track the state of pull requests and the versions they got |
fixed bug when $item was set as bool true value added 'off' literal
added literals off and on to the testcase
*/ | ||
public function convertFromBoolean($item) | ||
{ | ||
if(in_array((string)$item, array('false', 'f', 'n', 'no', 'off'))) |
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.
Can you fix the CS here? We follow PSR-2 with some minor additions
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 avoid the call to in_array
if the returned value is either null
, true
or false
Changes after Ocramius comments
fixed parameter order in conditions
@Ocramius hope I made the changes correctly in the way you wanted |
Description fix
*/ | ||
public function convertFromBoolean($item) | ||
{ | ||
if((null !== $item) && (false !== $item) && (true !== $item) && in_array((string)$item, array('false', 'f', 'n', 'no', 'off'))) |
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.
Break this line
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 mean the "if" statement?
Ok now I don't know what more I could do for the CS. |
in_array((string)$item, array('false', 'f', 'n', 'no', 'off')) | ||
) { | ||
return false; | ||
} else { |
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 avoid the else
clause completely, since you're returning early
updated convert function to include uppercased values
Updated test for uppercased values
@Ocramius You were right I forgot to think about uppercased values |
@deeky666 think another method in the abstract platform is ok? Otherwise, looks good to me. Could need squashing of the commits, but I can do that before someone merges |
@Ocramius Sorry for too much commints. I had problems cloning the repo into my PC so most of the changes I made here via Github edit function |
@birko that can be dealt with, no problem |
*/ | ||
public function convertFromBoolean($item) | ||
{ | ||
return (null === $item) ? null : (bool) $item; |
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.
Please use the short ternary expression: return null === $item ?: (bool) $item
@birko Can you please give more details about the issue here? I can see that PostgreSQL allows different boolean values but I don't understand why we need to check for those when retrieving them from the database. AFAIK |
@deeky666: After a bit self brainstorming I thought it would be better if there was support for all Boolean literals (http://www.postgresql.org/docs/9.3/static/datatype-boolean.html), in case someone would like to use DBAL to parse database data that come not directly from database but in a kind of CSV export. (Like export to csv from pgadmin.) And since in the Doctrine\DBAL\Types\BooleanType::convertToPHPValue was the $platform parameter I used it to make it more universal too. |
reverted @deeky666 suggestion. Since it failed on testBooleanNullConvertsToPHPValue test. with message Failed asserting that true is null. Since I moved this code from BoolenType::convertToPHPValue in the first place, I presume the code there correct.
{ | ||
if ((null !== $item) && | ||
(false !== $item) && | ||
(true !== $item) && |
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.
All these cases can be removed, because they will not in be in the array
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.
@stof I added this additional conditions after one of previous hints here, That I should add them there to avoid unnecessary in_array check
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.
It avoids unnessecary in_array() checks but adds new separate checks in the if statement and makes the code more complicated which reduces readability.
* | ||
* @param mixed $item | ||
* | ||
* @return bool |
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.
Please add the null as return value also, so: @return bool|null
sorry
Merged in #625 |
Added platform specific conversion form database Boolean type to PHP bool type