Skip to content

Commit

Permalink
Don't replace array data for custom types.
Browse files Browse the repository at this point in the history
In the comments attached to
7468434, a few people
mentioned that those changes broke their custom types. This change moves
the forced casting of array data into the string/text handling only. The
original intention was not to disturb/break custom type objects.

As a side effect of this change boolean handling now better reflects the
internal PHP handling of values in that arrays are truthy.
  • Loading branch information
markstory committed Mar 17, 2015
1 parent e205a0a commit 2cf9727
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 9 deletions.
24 changes: 18 additions & 6 deletions src/Database/Type.php
Expand Up @@ -51,8 +51,8 @@ class Type
* @var array
*/
protected static $_basicTypes = [
'string' => ['callback' => 'strval'],
'text' => ['callback' => 'strval'],
'string' => ['callback' => ['\Cake\Database\Type', 'strval']],
'text' => ['callback' => ['\Cake\Database\Type', 'strval']],
'boolean' => [
'callback' => ['\Cake\Database\Type', 'boolval'],
'pdo' => PDO::PARAM_BOOL
Expand Down Expand Up @@ -186,10 +186,6 @@ protected function _basicTypeCast($value)
if ($value === null) {
return null;
}
if (is_array($value)) {
$value = '';
}

if (!empty(self::$_basicTypes[$this->_name])) {
$typeInfo = self::$_basicTypes[$this->_name];
if (isset($typeInfo['callback'])) {
Expand Down Expand Up @@ -236,6 +232,22 @@ public static function boolval($value)
return !empty($value);
}

/**
* Type converter for string values.
*
* Will convert values into strings
*
* @param mixed $value The value to convert to a string.
* @return bool
*/
public static function strval($value)
{
if (is_array($value)) {
$value = '';
}
return strval($value);
}

/**
* Generate a new primary key value for a given type.
*
Expand Down
6 changes: 3 additions & 3 deletions tests/TestCase/Database/TypeTest.php
Expand Up @@ -261,7 +261,7 @@ public function testBooleanToDatabase()
$this->assertFalse($type->toDatabase(0, $driver));
$this->assertTrue($type->toDatabase('1', $driver));
$this->assertFalse($type->toDatabase('0', $driver));
$this->assertFalse($type->toDatabase([1, 2], $driver));
$this->assertTrue($type->toDatabase([1, 2], $driver));
}

/**
Expand Down Expand Up @@ -299,7 +299,7 @@ public function testBooleanToPHP()
$this->assertFalse($type->toPHP('0', $driver));
$this->assertFalse($type->toPHP('FALSE', $driver));
$this->assertFalse($type->toPHP('false', $driver));
$this->assertFalse($type->toPHP(['2', '3'], $driver));
$this->assertTrue($type->toPHP(['2', '3'], $driver));
}

/**
Expand All @@ -320,7 +320,7 @@ public function testBooleanMarshal()
$this->assertFalse($type->marshal(0));
$this->assertFalse($type->marshal(''));
$this->assertFalse($type->marshal('invalid'));
$this->assertFalse($type->marshal(['2', '3']));
$this->assertTrue($type->marshal(['2', '3']));
}


Expand Down

0 comments on commit 2cf9727

Please sign in to comment.