Commit d2995659 authored by Davi Koscianski Vidal's avatar Davi Koscianski Vidal Committed by lucasvanlierop

Improving conversion using @deeky666 idea

parent 08b83087
...@@ -708,41 +708,76 @@ class PostgreSqlPlatform extends AbstractPlatform ...@@ -708,41 +708,76 @@ class PostgreSqlPlatform extends AbstractPlatform
} }
/** /**
* {@inheritDoc} * Converts a single boolean value.
* *
* Postgres wants boolean values converted to the strings 'true'/'false'. * First converts the value to its native PHP boolean type
* and passes it to the given callback function to be reconverted
* into any custom representation.
*
* @param mixed $value The value to convert.
* @param callable $callback The callback function to use for converting the real boolean value.
*
* @return mixed
*/ */
public function convertBooleans($item) private function convertSingleBooleanValue($value, $callback)
{ {
if ( ! $this->useBooleanTrueFalseStrings) { if (null === $value) {
return parent::convertBooleans($item); return $callback(false);
} }
if (is_array($item)) {
foreach ($item as $key => $value) {
if (is_bool($value) || is_numeric($value)) { if (is_bool($value) || is_numeric($value)) {
$item[$key] = ($value) ? 'true' : 'false'; return $callback($value ? true : false);
} elseif (is_string($value)) {
if (in_array(trim(strtolower($value)), $this->booleanLiterals['false'])) {
$item[$key] = 'false';
} else {
$item[$key] = 'true';
} }
if (is_string($value) && in_array(trim(strtolower($value)), $this->booleanLiterals['false'])) {
return $callback(false);
} }
return $callback(true);
} }
} else {
if (is_bool($item) || is_numeric($item)) { /**
$item = ($item) ? 'true' : 'false'; * Converts one or multiple boolean values.
} elseif (is_string($item)) { *
if (in_array(trim(strtolower($item)), $this->booleanLiterals['false'])) { * First converts the value(s) to their native PHP boolean type
$item = 'false'; * and passes them to the given callback function to be reconverted
} else { * into any custom representation.
$item = 'true'; *
* @param $item The value(s) to convert.
* @param $callback The callback function to use for converting the real boolean value(s).
*
* @return mixed
*/
private function doConvertBooleans($item, $callback)
{
if (is_array($item)) {
foreach ($item as $key => $value) {
$item[$key] = $this->convertSingleBooleanValue($value, $callback);
} }
return $item;
} }
return $this->convertSingleBooleanValue($item, $callback);
} }
return $item; /**
* {@inheritDoc}
*
* Postgres wants boolean values converted to the strings 'true'/'false'.
*/
public function convertBooleans($item)
{
if ( ! $this->useBooleanTrueFalseStrings) {
return parent::convertBooleans($item);
}
return $this->doConvertBooleans(
$item,
function ($boolean) {
return true === $boolean ? 'true' : 'false';
}
);
} }
/** /**
...@@ -754,19 +789,12 @@ class PostgreSqlPlatform extends AbstractPlatform ...@@ -754,19 +789,12 @@ class PostgreSqlPlatform extends AbstractPlatform
return parent::convertBooleansToDatabaseValue($item); return parent::convertBooleansToDatabaseValue($item);
} }
$item = $this->convertBooleans($item); return $this->doConvertBooleans(
if (is_array($item)) { $item,
$item = array_map( function ($boolean) {
function ($element) { return (int) $boolean;
return (int) ('true' === $element);
},
$item
);
} else {
$item = (int) ('true' === $item);
} }
);
return $item;
} }
/** /**
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment