1: <?php
2: /**
3: * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
4: * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
5: *
6: * Licensed under The MIT License
7: * For full copyright and license information, please see the LICENSE.txt
8: * Redistributions of files must retain the above copyright notice.
9: *
10: * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
11: * @link https://cakephp.org CakePHP(tm) Project
12: * @since 3.0.0
13: * @license https://opensource.org/licenses/mit-license.php MIT License
14: */
15: namespace Cake\Database\Type;
16:
17: use Cake\Database\Driver;
18: use Cake\Database\Type;
19: use Cake\Database\TypeInterface;
20: use Cake\Database\Type\BatchCastingInterface;
21: use InvalidArgumentException;
22: use PDO;
23:
24: /**
25: * Integer type converter.
26: *
27: * Use to convert integer data between PHP and the database types.
28: */
29: class IntegerType extends Type implements TypeInterface, BatchCastingInterface
30: {
31: /**
32: * Identifier name for this type.
33: *
34: * (This property is declared here again so that the inheritance from
35: * Cake\Database\Type can be removed in the future.)
36: *
37: * @var string|null
38: */
39: protected $_name;
40:
41: /**
42: * Constructor.
43: *
44: * (This method is declared here again so that the inheritance from
45: * Cake\Database\Type can be removed in the future.)
46: *
47: * @param string|null $name The name identifying this type
48: */
49: public function __construct($name = null)
50: {
51: $this->_name = $name;
52: }
53:
54: /**
55: * Checks if the value is not a numeric value
56: *
57: * @throws \InvalidArgumentException
58: * @param mixed $value Value to check
59: * @return void
60: */
61: protected function checkNumeric($value)
62: {
63: if (!is_numeric($value)) {
64: throw new InvalidArgumentException(sprintf(
65: 'Cannot convert value of type `%s` to integer',
66: getTypeName($value)
67: ));
68: }
69: }
70:
71: /**
72: * Convert integer data into the database format.
73: *
74: * @param mixed $value The value to convert.
75: * @param \Cake\Database\Driver $driver The driver instance to convert with.
76: * @return int|null
77: */
78: public function toDatabase($value, Driver $driver)
79: {
80: if ($value === null || $value === '') {
81: return null;
82: }
83:
84: $this->checkNumeric($value);
85:
86: return (int)$value;
87: }
88:
89: /**
90: * Convert integer values to PHP integers
91: *
92: * @param mixed $value The value to convert.
93: * @param \Cake\Database\Driver $driver The driver instance to convert with.
94: * @return int|null
95: */
96: public function toPHP($value, Driver $driver)
97: {
98: if ($value === null) {
99: return $value;
100: }
101:
102: return (int)$value;
103: }
104:
105: /**
106: * {@inheritDoc}
107: *
108: * @return array
109: */
110: public function manyToPHP(array $values, array $fields, Driver $driver)
111: {
112: foreach ($fields as $field) {
113: if (!isset($values[$field])) {
114: continue;
115: }
116:
117: $this->checkNumeric($values[$field]);
118:
119: $values[$field] = (int)$values[$field];
120: }
121:
122: return $values;
123: }
124:
125: /**
126: * Get the correct PDO binding type for integer data.
127: *
128: * @param mixed $value The value being bound.
129: * @param \Cake\Database\Driver $driver The driver.
130: * @return int
131: */
132: public function toStatement($value, Driver $driver)
133: {
134: return PDO::PARAM_INT;
135: }
136:
137: /**
138: * Marshalls request data into PHP floats.
139: *
140: * @param mixed $value The value to convert.
141: * @return int|null Converted value.
142: */
143: public function marshal($value)
144: {
145: if ($value === null || $value === '') {
146: return null;
147: }
148: if (is_numeric($value)) {
149: return (int)$value;
150: }
151:
152: return null;
153: }
154: }
155: