<?php
namespace Cake\Database\Type;
use Cake\Database\Driver;
use Cake\Database\Type;
use Cake\Database\TypeInterface;
use Cake\Database\Type\BatchCastingInterface;
use InvalidArgumentException;
use PDO;
class IntegerType extends Type implements TypeInterface, BatchCastingInterface
{
protected $_name;
public function __construct($name = null)
{
$this->_name = $name;
}
protected function checkNumeric($value)
{
if (!is_numeric($value)) {
throw new InvalidArgumentException(sprintf(
'Cannot convert value of type `%s` to integer',
getTypeName($value)
));
}
}
public function toDatabase($value, Driver $driver)
{
if ($value === null || $value === '') {
return null;
}
$this->checkNumeric($value);
return (int)$value;
}
public function toPHP($value, Driver $driver)
{
if ($value === null) {
return $value;
}
return (int)$value;
}
public function manyToPHP(array $values, array $fields, Driver $driver)
{
foreach ($fields as $field) {
if (!isset($values[$field])) {
continue;
}
$this->checkNumeric($values[$field]);
$values[$field] = (int)$values[$field];
}
return $values;
}
public function toStatement($value, Driver $driver)
{
return PDO::PARAM_INT;
}
public function marshal($value)
{
if ($value === null || $value === '') {
return null;
}
if (is_numeric($value)) {
return (int)$value;
}
return null;
}
}