Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding basic implementation for more types
- Loading branch information
Showing
2 changed files
with
77 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
|
||
} |