Explicitly implement `IDefaultValueDatabaseTableColumn` in supported column types
[GitHub/WoltLab/WCF.git] / wcfsetup / install / files / lib / system / database / table / column / AbstractIntDatabaseTableColumn.class.php
1 <?php
2
3 namespace wcf\system\database\table\column;
4
5 /**
6 * Abstract implementation of an integer database table column.
7 *
8 * @author Matthias Schmidt
9 * @copyright 2001-2020 WoltLab GmbH
10 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
11 * @package WoltLabSuite\Core\System\Database\Table\Column
12 * @since 5.2
13 */
14 abstract class AbstractIntDatabaseTableColumn extends AbstractDatabaseTableColumn implements
15 IAutoIncrementDatabaseTableColumn,
16 IDefaultValueDatabaseTableColumn,
17 ILengthDatabaseTableColumn
18 {
19 use TAutoIncrementDatabaseTableColumn;
20 use TDefaultValueDatabaseTableColumn;
21 use TLengthDatabaseTableColumn;
22
23 /**
24 * @inheritDoc
25 */
26 public function getMinimumLength()
27 {
28 return 1;
29 }
30
31 /**
32 * @inheritDoc
33 */
34 public static function createFromData($name, array $data)
35 {
36 $length = $data['length'] ?? null;
37
38 // Unset `length` so that `parent::createFromData()` does not validate the length
39 // which is done below.
40 $data['length'] = null;
41
42 /** @var static $column */
43 $column = parent::createFromData($name, $data);
44
45 if ($length) {
46 try {
47 $column->length($length);
48 } catch (\InvalidArgumentException $e) {
49 // Ignore exceptions due to the length being to large.
50 // Such cases can happen when columns were created using the SQL PIP
51 // where the length (which is just a display length for integer column
52 // types) is not validated. To update tables with such columns,
53 // exceptions related to the maximum length must be ignored.
54 if ($length > $column->getMaximumLength()) {
55 $column->length = $length;
56 } else {
57 throw $e;
58 }
59 }
60 }
61
62 return $column;
63 }
64 }