Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
IntegerDefinition.php
Go to the documentation of this file.
1 <?php
8 
13 {
19  private static $lengthDefaults = [
20  'tinyint' => 3,
21  'smallint' => 6,
22  'mediumint' => 8,
23  'int' => 11,
24  'bigint' => 20
25  ];
26 
30  private $booleanDefinition;
31 
37  public function __construct(BooleanDefinition $booleanDefinition)
38  {
39  $this->booleanDefinition = $booleanDefinition;
40  }
41 
45  public function convertToDefinition(array $definition)
46  {
47  if ($definition['type'] === 'integer') {
48  $definition['type'] = 'int';
49  }
50 
51  if (isset($definition['length']) && (int) $definition['length'] === 1) {
52  $definition['type'] = 'boolean';
53  return $this->booleanDefinition->convertToDefinition($definition);
54  }
55  $length = $definition['length'] ?? self::$lengthDefaults[$definition['type']];
56  $unsigned = $definition['unsigned'] ?? false;
57 
58  if ((bool) $unsigned && in_array($definition['type'], ['int', 'smallint'])) {
59  $length--;
60  }
61 
62  return [
63  'xsi:type' => $definition['type'],
64  'name' => $definition['name'],
65  'padding' => $length,
66  'unsigned' => $unsigned,
67  'nullable' => $definition['nullable'] ?? true,
68  'identity' => $definition['identity'] ?? false,
69  'default' => isset($definition['default']) && $definition['default'] !== false ?
70  (int) $definition['default'] : null,
71  'primary' => $definition['primary'] ?? false
72  ];
73  }
74 }