]> git.mxchange.org Git - friendica.git/blobdiff - src/Database/Definition/DbaDefinition.php
spelling: days
[friendica.git] / src / Database / Definition / DbaDefinition.php
index 9f42d176a7658be08add0004db5d6d3bb94176c7..3d3a2fa928648060b1be9cfd7e02328debeb7cd1 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /**
- * @copyright Copyright (C) 2010-2022, the Friendica project
+ * @copyright Copyright (C) 2010-2023, the Friendica project
  *
  * @license GNU AGPL version 3 or any later version
  *
@@ -61,13 +61,14 @@ class DbaDefinition
        }
 
        /**
-        * Get field data for the given table
+        * Truncate field data for the given table
+        *
+        * @param string $table Name of the table to load field definitions for
+        * @param array  $data  data fields
         *
-        * @param string $table Tavle to load field definitions for
-        * @param array $data data fields
         * @return array fields for the given
         */
-       public function getFieldsForTable(string $table, array $data = []): array
+       public function truncateFieldsForTable(string $table, array $data): array
        {
                $definition = $this->definition;
                if (empty($definition[$table])) {
@@ -81,11 +82,15 @@ class DbaDefinition
                // Assign all field that are present in the table
                foreach ($fieldNames as $field) {
                        if (isset($data[$field])) {
-                               // Limit the length of varchar, varbinary, char and binrary fields
+                               // Limit the length of varchar, varbinary, char and binary fields
                                if (is_string($data[$field]) && preg_match("/char\((\d*)\)/", $definition[$table]['fields'][$field]['type'], $result)) {
                                        $data[$field] = mb_substr($data[$field], 0, $result[1]);
                                } elseif (is_string($data[$field]) && preg_match("/binary\((\d*)\)/", $definition[$table]['fields'][$field]['type'], $result)) {
                                        $data[$field] = substr($data[$field], 0, $result[1]);
+                               } elseif (is_numeric($data[$field]) && $definition[$table]['fields'][$field]['type'] === 'int') {
+                                       $data[$field] = min(max((int)$data[$field], -2147483648), 2147483647);
+                               } elseif (is_numeric($data[$field]) && $definition[$table]['fields'][$field]['type'] === 'int unsigned') {
+                                       $data[$field] = min(max((int)$data[$field], 0), 4294967295);
                                }
                                $fields[$field] = $data[$field];
                        }