Skip to content

Commit

Permalink
Adding basic implementation for more types
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed Dec 29, 2012
1 parent c7f405f commit 4862087
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 0 deletions.
52 changes: 52 additions & 0 deletions lib/Cake/Model/Datasource/Database/Type/BooleanType.php
@@ -0,0 +1,52 @@
<?php

namespace Cake\Model\Datasource\Database\Type;

use Cake\Model\Datasource\Database\Driver;
use PDO;

class BooleanType extends \Cake\Model\Datasource\Database\Type {

/**
* Casts given value to an acceptable boolean representation for the passed
* driver
*
* @param mixed $value value to be converted to database boolean
* @param Driver $driver Driver to be used for getting boolean representation
* @todo Needs to actually ask the driver for conversion
* @return mixed
**/
public function toDatabase($value, Driver $driver) {
if ($value === null) {
return null;
}
return (bool)$value;
}

/**
* Casts given value to boolean
*
* @param mixed $value value to be converted to PHP boolean
* @param Driver $driver object from which database preferences and configuration will be extracted
* @todo Needs to actually ask the driver for conversion
* @return boolean
**/
public function toPHP($value, Driver $driver) {
if ($value === null) {
return null;
}
return (bool)$value;
}

/**
* Casts give value to Statement equivalent
*
* @param mixed $value value to be bound in a prepared statement as boolean
* @param Driver $driver object from which database preferences and configuration will be extracted
* @return mixed
**/
public function toStatement($value, Driver $driver) {
return PDO::PARAM_BOOL;
}

}
25 changes: 25 additions & 0 deletions lib/Cake/Model/Datasource/Database/Type/DateTimeType.php
@@ -0,0 +1,25 @@
<?php

namespace Cake\Model\Datasource\Database\Type;

use Cake\Model\Datasource\Database\Driver;
use \DateTime;

class DateTimeType extends \Cake\Model\Datasource\Database\Type {

public function toDatabase($value, Driver $driver) {
if (is_string($value)) {
return $value;
}
return $value->format('Y-m-d H:i:s');
}

public function toPHP($value, Driver $driver) {
if ($value === null) {
return null;
}
$value = DateTime::createFromFormat('Y-m-d H:i:s', $value);
return $value;
}

}

0 comments on commit 4862087

Please sign in to comment.