88
99namespace OCP \AppFramework \Db ;
1010
11+ use OCP \DB \Schema \ColumnType ;
1112use OCP \DB \Types ;
1213use function lcfirst ;
1314use function substr ;
@@ -23,8 +24,8 @@ abstract class Entity {
2324 public $ id ;
2425 /** @var array<string, true> $_updatedFields */
2526 private array $ _updatedFields = [];
26- /** @var array<string, Types::* > $_fieldTypes */
27- protected array $ _fieldTypes = ['id ' => ' integer ' ];
27+ /** @var array<string, ColumnType > $_fieldTypes */
28+ protected array $ _fieldTypes = ['id ' => ColumnType::Integer ];
2829
2930 /**
3031 * Simple alternative constructor for building entities from a request
@@ -66,7 +67,7 @@ public static function fromRow(array $row): static {
6667 * @since 7.0.0
6768 */
6869 public function getFieldTypes (): array {
69- return $ this ->_fieldTypes ;
70+ return array_map ( fn ( ColumnType $ type ) => $ type -> value , $ this ->_fieldTypes ) ;
7071 }
7172
7273 /**
@@ -98,7 +99,7 @@ protected function setter(string $name, array $args): void {
9899 // if type definition exists, cast to correct type
99100 if ($ args [0 ] !== null && array_key_exists ($ name , $ this ->_fieldTypes )) {
100101 $ type = $ this ->_fieldTypes [$ name ];
101- if ($ type === Types:: BLOB ) {
102+ if ($ type === ColumnType::Blob ) {
102103 // (B)LOB is treated as string when we read from the DB
103104 if (is_resource ($ args [0 ])) {
104105 $ args [0 ] = stream_get_contents ($ args [0 ]);
@@ -107,32 +108,32 @@ protected function setter(string $name, array $args): void {
107108 }
108109
109110 switch ($ type ) {
110- case Types:: BIGINT :
111- case Types:: SMALLINT :
111+ case ColumnType::Bigint :
112+ case ColumnType::Smallint :
112113 settype ($ args [0 ], Types::INTEGER );
113114 break ;
114- case Types:: BINARY :
115- case Types:: DECIMAL :
116- case Types:: TEXT :
115+ case ColumnType::Binary :
116+ case ColumnType::Decimal :
117+ case ColumnType::Text :
117118 settype ($ args [0 ], Types::STRING );
118119 break ;
119- case Types:: TIME :
120- case Types:: DATE :
121- case Types:: DATETIME :
122- case Types:: DATETIME_TZ :
120+ case ColumnType::Time :
121+ case ColumnType::Date :
122+ case ColumnType::Datetime :
123+ case ColumnType::DatetimeTz :
123124 if (!$ args [0 ] instanceof \DateTime) {
124125 $ args [0 ] = new \DateTime ($ args [0 ]);
125126 }
126127 break ;
127- case Types:: TIME_IMMUTABLE :
128- case Types:: DATE_IMMUTABLE :
129- case Types:: DATETIME_IMMUTABLE :
130- case Types:: DATETIME_TZ_IMMUTABLE :
128+ case ColumnType::TimeImmutable :
129+ case ColumnType::DateImmutable :
130+ case ColumnType::DatetimeImmutable :
131+ case ColumnType::DatetimeTzImmutable :
131132 if (!$ args [0 ] instanceof \DateTimeImmutable) {
132133 $ args [0 ] = new \DateTimeImmutable ($ args [0 ]);
133134 }
134135 break ;
135- case Types:: JSON :
136+ case ColumnType::Json :
136137 if (!is_array ($ args [0 ])) {
137138 $ args [0 ] = json_decode ($ args [0 ], true );
138139 }
@@ -187,7 +188,7 @@ public function __call(string $methodName, array $args) {
187188 protected function isGetterForBoolProperty (string $ methodName ): bool {
188189 if (str_starts_with ($ methodName , 'is ' )) {
189190 $ fieldName = lcfirst (substr ($ methodName , 2 ));
190- return isset ($ this ->_fieldTypes [$ fieldName ]) && str_starts_with ($ this ->_fieldTypes [$ fieldName ], 'bool ' );
191+ return isset ($ this ->_fieldTypes [$ fieldName ]) && str_starts_with ($ this ->_fieldTypes [$ fieldName ]-> value , 'bool ' );
191192 }
192193 return false ;
193194 }
@@ -258,23 +259,28 @@ public function getUpdatedFields(): array {
258259 * that value once its being returned from the database
259260 *
260261 * @param string $fieldName the name of the attribute
261- * @param Types::* $type the type which will be used to match a cast
262+ * @param Types::*|ColumnType $type the type which will be used to match a cast
262263 * @since 31.0.0 Parameter $type is now restricted to {@see Types} constants. The formerly accidentally supported types 'int'|'bool'|'double' are mapped to Types::INTEGER|Types::BOOLEAN|Types::FLOAT accordingly.
264+ * @since 35.0.0 Parameter $type now prefers using one of the {@see ColumnType} enum values.
263265 * @since 7.0.0
264266 */
265- protected function addType (string $ fieldName , string $ type ): void {
267+ protected function addType (string $ fieldName , string | ColumnType $ type ): void {
266268 /** @psalm-suppress TypeDoesNotContainType */
267269 if (in_array ($ type , ['bool ' , 'double ' , 'int ' , 'array ' , 'object ' ], true )) {
268270 // Mapping legacy strings to the actual types
269271 $ type = match ($ type ) {
270- 'int ' => Types:: INTEGER ,
271- 'bool ' => Types:: BOOLEAN ,
272- 'double ' => Types:: FLOAT ,
272+ 'int ' => ColumnType::Integer ,
273+ 'bool ' => ColumnType::Boolean ,
274+ 'double ' => ColumnType::Float ,
273275 'array ' ,
274- 'object ' => Types:: STRING ,
276+ 'object ' => ColumnType::String ,
275277 };
276278 }
277279
280+ if (is_string ($ type )) {
281+ $ type = ColumnType::from ($ type );
282+ }
283+
278284 $ this ->_fieldTypes [$ fieldName ] = $ type ;
279285 }
280286
0 commit comments