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;
16:
17: /*
18: * Represents a class that holds a TypeMap object
19: */
20: /**
21: * Trait TypeMapTrait
22: */
23: trait TypeMapTrait
24: {
25: /**
26: * @var \Cake\Database\TypeMap
27: */
28: protected $_typeMap;
29:
30: /**
31: * Creates a new TypeMap if $typeMap is an array, otherwise exchanges it for the given one.
32: *
33: * @param array|\Cake\Database\TypeMap $typeMap Creates a TypeMap if array, otherwise sets the given TypeMap
34: * @return $this
35: */
36: public function setTypeMap($typeMap)
37: {
38: $this->_typeMap = is_array($typeMap) ? new TypeMap($typeMap) : $typeMap;
39:
40: return $this;
41: }
42:
43: /**
44: * Returns the existing type map.
45: *
46: * @return \Cake\Database\TypeMap
47: */
48: public function getTypeMap()
49: {
50: if ($this->_typeMap === null) {
51: $this->_typeMap = new TypeMap();
52: }
53:
54: return $this->_typeMap;
55: }
56:
57: /**
58: * Creates a new TypeMap if $typeMap is an array, otherwise returns the existing type map
59: * or exchanges it for the given one.
60: *
61: * @deprecated 3.4.0 Use setTypeMap()/getTypeMap() instead.
62: * @param array|\Cake\Database\TypeMap|null $typeMap Creates a TypeMap if array, otherwise sets the given TypeMap
63: * @return $this|\Cake\Database\TypeMap
64: */
65: public function typeMap($typeMap = null)
66: {
67: deprecationWarning(
68: 'TypeMapTrait::typeMap() is deprecated. ' .
69: 'Use TypeMapTrait::setTypeMap()/getTypeMap() instead.'
70: );
71: if ($typeMap !== null) {
72: return $this->setTypeMap($typeMap);
73: }
74:
75: return $this->getTypeMap();
76: }
77:
78: /**
79: * Overwrite the default type mappings for fields
80: * in the implementing object.
81: *
82: * This method is useful if you need to set type mappings that are shared across
83: * multiple functions/expressions in a query.
84: *
85: * To add a default without overwriting existing ones
86: * use `getTypeMap()->addDefaults()`
87: *
88: * @param array $types The array of types to set.
89: * @return $this
90: * @see \Cake\Database\TypeMap::setDefaults()
91: */
92: public function setDefaultTypes(array $types)
93: {
94: $this->getTypeMap()->setDefaults($types);
95:
96: return $this;
97: }
98:
99: /**
100: * Gets default types of current type map.
101: *
102: * @return array
103: */
104: public function getDefaultTypes()
105: {
106: return $this->getTypeMap()->getDefaults();
107: }
108:
109: /**
110: * Allows setting default types when chaining query
111: *
112: * @deprecated 3.4.0 Use setDefaultTypes()/getDefaultTypes() instead.
113: * @param array|null $types The array of types to set.
114: * @return $this|array
115: */
116: public function defaultTypes(array $types = null)
117: {
118: deprecationWarning(
119: 'TypeMapTrait::defaultTypes() is deprecated. ' .
120: 'Use TypeMapTrait::setDefaultTypes()/getDefaultTypes() instead.'
121: );
122: if ($types !== null) {
123: return $this->setDefaultTypes($types);
124: }
125:
126: return $this->getDefaultTypes();
127: }
128: }
129: