<?php
namespace Cake\Database;
use InvalidArgumentException;
use PDO;
class Type implements TypeInterface
{
protected static $_types = [
'tinyinteger' => 'Cake\Database\Type\IntegerType',
'smallinteger' => 'Cake\Database\Type\IntegerType',
'integer' => 'Cake\Database\Type\IntegerType',
'biginteger' => 'Cake\Database\Type\IntegerType',
'binary' => 'Cake\Database\Type\BinaryType',
'binaryuuid' => 'Cake\Database\Type\BinaryUuidType',
'boolean' => 'Cake\Database\Type\BoolType',
'date' => 'Cake\Database\Type\DateType',
'datetime' => 'Cake\Database\Type\DateTimeType',
'decimal' => 'Cake\Database\Type\DecimalType',
'float' => 'Cake\Database\Type\FloatType',
'json' => 'Cake\Database\Type\JsonType',
'string' => 'Cake\Database\Type\StringType',
'text' => 'Cake\Database\Type\StringType',
'time' => 'Cake\Database\Type\TimeType',
'timestamp' => 'Cake\Database\Type\DateTimeType',
'uuid' => 'Cake\Database\Type\UuidType',
];
protected static $_basicTypes = [
'string' => ['callback' => [Type::class, 'strval']],
'text' => ['callback' => [Type::class, 'strval']],
'boolean' => [
'callback' => [Type::class, 'boolval'],
'pdo' => PDO::PARAM_BOOL
],
];
protected static $_builtTypes = [];
protected $_name;
public function __construct($name = null)
{
$this->_name = $name;
}
public static function build($name)
{
if (isset(static::$_builtTypes[$name])) {
return static::$_builtTypes[$name];
}
if (!isset(static::$_types[$name])) {
throw new InvalidArgumentException(sprintf('Unknown type "%s"', $name));
}
if (is_string(static::$_types[$name])) {
return static::$_builtTypes[$name] = new static::$_types[$name]($name);
}
return static::$_builtTypes[$name] = static::$_types[$name];
}
public static function buildAll()
{
$result = [];
foreach (static::$_types as $name => $type) {
$result[$name] = isset(static::$_builtTypes[$name]) ? static::$_builtTypes[$name] : static::build($name);
}
return $result;
}
public static function set($name, Type $instance)
{
static::$_builtTypes[$name] = $instance;
}
public static function map($type = null, $className = null)
{
if ($type === null) {
deprecationWarning(
'Using `Type::map()` as getter is deprecated. ' .
'Use `Type::getMap()` instead.'
);
return static::$_types;
}
if (is_array($type)) {
deprecationWarning(
'Using `Type::map()` to set complete types map is deprecated. ' .
'Use `Type::setMap()` instead.'
);
static::$_types = $type;
return null;
}
if ($className === null) {
deprecationWarning(
'Using `Type::map()` as getter is deprecated. ' .
'Use `Type::getMap()` instead.'
);
return isset(static::$_types[$type]) ? static::$_types[$type] : null;
}
if (!is_string($className)) {
deprecationWarning(
'Passing $className as object to Type::map() is deprecated. ' .
'Use Type::set() instead.'
);
}
static::$_types[$type] = $className;
unset(static::$_builtTypes[$type]);
}
public static function setMap(array $map)
{
static::$_types = $map;
static::$_builtTypes = [];
}
public static function getMap($type = null)
{
if ($type === null) {
return static::$_types;
}
return isset(static::$_types[$type]) ? static::$_types[$type] : null;
}
public static function clear()
{
static::$_types = [];
static::$_builtTypes = [];
}
public function getName()
{
return $this->_name;
}
public function getBaseType()
{
return $this->_name;
}
public function toDatabase($value, Driver $driver)
{
return $this->_basicTypeCast($value);
}
public function toPHP($value, Driver $driver)
{
return $this->_basicTypeCast($value);
}
protected function _basicTypeCast($value)
{
deprecationWarning(
'Using Type::_basicTypeCast() is deprecated. ' .
"The '{$this->_name}' type needs to be updated to implement `TypeInterface`."
);
if ($value === null) {
return null;
}
if (!empty(static::$_basicTypes[$this->_name])) {
$typeInfo = static::$_basicTypes[$this->_name];
if (isset($typeInfo['callback'])) {
return $typeInfo['callback']($value);
}
}
return $value;
}
public function toStatement($value, Driver $driver)
{
if ($value === null) {
return PDO::PARAM_NULL;
}
return PDO::PARAM_STR;
}
public static function boolval($value)
{
deprecationWarning('Type::boolval() is deprecated.');
if (is_string($value) && !is_numeric($value)) {
return strtolower($value) === 'true';
}
return !empty($value);
}
public static function strval($value)
{
deprecationWarning('Type::strval() is deprecated.');
if (is_array($value)) {
$value = '';
}
return (string)$value;
}
public function newId()
{
return null;
}
public function marshal($value)
{
return $this->_basicTypeCast($value);
}
public function __debugInfo()
{
return [
'name' => $this->_name,
];
}
}