Commit 4090136a authored by Davi Koscianski Vidal's avatar Davi Koscianski Vidal Committed by lucasvanlierop

Handling all Postgres boolean literals

parent 449055af
...@@ -44,6 +44,28 @@ class PostgreSqlPlatform extends AbstractPlatform ...@@ -44,6 +44,28 @@ class PostgreSqlPlatform extends AbstractPlatform
*/ */
private $useBooleanTrueFalseStrings = true; private $useBooleanTrueFalseStrings = true;
/**
* @var array PostgreSQL booleans literals
*/
private $booleanLiterals = array(
'true' => array(
't',
'true',
'y',
'yes',
'on',
'1'
),
'false' => array(
'f',
'false',
'n',
'no',
'off',
'0'
)
);
/** /**
* PostgreSQL has different behavior with some drivers * PostgreSQL has different behavior with some drivers
* with regard to how booleans have to be handled. * with regard to how booleans have to be handled.
...@@ -701,8 +723,9 @@ class PostgreSqlPlatform extends AbstractPlatform ...@@ -701,8 +723,9 @@ class PostgreSqlPlatform extends AbstractPlatform
if (is_bool($value) || is_numeric($value)) { if (is_bool($value) || is_numeric($value)) {
$item[$key] = ($value) ? 'true' : 'false'; $item[$key] = ($value) ? 'true' : 'false';
} elseif (is_string($value)) { } elseif (is_string($value)) {
$value = trim(strtolower($value)); if (in_array(trim(strtolower($value)), $this->booleanLiterals['false'])) {
if ('false' !== $value && 'f' !== $value) { $item[$key] = 'false';
} else {
$item[$key] = 'true'; $item[$key] = 'true';
} }
} }
...@@ -711,8 +734,9 @@ class PostgreSqlPlatform extends AbstractPlatform ...@@ -711,8 +734,9 @@ class PostgreSqlPlatform extends AbstractPlatform
if (is_bool($item) || is_numeric($item)) { if (is_bool($item) || is_numeric($item)) {
$item = ($item) ? 'true' : 'false'; $item = ($item) ? 'true' : 'false';
} elseif (is_string($item)) { } elseif (is_string($item)) {
$item = trim(strtolower($item)); if (in_array(trim(strtolower($item)), $this->booleanLiterals['false'])) {
if ('false' !== $item && 'f' !== $item) { $item = 'false';
} else {
$item = 'true'; $item = 'true';
} }
} }
...@@ -735,11 +759,10 @@ class PostgreSqlPlatform extends AbstractPlatform ...@@ -735,11 +759,10 @@ class PostgreSqlPlatform extends AbstractPlatform
if (is_bool($value) || is_numeric($value)) { if (is_bool($value) || is_numeric($value)) {
$item[$key] = $value ? 1 : 0; $item[$key] = $value ? 1 : 0;
} elseif (is_string($value)) { } elseif (is_string($value)) {
$value = trim(strtolower($item)); if (in_array(trim(strtolower($item)), $this->booleanLiterals['false'])) {
if ('false' !== $value && 'f' !== $value) {
$item[$key] = 1;
} else {
$item[$key] = 0; $item[$key] = 0;
} else {
$item[$key] = 1;
} }
} }
} }
...@@ -747,11 +770,10 @@ class PostgreSqlPlatform extends AbstractPlatform ...@@ -747,11 +770,10 @@ class PostgreSqlPlatform extends AbstractPlatform
if (is_bool($item) || is_numeric($item)) { if (is_bool($item) || is_numeric($item)) {
$item = $item ? 1 : 0; $item = $item ? 1 : 0;
} elseif (is_string($item)) { } elseif (is_string($item)) {
$item = trim(strtolower($item)); if (in_array(trim(strtolower($item)), $this->booleanLiterals['false'])) {
if ('false' !== $item && 'f' !== $item) {
$item = 1;
} else {
$item = 0; $item = 0;
} else {
$item = 1;
} }
} }
} }
......
...@@ -296,7 +296,21 @@ abstract class AbstractPostgreSqlPlatformTestCase extends AbstractPlatformTestCa ...@@ -296,7 +296,21 @@ abstract class AbstractPostgreSqlPlatformTestCase extends AbstractPlatformTestCa
$platform = $this->createPlatform(); $platform = $this->createPlatform();
$this->assertEquals('true', $platform->convertBooleans(true)); $this->assertEquals('true', $platform->convertBooleans(true));
$this->assertEquals('true', $platform->convertBooleans('t'));
$this->assertEquals('true', $platform->convertBooleans('true'));
$this->assertEquals('true', $platform->convertBooleans('y'));
$this->assertEquals('true', $platform->convertBooleans('yes'));
$this->assertEquals('true', $platform->convertBooleans('on'));
$this->assertEquals('true', $platform->convertBooleans('1'));
$this->assertEquals('false', $platform->convertBooleans(false)); $this->assertEquals('false', $platform->convertBooleans(false));
$this->assertEquals('false', $platform->convertBooleans('f'));
$this->assertEquals('false', $platform->convertBooleans('false'));
$this->assertEquals('false', $platform->convertBooleans('n'));
$this->assertEquals('false', $platform->convertBooleans('no'));
$this->assertEquals('false', $platform->convertBooleans('off'));
$this->assertEquals('false', $platform->convertBooleans('0'));
} }
/** /**
......
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